mercredi 3 juin 2020

Is this a message bus based pub-sub pattern?

As per this answer,

A Message Bus is a messaging infrastructure to allow different systems to communicate through a shared set of interfaces(message bus).

https://i.stack.imgur.com/5PkJy.gif


Below is the createHub() function & Run() method launched by main() to create message hub to communicate a publisher with multiple subscribers:

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

type HandlerSubscription struct {
    ConnInstance *websocket.Conn
    DataChannel  chan *events.Envelope
}

Can the above code be considered following message bus based pub-sub pattern?

Aucun commentaire:

Enregistrer un commentaire