bully-algorithm: github.com/TimTosi/bully-algorithm Index | Files

package bully

import "github.com/TimTosi/bully-algorithm"

Index

Package Files

bully.go messages.go peer.go peers.go

Constants

const (
    ELECTION = iota
    OK
    COORDINATOR
    CLOSE
)

Message Types.

type Bully Uses

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 Uses

func NewBully(ID, addr, proto string, peers map[string]string) (*Bully, error)

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) Connect Uses

func (b *Bully) Connect(proto string, peers map[string]string)

Connect performs a connection to the remote `Peer`s.

func (*Bully) Coordinator Uses

func (b *Bully) Coordinator() string

Coordinator returns `b.coordinator`.

NOTE: This function is thread-safe.

func (*Bully) Elect Uses

func (b *Bully) Elect()

Elect handles the leader election mechanism of the `Bully algorithm`.

func (*Bully) Listen Uses

func (b *Bully) Listen(proto, addr string) error

Listen makes `b` listens on the address `addr` provided using the protocol `proto` and returns an `error` if something occurs.

func (*Bully) Run Uses

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 Uses

func (b *Bully) Send(to, addr string, what int) error

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 Uses

func (b *Bully) SetCoordinator(ID string)

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 Message Uses

type Message struct {
    PeerID string
    Addr   string
    Type   int
}

Message is a `struct` used for communication between `bully.Bully`s.

type Peer Uses

type Peer struct {
    ID string
    // contains filtered or unexported fields
}

Peer is a `struct` representing a remote `bully.Bully`.

func NewPeer Uses

func NewPeer(ID, addr string, fd io.Writer) *Peer

NewPeer returns a new `*bully.Peer`.

type PeerMap Uses

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 NewPeerMap Uses

func NewPeerMap() *PeerMap

NewPeerMap returns a new `bully.PeerMap`.

func (*PeerMap) Add Uses

func (pm *PeerMap) Add(ID, addr string, fd io.Writer)

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 Uses

func (pm *PeerMap) Delete(ID string)

Delete erases the `bully.Peer` corresponding to `ID` from `pm.peers`.

NOTE: This function is thread-safe.

func (*PeerMap) Find Uses

func (pm *PeerMap) Find(ID string) bool

Find returns `true` if `pm.peers[ID]` exists, `false` otherwise.

NOTE: This function is thread-safe.

func (*PeerMap) PeerData Uses

func (pm *PeerMap) PeerData() []struct {
    ID   string
    Addr string
}

PeerData returns a slice of anonymous structures representing a tupple composed of a `Peer.ID` and `Peer.addr`.

NOTE: This function is thread-safe.

func (*PeerMap) Write Uses

func (pm *PeerMap) Write(ID string, msg interface{}) error

Write writes `msg` to `pm.peers[ID]`. It returns `nil` or an `error` if something occurs.

NOTE: This function is thread-safe.

type Peers Uses

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.

Package bully imports 7 packages (graph). Updated 2019-03-25. Refresh now. Tools for package owners.