2017-06-28 17:10:17 +02: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 12:03:41 +02:00
|
|
|
"context"
|
2020-03-27 17:28:22 +01:00
|
|
|
"encoding/json"
|
2017-06-28 17:10:17 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-04-28 11:53:07 +02:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/producers"
|
2017-06-28 17:10:17 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-03-27 17:28:22 +01:00
|
|
|
"github.com/matrix-org/util"
|
2017-11-16 11:12:02 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-04-03 15:29:06 +02:00
|
|
|
"go.uber.org/atomic"
|
2017-06-28 17:10:17 +02: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 {
|
2020-04-28 11:53:07 +02:00
|
|
|
rsProducer *producers.RoomserverProducer
|
2017-06-28 17:10:17 +02:00
|
|
|
client *gomatrixserverlib.FederationClient
|
|
|
|
origin gomatrixserverlib.ServerName
|
|
|
|
destination gomatrixserverlib.ServerName
|
2020-04-03 15:29:06 +02:00
|
|
|
running atomic.Bool
|
|
|
|
// The running mutex protects sentCounter, lastTransactionIDs and
|
2018-08-10 17:26:57 +02:00
|
|
|
// pendingEvents, pendingEDUs.
|
2017-06-28 17:10:17 +02:00
|
|
|
runningMutex sync.Mutex
|
|
|
|
sentCounter int
|
|
|
|
lastTransactionIDs []gomatrixserverlib.TransactionID
|
2020-03-27 17:28:22 +01:00
|
|
|
pendingEvents []*gomatrixserverlib.HeaderedEvent
|
2018-08-10 17:26:57 +02:00
|
|
|
pendingEDUs []*gomatrixserverlib.EDU
|
2020-04-03 15:29:06 +02:00
|
|
|
pendingInvites []*gomatrixserverlib.InviteV2Request
|
2017-06-28 17:10:17 +02: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.
|
2020-03-27 17:28:22 +01:00
|
|
|
func (oq *destinationQueue) sendEvent(ev *gomatrixserverlib.HeaderedEvent) {
|
2017-06-28 17:10:17 +02:00
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
|
|
|
oq.pendingEvents = append(oq.pendingEvents, ev)
|
2020-04-03 15:29:06 +02:00
|
|
|
if !oq.running.Load() {
|
2017-06-28 17:10:17 +02:00
|
|
|
go oq.backgroundSend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:26:57 +02: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
|
2020-04-03 15:29:06 +02:00
|
|
|
// start sending events to that destination.
|
2018-08-10 17:26:57 +02:00
|
|
|
func (oq *destinationQueue) sendEDU(e *gomatrixserverlib.EDU) {
|
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
|
|
|
oq.pendingEDUs = append(oq.pendingEDUs, e)
|
2020-04-03 15:29:06 +02:00
|
|
|
if !oq.running.Load() {
|
|
|
|
go oq.backgroundSend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendInvite adds the invite 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) sendInvite(ev *gomatrixserverlib.InviteV2Request) {
|
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
|
|
|
oq.pendingInvites = append(oq.pendingInvites, ev)
|
|
|
|
if !oq.running.Load() {
|
2018-08-10 17:26:57 +02:00
|
|
|
go oq.backgroundSend()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 15:29:06 +02:00
|
|
|
// backgroundSend is the worker goroutine for sending events.
|
2017-06-28 17:10:17 +02:00
|
|
|
func (oq *destinationQueue) backgroundSend() {
|
2020-04-03 15:29:06 +02:00
|
|
|
oq.running.Store(true)
|
|
|
|
defer oq.running.Store(false)
|
|
|
|
|
2017-06-28 17:10:17 +02:00
|
|
|
for {
|
2020-04-03 15:29:06 +02:00
|
|
|
transaction, invites := oq.nextTransaction(), oq.nextInvites()
|
|
|
|
if !transaction && !invites {
|
2017-06-28 17:10:17 +02:00
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 15:29:06 +02:00
|
|
|
// nextTransaction creates a new transaction from the pending event
|
|
|
|
// queue and sends it. Returns true if a transaction was sent or
|
|
|
|
// false otherwise.
|
|
|
|
func (oq *destinationQueue) nextTransaction() bool {
|
2017-06-28 17:10:17 +02:00
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
2018-08-10 17:26:57 +02:00
|
|
|
|
|
|
|
if len(oq.pendingEvents) == 0 && len(oq.pendingEDUs) == 0 {
|
2020-04-03 15:29:06 +02:00
|
|
|
return false
|
2017-06-28 17:10:17 +02:00
|
|
|
}
|
2018-08-10 17:26:57 +02:00
|
|
|
|
2020-02-28 15:54:51 +01:00
|
|
|
t := gomatrixserverlib.Transaction{
|
2020-03-27 17:28:22 +01:00
|
|
|
PDUs: []json.RawMessage{},
|
2020-02-28 15:54:51 +01:00
|
|
|
EDUs: []gomatrixserverlib.EDU{},
|
|
|
|
}
|
2017-06-28 17:10:17 +02: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 17:26:57 +02:00
|
|
|
|
2017-06-28 17:10:17 +02:00
|
|
|
oq.lastTransactionIDs = []gomatrixserverlib.TransactionID{t.TransactionID}
|
2018-08-10 17:26:57 +02:00
|
|
|
|
2017-06-28 17:10:17 +02:00
|
|
|
for _, pdu := range oq.pendingEvents {
|
2020-03-27 17:28:22 +01:00
|
|
|
// Append the JSON of the event, since this is a json.RawMessage type in the
|
|
|
|
// gomatrixserverlib.Transaction struct
|
|
|
|
t.PDUs = append(t.PDUs, (*pdu).JSON())
|
2017-06-28 17:10:17 +02:00
|
|
|
}
|
|
|
|
oq.pendingEvents = nil
|
|
|
|
oq.sentCounter += len(t.PDUs)
|
2018-08-10 17:26:57 +02:00
|
|
|
|
|
|
|
for _, edu := range oq.pendingEDUs {
|
|
|
|
t.EDUs = append(t.EDUs, *edu)
|
|
|
|
}
|
|
|
|
oq.pendingEDUs = nil
|
|
|
|
oq.sentCounter += len(t.EDUs)
|
|
|
|
|
2020-04-03 15:29:06 +02:00
|
|
|
util.GetLogger(context.TODO()).Infof("Sending transaction %q containing %d PDUs, %d EDUs", t.TransactionID, len(t.PDUs), len(t.EDUs))
|
|
|
|
|
|
|
|
_, err := oq.client.SendTransaction(context.TODO(), t)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"destination": oq.destination,
|
|
|
|
log.ErrorKey: err,
|
|
|
|
}).Info("problem sending transaction")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextInvite takes pending invite events from the queue and sends
|
|
|
|
// them. Returns true if a transaction was sent or false otherwise.
|
|
|
|
func (oq *destinationQueue) nextInvites() bool {
|
|
|
|
oq.runningMutex.Lock()
|
|
|
|
defer oq.runningMutex.Unlock()
|
|
|
|
|
|
|
|
if len(oq.pendingInvites) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, inviteReq := range oq.pendingInvites {
|
2020-04-28 11:53:07 +02:00
|
|
|
ev, roomVersion := inviteReq.Event(), inviteReq.RoomVersion()
|
2020-04-03 15:29:06 +02:00
|
|
|
|
2020-04-28 11:53:07 +02:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"room_version": roomVersion,
|
|
|
|
"destination": oq.destination,
|
|
|
|
}).Info("sending invite")
|
|
|
|
|
|
|
|
inviteRes, err := oq.client.SendInviteV2(
|
2020-04-03 15:29:06 +02:00
|
|
|
context.TODO(),
|
|
|
|
oq.destination,
|
|
|
|
*inviteReq,
|
2020-04-28 11:53:07 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-04-03 15:29:06 +02:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"state_key": ev.StateKey(),
|
|
|
|
"destination": oq.destination,
|
|
|
|
}).WithError(err).Error("failed to send invite")
|
2020-04-28 11:53:07 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = oq.rsProducer.SendInviteResponse(
|
|
|
|
context.TODO(),
|
|
|
|
inviteRes,
|
|
|
|
roomVersion,
|
|
|
|
); err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"event_id": ev.EventID(),
|
|
|
|
"state_key": ev.StateKey(),
|
|
|
|
"destination": oq.destination,
|
|
|
|
}).WithError(err).Error("failed to return signed invite to roomserver")
|
2020-04-03 15:29:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oq.pendingInvites = nil
|
|
|
|
|
|
|
|
return true
|
2017-06-28 17:10:17 +02:00
|
|
|
}
|