This is an implementation of the Bimodal Multicast Protocol written in GO.
You can synchronize all types of messages: bool, string, int, complex structs, etc.
The Bimodal Multicast Protocol runs in a series of rounds.
At the beginning of each round, every node randomly chooses another node and sends it a digest of its message histories. The message is called gossip message.
The node that receive the gossip message compares the given digest with the messages in its own message buffer.
If the digest differs from its message histories, then it send a message back to the original sender to request the missing messages. This message is called solicitation.
import "github.com/rstefan1/bimodal-multicast/pkg/bmmc"
The host must implement Peer interface:
type Peer interface {
String() string
Send(msg []byte, route string, peerToSend string) error
}
cfg := bmmc.Config{
Host: host,
Callbacks: map[string]func (interface{}, *log.Logger) error {
"custom-callback":
func (msg interface{}, logger *log.Logger) error {
fmt.Println("The message is:", msg)
return nil
},
},
Beta: float64,
Logger: logger,
RoundDuration: time.Second * 5,
BufferSize: 2048,
}
Config | Required | Description |
---|---|---|
Host | Yes | Host of Bimodal Multicast server. Must implement Peer interface. Check the previous step. |
Callback | No | You can define a list of callbacks. A callback is a function that is called every time a message on the server is synchronized. |
Beta | No | The beta factor is used to control the ratio of unicast to multicast traffic that the protocol allows. |
Logger | No | You can define a structured logger. |
RoundDuration | No | The duration of a gossip round. |
BufferSize | Yes | The size of messages buffer. The buffer will also include internal messages (e.g. synchronization of the peer list). When the buffer is full, the oldest message will be removed. |
bmmcServer, err := bmmc.New(cfg)
The server must handle a list of predefined requests. Each of these handlers must read the message body and call a predefined function.
Handler route | Function to be called |
---|---|
bmmc.GossipRoute |
bmmcServer.GossipHandler(body) |
bmmc.SolicitationRoute |
bmmcServer.SolicitationHandler(body) |
bmmc.SynchronizationRoute |
bmmcServer.SynchronizationHandler(body) |
For more details, check the exemples.
# Start the host server
hostServer.Start()
# Start the bimodal multicast server
bmmcServer.Start()
bmmcServer.AddMessage("new-message", "my-callback")
bmmcServer.AddMessage(12345, "another-callback")
bmmcServer.AddMessage(true, bmmc.NOCALLBACK)
bmcServer.GetMessages()
bmmcServer.AddPeer(peerToAdd)
bmmcServer.RemovePeer(peerToRemove)
bmmcServer.Stop()
- using a http server
- using a maelstrom server
I welcome all contributions in the form of new issues for feature requests, bugs or even pull requests.
This project is licensed under Apache 2.0 license. Read the LICENSE file in the top distribution directory for the full license text.