code.vegaprotocol.io/vega@v0.79.0/datanode/docs/broker.md (about) 1 # Event bus 2 3 This document provides some information regarding the design and use of the event bus as it currently stands 4 5 ## Broker 6 7 The broker package defines a generic event broker. This is the component to which events are passed to be passed on to the subscribers/consumers. There is just a single `Send()` function. Any registered subscriber can receive the event that was sent through this function. Whether or not they will receive the event depends on the subscriber configuration. 8 9 Currently data node version of broker is listening on TCP socket where it can receive events from core Vega. Every event received by socket is then internally dispatch by `Send()` function mentioned above. 10 11 Subscribers are registered (and can be removed) using the `Subscribe` and `Unsubscribe` methods: 12 13 * `Subscribe(s Subscriber, required bool) int`: This method takes a Subscriber (interface defined in the package), a boolean flag indicating whether or not this subscriber _requires_ all events. The method returns the subscriber `ID` (`int`). 14 * `Unsubscribe(key int)`: this method removes a subscriber by ID. The ID can be reused from that point on. If the subscriber has already been removed, this call is a `noop`. 15 16 The broker will check the its context every time it tries to send an event to a subscriber. 17 18 ## Subscribers 19 20 Subscribers implement an interface with 4 methods: 21 22 * `Push(interface{})`: This method is called on _required_ subscribers. The call is treated as a blocking call, pushing a single event to the subscriber. 23 * `C() chan<- interface{}`: Similar to the `Push` method, only the broker _attempts_ to push the event onto the subscriber channel, but if the channel is not being read from, or its buffer is full, the event is skipped instead. 24 * `Skip() <-chan struct{}`: This method returns a channel that the broker checks to see if the subscriber is in a suspended state. As long as this function returns a closed channel (or the broker can read from this channel), the subscriber won't receive any new events, but the subscriber remains registered. 25 * `Closed() <-chan struct{}`: This works in the same way as `Skip()`, but if the subscriber is marked as closed, the subscriber is automatically unregistered from the broker. 26