bridge.go 5.9 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
// 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 bridge

import (
	"context"
	"encoding/gob"
	"encoding/json"
	"errors"
	"sync"
	"time"

	"github.com/finogeeks/ligase/common"
	"github.com/finogeeks/ligase/common/config"
	"github.com/finogeeks/ligase/model/service/roomserverapi"

	"github.com/finogeeks/ligase/core"
	"github.com/finogeeks/ligase/plugins/message/external"
	"github.com/finogeeks/ligase/skunkworks/gomatrixserverlib"
	"github.com/finogeeks/ligase/skunkworks/log"
	"github.com/finogeeks/ligase/skunkworks/util/id"

	// "github.com/finogeeks/ligase/skunkworks/log"
	"github.com/finogeeks/ligase/model"
	wp "github.com/finogeeks/ligase/skunkworks/util/workerpool"
	pool "github.com/jolestar/go-commons-pool"
)

func init() {
	// must register the encode type first
	gob.Register(model.TxnReq{})
	gob.Register(map[string]string{})
	// gob.Register(api.GetAliasRoomIDResponse{})
	gob.Register(&roomserverapi.GetAliasRoomIDResponse{})
	gob.Register(&external.GetProfileResponse{})
	gob.Register(&external.GetAvatarURLResponse{})
	gob.Register(&external.GetDisplayNameResponse{})
	gob.Register(&gomatrixserverlib.FederationRequest{})
	gob.Register(&gomatrixserverlib.Transaction{})
	gob.Register(&roomserverapi.FederationEvent{})
	gob.Register(&roomserverapi.RawEvent{})
	gob.Register(&external.GetFedBackFillRequest{})
	gob.Register(&roomserverapi.QueryBackFillEventsResponse{})
}

var bridge Bridge

func newReplyChan(context.Context) (interface{}, error) {
	return make(chan *model.GobMessage), nil
}

type Bridge struct {
	w         *wp.WorkerPool
	replyChan sync.Map
	objPool   *pool.ObjectPool
	ctx       context.Context
	cfg       *config.Dendrite
}

// handle request and response
func (b *Bridge) processRequest(gobMsg *model.GobMessage) error {
	prod := b.cfg.Kafka.Producer.FedBridgeOutHs
	if gobMsg.Cmd >= model.CMD_FED {
		prod = b.cfg.Kafka.Producer.FedBridgeOut
	}
	// subject := topic //fmt.Sprintf("%s.%d", topic, id.GetNodeId())
	// log.Infof("connector pub addr:%s subject: %s data:%v", nc.addrs, subject, gobMsg)
	// return nc.econn.Publish(subject, gobMsg)

	keys := gobMsg.Key
	if gobMsg.Key == nil {
		keys = []byte{}
	}

	return common.GetTransportMultiplexer().SendWithRetry(
		prod.Underlying,
		prod.Name,
		&core.TransportPubMsg{
			//Format: core.FORMAT_GOB,
			Keys: keys,
			Obj:  gobMsg,
		})
}

func (b *Bridge) processReply(gobMsg *model.GobMessage) error {
	ch, ok := b.replyChan.Load(gobMsg.MsgSeq)
	if !ok {
		return errors.New("Bridge reply MsgSeq not found in map. " + gobMsg.MsgSeq)
	}
	ch.(chan *model.GobMessage) <- gobMsg
	return nil
}

// 推送等单向消息
func (b *Bridge) processInstantMessage(gobMsg *model.GobMessage) error {
	return nil
}

func (b *Bridge) process(payload model.Payload) error {
	msg := payload.(model.GobMessage)
	// fmt.Printf("process, msg type: %d, msg: %v\n", msg.MsgType, msg)
	switch msg.MsgType {
	case model.REQUEST:
		b.processRequest(&msg)
	case model.REPLY:
		b.processReply(&msg)
	case model.MESSAGE:
		b.processInstantMessage(&msg)
	}
	return nil
}

func (b *Bridge) SetupBridge(cfg *config.Dendrite) {
	b.cfg = cfg

	factory := pool.NewPooledObjectFactorySimple(newReplyChan)
	b.ctx = context.Background()
	b.objPool = pool.NewObjectPoolWithDefaultConfig(b.ctx, factory)

	b.w = wp.NewWorkerPool(10)
	// defer w.Stop()
	b.w.SetHandler(b.process).Run()

	val, ok := common.GetTransportMultiplexer().GetChannel(
		b.cfg.Kafka.Consumer.FedBridgeOutRes.Underlying,
		b.cfg.Kafka.Consumer.FedBridgeOutRes.Name,
	)
	if ok {
		channel := val.(core.IChannel)
		channel.SetHandler(b)
		channel.Start()
	}
}

func (b *Bridge) OnMessage(ctx context.Context, topic string, partition int32, data []byte, rawMsg interface{}) {
	//dec := gob.NewDecoder(bytes.NewReader(data))
	msg := &model.GobMessage{}
	//err := dec.Decode(msg)
	err := json.Unmarshal(data, msg)
	if err != nil {
		log.Errorf("decode error: %s", err.Error())
		return
	}

	log.Infof("fed bridge response recv msg, MsgType:%d , MsgSeq:%s, NodeId:%d cmd:%d Body:%s", msg.MsgType, msg.MsgSeq, msg.NodeId, msg.Cmd, msg.Body)
	// msg := payload.(*model.GobMessage)
	msg.MsgType = model.REPLY
	b.w.FeedPayload(*msg)
}

func (b *Bridge) SendAndRecv(msg model.GobMessage, timeout int) (reply *model.GobMessage, err error) {
	// send msg
	Send(msg)

	// recv msg
	var obj interface{}
	if replyCh, ok := b.replyChan.Load(msg.MsgSeq); !ok {
		obj, err = b.objPool.BorrowObject(b.ctx)
		if err == nil && obj != nil {
			actual, loaded := b.replyChan.LoadOrStore(msg.MsgSeq, obj)
			if loaded {
				log.Errorf("Bridge reply channel exists, %s", msg.MsgSeq)
				b.objPool.ReturnObject(b.ctx, obj)
				obj = actual
			}
		} else {
			return nil, errors.New("server memory alloc failed")
		}
	} else {
		log.Errorf("Bridge reply channel exists, %s", msg.MsgSeq)
		obj = replyCh
	}
	ch := obj.(chan *model.GobMessage)

	select {
	case <-time.After(time.Millisecond * time.Duration(timeout)):
		err = model.ErrRequestTimedOut
	case m := <-ch:
		reply = m
	}

	b.replyChan.Delete(msg.MsgSeq)
	b.objPool.ReturnObject(b.ctx, obj)

	return
}

func (b *Bridge) Send(msg model.GobMessage) {
	msg.MsgType = model.REQUEST
	msg.NodeId = id.GetNodeId()
	b.w.FeedPayload(msg)
}

func SetupBridge(cfg *config.Dendrite) {
	bridge.SetupBridge(cfg)
}

func SendAndRecv(msg model.GobMessage, timeout int) (reply *model.GobMessage, err error) {
	return bridge.SendAndRecv(msg, timeout)
}

func Send(msg model.GobMessage) {
	bridge.Send(msg)
}