mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-21 05:43:09 -06:00
Thread safety on transaction ID/count
This commit is contained in:
parent
d6c94064af
commit
ea1e8397fc
|
|
@ -38,25 +38,25 @@ const maxPDUsPerTransaction = 50
|
||||||
// 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 {
|
||||||
db storage.Database
|
db storage.Database
|
||||||
signing *SigningInfo
|
signing *SigningInfo
|
||||||
rsAPI api.RoomserverInternalAPI
|
rsAPI api.RoomserverInternalAPI
|
||||||
client *gomatrixserverlib.FederationClient // federation client
|
client *gomatrixserverlib.FederationClient // federation client
|
||||||
origin gomatrixserverlib.ServerName // origin of requests
|
origin gomatrixserverlib.ServerName // origin of requests
|
||||||
destination gomatrixserverlib.ServerName // destination of requests
|
destination gomatrixserverlib.ServerName // destination of requests
|
||||||
running atomic.Bool // is the queue worker running?
|
running atomic.Bool // is the queue worker running?
|
||||||
backingOff atomic.Bool // true if we're backing off
|
backingOff atomic.Bool // true if we're backing off
|
||||||
statistics *types.ServerStatistics // statistics about this remote server
|
statistics *types.ServerStatistics // statistics about this remote server
|
||||||
incomingInvites chan *gomatrixserverlib.InviteV2Request // invites to send
|
incomingInvites chan *gomatrixserverlib.InviteV2Request // invites to send
|
||||||
incomingEDUs chan *gomatrixserverlib.EDU // EDUs to send
|
incomingEDUs chan *gomatrixserverlib.EDU // EDUs to send
|
||||||
transactionMutex sync.Mutex // protects transactionID and transactionCount
|
transactionIDMutex sync.Mutex // protects transactionID
|
||||||
transactionID gomatrixserverlib.TransactionID // last transaction ID
|
transactionID gomatrixserverlib.TransactionID // last transaction ID
|
||||||
transactionCount int // how many events in this transaction so far
|
transactionCount atomic.Int32 // how many events in this transaction so far
|
||||||
pendingPDUs atomic.Int32 // how many PDUs are waiting to be sent
|
pendingPDUs atomic.Int32 // how many PDUs are waiting to be sent
|
||||||
pendingEDUs []*gomatrixserverlib.EDU // owned by backgroundSend
|
pendingEDUs []*gomatrixserverlib.EDU // owned by backgroundSend
|
||||||
pendingInvites []*gomatrixserverlib.InviteV2Request // owned by backgroundSend
|
pendingInvites []*gomatrixserverlib.InviteV2Request // owned by backgroundSend
|
||||||
wakeServerCh chan bool // interrupts idle wait
|
wakeServerCh chan bool // interrupts idle wait
|
||||||
retryServerCh chan bool // interrupts backoff
|
retryServerCh chan bool // interrupts backoff
|
||||||
}
|
}
|
||||||
|
|
||||||
// retry will clear the blacklist state and attempt to send built up events to the server,
|
// retry will clear the blacklist state and attempt to send built up events to the server,
|
||||||
|
|
@ -95,13 +95,13 @@ func (oq *destinationQueue) sendEvent(nid int64) {
|
||||||
// one made up yet, or if we've exceeded the number of maximum
|
// one made up yet, or if we've exceeded the number of maximum
|
||||||
// events allowed in a single tranaction. We'll reset the counter
|
// events allowed in a single tranaction. We'll reset the counter
|
||||||
// when we do.
|
// when we do.
|
||||||
oq.transactionMutex.Lock()
|
oq.transactionIDMutex.Lock()
|
||||||
if oq.transactionID == "" || oq.transactionCount >= maxPDUsPerTransaction {
|
if oq.transactionID == "" || oq.transactionCount.Load() >= maxPDUsPerTransaction {
|
||||||
now := gomatrixserverlib.AsTimestamp(time.Now())
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
||||||
oq.transactionID = gomatrixserverlib.TransactionID(fmt.Sprintf("%d-%d", now, oq.statistics.SuccessCount()))
|
oq.transactionID = gomatrixserverlib.TransactionID(fmt.Sprintf("%d-%d", now, oq.statistics.SuccessCount()))
|
||||||
oq.transactionCount = 0
|
oq.transactionCount.Store(0)
|
||||||
}
|
}
|
||||||
oq.transactionMutex.Unlock()
|
oq.transactionIDMutex.Unlock()
|
||||||
// Create a database entry that associates the given PDU NID with
|
// Create a database entry that associates the given PDU NID with
|
||||||
// this destination queue. We'll then be able to retrieve the PDU
|
// this destination queue. We'll then be able to retrieve the PDU
|
||||||
// later.
|
// later.
|
||||||
|
|
@ -116,7 +116,7 @@ func (oq *destinationQueue) sendEvent(nid int64) {
|
||||||
}
|
}
|
||||||
// We've successfully added a PDU to the transaction so increase
|
// We've successfully added a PDU to the transaction so increase
|
||||||
// the counter.
|
// the counter.
|
||||||
oq.transactionCount++
|
oq.transactionCount.Add(1)
|
||||||
// If the queue isn't running at this point then start it.
|
// If the queue isn't running at this point then start it.
|
||||||
if !oq.running.Load() {
|
if !oq.running.Load() {
|
||||||
go oq.backgroundSend()
|
go oq.backgroundSend()
|
||||||
|
|
@ -300,10 +300,10 @@ func (oq *destinationQueue) nextTransaction(
|
||||||
// Otherwise it's possible that we'll pick up an incomplete
|
// Otherwise it's possible that we'll pick up an incomplete
|
||||||
// transaction and end up nuking the rest of the events at the
|
// transaction and end up nuking the rest of the events at the
|
||||||
// cleanup stage.
|
// cleanup stage.
|
||||||
oq.transactionMutex.Lock()
|
oq.transactionIDMutex.Lock()
|
||||||
oq.transactionID = ""
|
oq.transactionID = ""
|
||||||
oq.transactionCount = 0
|
oq.transactionIDMutex.Unlock()
|
||||||
oq.transactionMutex.Unlock()
|
oq.transactionCount.Store(0)
|
||||||
|
|
||||||
// Create the transaction.
|
// Create the transaction.
|
||||||
t := gomatrixserverlib.Transaction{
|
t := gomatrixserverlib.Transaction{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue