cluster

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

README

Cluster

Allows communication and synchronization between cluster nodes

Install

To install the library

$ go get github.com/najibulloShapoatov/server-core/cluster

Usage example


c, err := cluster.Join("cluster-name")

// Listen for cluster messages
c.OnMessage(func(msg *cluster.Message) {
   // handle message from other nodes
})

// send a message to all nodes in the cluster
c.Broadcast(message)

// Obtain a mutex lock in the cluster
err := c.Lock("lock-name")
if err != nil {
    // Mutex is already lock by another node
}

// ...
// Perform action
// ...

// release lock 
c.Unlock("lock-name")

// Leave the cluster
c.Leave()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

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

func Join

func Join(name string) (cluster *Cluster, err error)

Join a cluster by name

func (*Cluster) Broadcast

func (c *Cluster) Broadcast(payload interface{}) (err error)

Send a message to all other nodes

func (*Cluster) ID

func (c *Cluster) ID() int

func (*Cluster) Leave

func (c *Cluster) Leave() (err error)

Leave a cluster

func (*Cluster) Lock

func (c *Cluster) Lock(name string) error

func (*Cluster) OnMessage

func (c *Cluster) OnMessage(callback MessageHandler)

Register a callback to handle cluster messages

func (*Cluster) Unlock

func (c *Cluster) Unlock(name string) error

type Message

type Message struct {
	Type   messageType     `json:"type"`
	NodeID int             `json:"nodeID"`
	Data   json.RawMessage `json:"data"`
}

func (Message) Bool

func (m Message) Bool() (res bool, err error)

func (Message) Float

func (m Message) Float() (res float64, err error)

func (Message) Int

func (m Message) Int() (res int, err error)

func (Message) MarshalBinary

func (m Message) MarshalBinary() (data []byte, err error)

func (Message) String

func (m Message) String() (res string, err error)

func (*Message) UnmarshalBinary

func (m *Message) UnmarshalBinary(data []byte) error

func (Message) Unpack

func (m Message) Unpack(ptr interface{}) (err error)

type MessageHandler

type MessageHandler func(*Message)

Jump to

Keyboard shortcuts

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