2017-06-28 15:10:17 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
2017-09-13 10:03:41 +00:00
|
|
|
"context"
|
2017-06-28 15:10:17 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-11-16 10:12:02 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-06-28 15:10:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// destinationQueue is a queue of events for a single destination.
|
|
|
|
// It is responsible for sending the events to the destination and
|
|
|
|
// ensures that only one request is in flight to a given destination
|
|
|
|
// at a time.
|
|
|
|
type destinationQueue struct {
|
|
|
|
client *gomatrixserverlib.FederationClient
|
|
|
|
origin gomatrixserverlib.ServerName
|
|
|
|
destination gomatrixserverlib.ServerName
|
|
|
|
// The running mutex protects running, sentCounter, lastTransactionIDs and
|
2018-08-10 15:26:57 +00:00
|
|
|
// pendingEvents, pendingEDUs.
|
2017-06-28 15:10:17 +00:00
|
|
|
runningMutex sync.Mutex
|
|
|
|
running bool
|
|
|
|
sentCounter int
|
|
|
|
lastTransactionIDs []gomatrixserverlib.TransactionID
|
|
|
|
pendingEvents []*gomatrixserverlib.Event
|
2018-08-10 15:26:57 +00:00
|
|
|
pendingEDUs []*gomatrixserverlib.EDU
|
2017-06-28 15:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send event adds the event to the pending queue for the destination.
|
|
|
|
// If the queue is empty then it starts a background goroutine to
|
|
|
|
// start sending events to that destination.
|
|
|
|
func (oq *destinationQueue) sendEvent(ev *gomatrixserverlib.Event) {
|
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
|
|
|
oq.pendingEvents = append(oq.pendingEvents, ev)
|
|
|
|
if !oq.running {
|
|
|
|
oq.running = true
|
|
|
|
go oq.backgroundSend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:26:57 +00:00
|
|
|
// sendEDU adds the EDU event to the pending queue for the destination.
|
|
|
|
// If the queue is empty then it starts a background goroutine to
|
|
|
|
// start sending event to that destination.
|
|
|
|
func (oq *destinationQueue) sendEDU(e *gomatrixserverlib.EDU) {
|
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
|
|
|
oq.pendingEDUs = append(oq.pendingEDUs, e)
|
|
|
|
if !oq.running {
|
|
|
|
oq.running = true
|
|
|
|
go oq.backgroundSend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:10:17 +00:00
|
|
|
func (oq *destinationQueue) backgroundSend() {
|
|
|
|
for {
|
|
|
|
t := oq.next()
|
|
|
|
if t == nil {
|
|
|
|
// If the queue is empty then stop processing for this destination.
|
|
|
|
// TODO: Remove this destination from the queue map.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle retries.
|
|
|
|
// TODO: blacklist uncooperative servers.
|
|
|
|
|
2017-09-13 10:03:41 +00:00
|
|
|
_, err := oq.client.SendTransaction(context.TODO(), *t)
|
2017-06-28 15:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"destination": oq.destination,
|
|
|
|
log.ErrorKey: err,
|
|
|
|
}).Info("problem sending transaction")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// next creates a new transaction from the pending event queue
|
|
|
|
// and flushes the queue.
|
|
|
|
// Returns nil if the queue was empty.
|
|
|
|
func (oq *destinationQueue) next() *gomatrixserverlib.Transaction {
|
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
2018-08-10 15:26:57 +00:00
|
|
|
|
|
|
|
if len(oq.pendingEvents) == 0 && len(oq.pendingEDUs) == 0 {
|
2017-06-28 15:10:17 +00:00
|
|
|
oq.running = false
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-10 15:26:57 +00:00
|
|
|
|
2020-02-28 14:54:51 +00:00
|
|
|
t := gomatrixserverlib.Transaction{
|
|
|
|
PDUs: []gomatrixserverlib.Event{},
|
|
|
|
EDUs: []gomatrixserverlib.EDU{},
|
|
|
|
}
|
2017-06-28 15:10:17 +00:00
|
|
|
now := gomatrixserverlib.AsTimestamp(time.Now())
|
|
|
|
t.TransactionID = gomatrixserverlib.TransactionID(fmt.Sprintf("%d-%d", now, oq.sentCounter))
|
|
|
|
t.Origin = oq.origin
|
|
|
|
t.Destination = oq.destination
|
|
|
|
t.OriginServerTS = now
|
|
|
|
t.PreviousIDs = oq.lastTransactionIDs
|
|
|
|
if t.PreviousIDs == nil {
|
|
|
|
t.PreviousIDs = []gomatrixserverlib.TransactionID{}
|
|
|
|
}
|
2018-08-10 15:26:57 +00:00
|
|
|
|
2017-06-28 15:10:17 +00:00
|
|
|
oq.lastTransactionIDs = []gomatrixserverlib.TransactionID{t.TransactionID}
|
2018-08-10 15:26:57 +00:00
|
|
|
|
2017-06-28 15:10:17 +00:00
|
|
|
for _, pdu := range oq.pendingEvents {
|
|
|
|
t.PDUs = append(t.PDUs, *pdu)
|
|
|
|
}
|
|
|
|
oq.pendingEvents = nil
|
|
|
|
oq.sentCounter += len(t.PDUs)
|
2018-08-10 15:26:57 +00:00
|
|
|
|
|
|
|
for _, edu := range oq.pendingEDUs {
|
|
|
|
t.EDUs = append(t.EDUs, *edu)
|
|
|
|
}
|
|
|
|
oq.pendingEDUs = nil
|
|
|
|
oq.sentCounter += len(t.EDUs)
|
|
|
|
|
2017-06-28 15:10:17 +00:00
|
|
|
return &t
|
|
|
|
}
|