[英]Is this a message bus based pub-sub pattern?
A Message Bus is a messaging infrastructure to allow different systems to communicate through a shared set of interfaces(message bus).
消息总线是一种消息基础设施,允许不同的系统通过一组共享的接口(消息总线)进行通信。
Below is the createHub()
function & Run()
method launched by main()
to create message hub to communicate a publisher with multiple subscribers:下面是
main()
() 启动的createHub()
function & Run()
方法,用于创建消息中心以将发布者与多个订阅者进行通信:
type PubHub struct {
subscribers map[*subscriptionmediator.HandlerSubscription]struct{}
Register chan *subscriptionmediator.HandlerSubscription
Unregister chan *subscriptionmediator.HandlerSubscription
Broadcast chan *events.Env
}
func createHub() *PubHub {
return &PubHub{
subscribers: map[*subscriptionmediator.HandlerSubscription]struct{}{},
Register: make(chan *subscriptionmediator.HandlerSubscription),
Unregister: make(chan *subscriptionmediator.HandlerSubscription),
Broadcast: make(chan *events.Envelope),
}
}
func (h *PubHub) Run() {
for {
select {
case subscriber := <-h.Register:
h.subscribers[subscriber] = struct{}{}
case subscriber := <-h.Unregister:
if _, ok := h.subscribers[subscriber]; ok {
delete(h.subscribers, subscriber)
}
case message := <-h.Broadcast:
for subscriber := range h.subscribers {
subscriber.DataChannel <- message
}
}
}
}
where each subscriber registers, as shown below:每个订阅者注册的地方,如下图:
subscription := &subscriptionmediator.HandlerSubscription{
conn,
make(chan *events.Envelope),
}
hub.Register <- subscription
DataChannel
is used for communication between publisher & multiple subscribers DataChannel
用于发布者和多个订阅者之间的通信
type HandlerSubscription struct {
ConnInstance *websocket.Conn
DataChannel chan *events.Envelope
}
1) Can the above code be considered following message bus based pub-sub pattern? 1) 上面的代码可以考虑遵循基于消息总线的发布-订阅模式吗?
2) How to avoid one subscriber blocking rest all subscribers, from signalling on a channel? 2)如何避免一个用户阻塞 rest 所有用户,从一个信道上发信号?
subscriber.DataChannel <- message
Can the above code be considered following message bus based pub-sub pattern?
上面的代码可以考虑遵循基于消息总线的发布-订阅模式吗?
Yes.是的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.