gossip

package module
v0.0.0-...-074130e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2022 License: MIT Imports: 15 Imported by: 0

README

gossip Go Reference Go Report Card Build Status

A simple gossip protocol implementation in < 200 lines of Go.

The fanout factor 'k' is defaulted to 13, optimised for large networks > 10000 nodes. You should try and reduce this to a more reasonable value if your network is smaller.

Usage

func main() {
    // some other gossip nodes on the network
    nodes := make([]*net.UDPAddr, 100)

    for i := range nodes {
        // parse the node address and add it to the list
        nodes[i], _ = net.ResolveUDPAddr("udp", fmt.Sprintf("10.64.0.%d:10000", 101+i))
    }

    cfg := &gossip.Config{
        Fanout: gossip.DefaultFanout,         // number of nodes to forward gossip messages to (default: 13)
        Nodes: gossip.DefaultNodeList(nodes), // specifies a node list that random nodes can be selected from
        ListenAddress: "10.64.0.100:10000",   // udp address to bind to
        OnGossip: func(message []byte) {
            // callback to handle gossip messages
            // ensure that the message value is not used outside of this callback!
            m := make([]byte, len(message))
            copy(m, message)

            // do something with m...
        },
    }

    network, err := gossip.New(cfg)
    if err != nil {
        log.Fatal(err)
    }

    log.Println("gossip node started!")

    // send a message to the network
    err = network.Gossip([]byte("hello!"))
    if err != nil {
        log.Fatal(err)
    }
}

Documentation

Index

Constants

View Source
const DefaultFanout = 13

DefaultFanout the default fanout factor 'k'

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ListenAddress UDP Address to listen on
	ListenAddress string
	// Fanout defines how many nodes a message should be rebroadcasted to
	Fanout int
	// Nodes defines the neighbourhood that random nodes can be selected from for rebroadcast
	Nodes NodeList
	// OnGossip callback for when new gossip is received
	OnGossip func(message []byte)
}

Config defines configuration for the gossip network

type Network

type Network struct {
	// contains filtered or unexported fields
}

Network defines the network that can be gossiped to/from

func New

func New(cfg *Config) (*Network, error)

New creates a new listener on the gossip network

func (*Network) Gossip

func (n *Network) Gossip(message []byte) error

Gossip gossip a network to the

type NodeList

type NodeList interface {
	GetRandom(count int) []*net.UDPAddr
}

NodeList represents a collection of nodes on the network

func DefaultNodeList

func DefaultNodeList(nodes []*net.UDPAddr) NodeList

DefaultNodeList returns a default nodelist implementation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL