Documentation ¶
Index ¶
- Constants
- type Bully
- func (b *Bully) Connect(proto string, peers map[string]string)
- func (b *Bully) Coordinator() string
- func (b *Bully) Elect()
- func (b *Bully) Listen(proto, addr string) error
- func (b *Bully) Run(workFunc func())
- func (b *Bully) Send(to, addr string, what int) error
- func (b *Bully) SetCoordinator(ID string)
- type Message
- type Peer
- type PeerMap
- type Peers
Constants ¶
const ( ELECTION = iota OK COORDINATOR CLOSE )
Message Types.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bully ¶
type Bully struct { *net.TCPListener ID string // contains filtered or unexported fields }
Bully is a `struct` representing a single node used by the `Bully Algorithm`.
NOTE: More details about the `Bully algorithm` can be found here https://en.wikipedia.org/wiki/Bully_algorithm .
func NewBully ¶
NewBully returns a new `Bully` or an `error`.
NOTE: All connections to `Peer`s are established during this function.
NOTE: The `proto` value can be one of this list: `tcp`, `tcp4`, `tcp6`.
func (*Bully) Coordinator ¶
Coordinator returns `b.coordinator`.
NOTE: This function is thread-safe.
func (*Bully) Elect ¶
func (b *Bully) Elect()
Elect handles the leader election mechanism of the `Bully algorithm`.
func (*Bully) Listen ¶
Listen makes `b` listens on the address `addr` provided using the protocol `proto` and returns an `error` if something occurs.
func (*Bully) Run ¶
func (b *Bully) Run(workFunc func())
Run launches the two main goroutine. The first one is tied to the execution of `workFunc` while the other one is the `Bully algorithm`.
NOTE: This function is an infinite loop.
func (*Bully) Send ¶
Send sends a `bully.Message` of type `what` to `b.peer[to]` at the address `addr`. If no connection is reachable at `addr` or if `b.peer[to]` does not exist, the function retries five times and returns an `error` if it does not succeed.
func (*Bully) SetCoordinator ¶
SetCoordinator sets `ID` as the new `b.coordinator` if `ID` is greater than `b.coordinator` or equal to `b.ID`.
NOTE: This function is thread-safe.
type Peer ¶
type Peer struct { ID string // contains filtered or unexported fields }
Peer is a `struct` representing a remote `bully.Bully`.
type PeerMap ¶
type PeerMap struct {
// contains filtered or unexported fields
}
PeerMap is a `struct` implementing the `Peers` interface and representing a container of `bully.Peer`s.
func (*PeerMap) Add ¶
Add creates a new `bully.Peer` and adds it to `pm.peers` using `ID` as a key.
NOTE: This function is thread-safe.
func (*PeerMap) Delete ¶
Delete erases the `bully.Peer` corresponding to `ID` from `pm.peers`.
NOTE: This function is thread-safe.
func (*PeerMap) Find ¶
Find returns `true` if `pm.peers[ID]` exists, `false` otherwise.
NOTE: This function is thread-safe.
type Peers ¶
type Peers interface { Add(ID, addr string, fd io.Writer) Delete(ID string) Find(ID string) bool Write(ID string, msg interface{}) error PeerData() []struct { ID string Addr string } }
Peers is an `interface` exposing methods to handle communication with other `bully.Bully`s.
NOTE: This project offers a default implementation of the `Peers` interface that provides basic functions. This will work for the most simple of use cases fo exemples, although I strongly recommend you provide your own, safer implementation while doing real work.