Tidy up a bit

This commit is contained in:
Neil Alexander 2020-05-06 09:30:12 +01:00
parent 4ff46624ff
commit 7434715f70

View file

@ -33,7 +33,7 @@ const (
// How many times should we tolerate consecutive failures before we // How many times should we tolerate consecutive failures before we
// just blacklist the host altogether? Bear in mind that the backoff // just blacklist the host altogether? Bear in mind that the backoff
// is exponential, so the max time here to attempt is 2**failures. // is exponential, so the max time here to attempt is 2**failures.
FailuresUntilBlacklist = 16 FailuresUntilBlacklist = 16 // 16 equates to roughly 18 hours.
) )
// destinationQueue is a queue of events for a single destination. // destinationQueue is a queue of events for a single destination.
@ -41,13 +41,13 @@ const (
// ensures that only one request is in flight to a given destination // ensures that only one request is in flight to a given destination
// at a time. // at a time.
type destinationQueue struct { type destinationQueue struct {
rsProducer *producers.RoomserverProducer // rsProducer *producers.RoomserverProducer // roomserver producer
client *gomatrixserverlib.FederationClient // client *gomatrixserverlib.FederationClient // federation client
origin gomatrixserverlib.ServerName // origin gomatrixserverlib.ServerName // origin of requests
destination gomatrixserverlib.ServerName // destination gomatrixserverlib.ServerName // destination of requests
running atomic.Bool // is the queue worke running? running atomic.Bool // is the queue worker running?
blacklisted atomic.Bool // is the remote side dead? blacklisted atomic.Bool // is the remote side dead?
backoffUntil atomic.Value // time.Time backoffUntil atomic.Value // time.Time to wait until before sending requests
idleCounter atomic.Uint32 // how many ticks have we done nothing? idleCounter atomic.Uint32 // how many ticks have we done nothing?
failCounter atomic.Uint32 // how many times have we failed? failCounter atomic.Uint32 // how many times have we failed?
sentCounter atomic.Uint32 // how many times have we succeeded? sentCounter atomic.Uint32 // how many times have we succeeded?
@ -124,11 +124,9 @@ func (oq *destinationQueue) sendEvent(ev *gomatrixserverlib.HeaderedEvent) {
// If the destination is blacklisted then drop the event. // If the destination is blacklisted then drop the event.
return return
} }
fmt.Println("Queuing event", ev.EventID())
oq.runningMutex.Lock() oq.runningMutex.Lock()
oq.pendingPDUs = append(oq.pendingPDUs, ev) oq.pendingPDUs = append(oq.pendingPDUs, ev)
oq.runningMutex.Unlock() oq.runningMutex.Unlock()
fmt.Println("Queued event", ev.EventID())
oq.wake() oq.wake()
} }
@ -206,8 +204,16 @@ func (oq *destinationQueue) backgroundSend() {
// the pending events and EDUs. // the pending events and EDUs.
if transaction { if transaction {
oq.runningMutex.Lock() oq.runningMutex.Lock()
oq.pendingPDUs = oq.pendingPDUs[len(pendingPDUs):] // Reallocate so that the underlying arrays can be GC'd, as
oq.pendingEDUs = oq.pendingEDUs[len(pendingEDUs):] // opposed to growing forever.
oq.pendingPDUs = append(
[]*gomatrixserverlib.HeaderedEvent{},
oq.pendingPDUs[len(pendingPDUs):]...,
)
oq.pendingEDUs = append(
[]*gomatrixserverlib.EDU{},
oq.pendingEDUs[len(pendingEDUs):]...,
)
oq.runningMutex.Unlock() oq.runningMutex.Unlock()
} }
} }
@ -226,7 +232,12 @@ func (oq *destinationQueue) backgroundSend() {
// the pending invites. // the pending invites.
if invites { if invites {
oq.runningMutex.Lock() oq.runningMutex.Lock()
oq.pendingInvites = oq.pendingInvites[len(pendingInvites):] // Reallocate so that the underlying array can be GC'd, as
// opposed to growing forever.
oq.pendingInvites = append(
[]*gomatrixserverlib.InviteV2Request{},
oq.pendingInvites[len(pendingInvites):]...,
)
oq.runningMutex.Unlock() oq.runningMutex.Unlock()
} }
} }