queue.go 4.2 KB
Newer Older
T
tanggen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or  modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package queue

import (
	"context"
	"sync"
	"time"

	"github.com/finogeeks/ligase/common"
	"github.com/finogeeks/ligase/federation/client"
	"github.com/finogeeks/ligase/federation/config"
	fedrepos "github.com/finogeeks/ligase/federation/model/repos"
	"github.com/finogeeks/ligase/model/repos"
	"github.com/finogeeks/ligase/model/service/roomserverapi"
	"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"
)

type PartitionProcessor interface {
	AssignRoomPartition(ctx context.Context, roomID, domain string, retryTime time.Duration, retryInterval time.Duration) (*fedrepos.RecordItem, bool)
	TryAssignRoomPartition(ctx context.Context, roomID, domain string) (*fedrepos.RecordItem, bool)
	UnassignRoomPartition(ctx context.Context, roomID, domain string)
	OnRoomDomainRelease(ctx context.Context, origin, roomID, domain string)
	HasAssgined(ctx context.Context, roomID, domain string) (*fedrepos.RecordItem, bool)
}

// OutgoingQueues is a collection of queues for sending transactions to other
// matrix servers
type OutgoingQueues struct {
	cfg                *config.Fed
	origin             gomatrixserverlib.ServerName
	fedClient          *client.FedClientWrap
	queues             sync.Map
	rpcCli             roomserverapi.RoomserverRPCAPI
	feddomains         *common.FedDomains
	rsRepo             *repos.RoomServerCurStateRepo
	recRepo            *fedrepos.SendRecRepo
	partitionProcessor PartitionProcessor
}

// NewOutgoingQueues makes a new OutgoingQueues
func NewOutgoingQueues(
	origin gomatrixserverlib.ServerName,
	fedClient *client.FedClientWrap,
	rpcCli roomserverapi.RoomserverRPCAPI,
	cfg *config.Fed,
	feddomains *common.FedDomains,
	rsRepo *repos.RoomServerCurStateRepo,
	recRepo *fedrepos.SendRecRepo,
	partitionProcessor PartitionProcessor,
) *OutgoingQueues {
	return &OutgoingQueues{
		cfg:                cfg,
		origin:             origin,
		fedClient:          fedClient,
		rpcCli:             rpcCli,
		feddomains:         feddomains,
		rsRepo:             rsRepo,
		recRepo:            recRepo,
		partitionProcessor: partitionProcessor,
	}
}

func (oqs *OutgoingQueues) getDestinationQueue(roomID, domain string) *destinationQueue {
	var oq *destinationQueue
	key := roomID + ":" + domain
	val, ok := oqs.queues.Load(key)
	if !ok {
		val, _ = oqs.queues.LoadOrStore(key, &destinationQueue{
			origin:             oqs.origin,
			roomID:             roomID,
			domain:             domain,
			feddomains:         oqs.feddomains,
			fedClient:          oqs.fedClient,
			rpcCli:             oqs.rpcCli,
			rsRepo:             oqs.rsRepo,
			recRepo:            oqs.recRepo,
			partitionProcessor: oqs.partitionProcessor,
		})
	}
	oq = val.(*destinationQueue)
	return oq
}

// SendEvent sends an event to the destinations
func (oqs *OutgoingQueues) SendEvent(
	ctx context.Context,
	partition int32,
	ev *gomatrixserverlib.Event,
	domain string,
	roomID string,
) error {
	if ev == nil {
		return nil
	}

	oq := oqs.getDestinationQueue(roomID, domain)
	oq.SendEvent(ctx, partition, ev)

	return nil
}

// SendEDU sends an EDU event to the destinations
func (oqs *OutgoingQueues) SendEDU(
	ctx context.Context,
	partition int32,
	e *gomatrixserverlib.EDU,
	domain string,
	roomID string,
) error {
	if e == nil {
		return nil
	}

	oq := oqs.getDestinationQueue(roomID, domain)
	oq.SendEDU(ctx, partition, e)

	return nil
}

func (oqs *OutgoingQueues) RetrySend(ctx context.Context, roomID, domain string) {
	oq := oqs.getDestinationQueue(roomID, domain)
	oq.RetrySend(ctx)
}

func (oqs *OutgoingQueues) Release(ctx context.Context, roomID, domain string) {
	oqs.queues.Delete(roomID + ":" + domain)
}