p2p

package
v0.0.0-...-85922c0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PayloadMessageType messageType = iota
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// NodeName of this node. This must be unique in the cluster.
	// If it is not set, set it to the host name of the running machine.
	NodeName string

	// BroadcastTimeout is the amount of time to wait for a broadcast message to be sent
	// to the cluster. If this is not set, a timeout of 5 seconds will be set.
	BroadcastTimeout time.Duration

	// MemberlistConfig is the memberlist configuration that P2P will
	// use to do the underlying membership management and gossip.
	MemberlistConfig *memberlist.Config

	// MessageBuffer is used to control how many messages are buffered.This is
	// used to prevent messages that have already been received from being redelivered.
	// The buffer must be large enough to handle all recent messages.
	MessageBuffer int

	// HandleFunc is a hook function used by the client to process messages and
	// must not be blocked.
	HandleFunc func([]byte) error

	// LogOutput is the location to write logs to. If this is not set,
	// logs will go to stderr.
	LogOutput io.Writer

	// Logger is a custom logger which you provide. If Logger is set, it will use
	// this for the internal logger. If Logger is not set, it will fall back to the
	// behavior for using LogOutput. You cannot specify both LogOutput and Logger
	// at the same time.
	Logger *log.Logger
}

Config is the configuration for creating a P2P-Node instance.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig provides a default p2p node configuration

type LamportClock

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

LamportClock is a thread safe implementation of a lamport clock. It uses efficient atomic operations for all of its functions, falling back to a heavy lock only if there are enough CAS failures.

func (*LamportClock) Increment

func (l *LamportClock) Increment() LamportTime

Increment is used to increment and return the value of the lamport clock

func (*LamportClock) Time

func (l *LamportClock) Time() LamportTime

Time is used to return the current value of the lamport clock

func (*LamportClock) Witness

func (l *LamportClock) Witness(v LamportTime)

Witness is called to update our local clock if necessary after witnessing a clock value received from another process

type LamportTime

type LamportTime uint64

LamportTime is the value of a LamportClock.

type Node

type Node struct {
	Config *Config

	HandleFunc func([]byte) error
	// contains filtered or unexported fields
}

func Create

func Create(conf Config) (*Node, error)

Create creates a new P2P instance

func (*Node) Join

func (n *Node) Join(existing []string) (int, error)

Join joins an existing P2P cluster. Returns the number of nodes successfully contacted. The returned error will be non-nil only in the case that no nodes could be contacted.

func (*Node) Leave

func (n *Node) Leave() error

Leave leaves all nodes,and returns an error if it times out

func (*Node) Members

func (n *Node) Members() []nodeState

Members returns the status information of all members

func (*Node) RegisterHandleFunc

func (n *Node) RegisterHandleFunc(f func([]byte) error)

func (*Node) SendMessage

func (n *Node) SendMessage(msgType messageType, buf []byte)

SendMessage is a method that needs to be called by the client to send messages. messageType is used to distinguish message types

func (*Node) State

func (n *Node) State() nodeStatus

State is the current state of this Node instance.

Jump to

Keyboard shortcuts

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