leaderelection

package module
v0.0.0-...-3407041 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: BSD-3-Clause Imports: 16 Imported by: 0

README

[WIP] leaderelection

A leader election package for go project. It bases on raft

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomTimeout

func RandomTimeout(minVal time.Duration) <-chan time.Time

RandomTimeout returns a channel that will fire after a random duration. The duration is between minVal and 2*minVal.

Types

type HTTP2Transport

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

HTTP2Transport implements Transport with http2.

func NewHTTP2Transport

func NewHTTP2Transport(addr string) *HTTP2Transport

NewHTTP2Transport returns a new HTTP2Transport instance.

func (*HTTP2Transport) Consumer

func (h *HTTP2Transport) Consumer() <-chan RPC

func (*HTTP2Transport) ListenAndServe

func (h *HTTP2Transport) ListenAndServe() error

func (*HTTP2Transport) SendHeartbeat

func (h *HTTP2Transport) SendHeartbeat(ctx context.Context, peer string,
	msg *HeartBeatMsg, resp *HeartbeatResponse) error

func (*HTTP2Transport) SendVoteRequest

func (h *HTTP2Transport) SendVoteRequest(ctx context.Context, peer string,
	msg *VoteRequest, resp *VoteResponse) error

func (*HTTP2Transport) Shutdown

func (h *HTTP2Transport) Shutdown(ctx context.Context) error

type HeartBeatMsg

type HeartBeatMsg struct {
	Lead uint64 // Current leader announcing itself.
	Term uint64 // Current term.
}

HeartBeatMsg is the message sent by the leader to the followers.

type HeartbeatRequest

type HeartbeatRequest struct {
	Lead uint64
	Term uint64
}

type HeartbeatResponse

type HeartbeatResponse struct {
	Success bool
	Term    uint64
}

type Node

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

func NewNode

func NewNode(id uint64, heartbeatTimeout time.Duration, peers map[uint64]string,
	transport Transport) *Node

NewNode returns new Node.

func (*Node) AddPeer

func (n *Node) AddPeer(id uint64, url string)

func (*Node) IsLeader

func (n *Node) IsLeader() bool

func (*Node) RemovePeer

func (n *Node) RemovePeer(id uint64)

func (*Node) Run

func (n *Node) Run() error

func (*Node) Stop

func (n *Node) Stop()

type RPC

type RPC struct {
	Command  any
	RespChan chan RPCResponse
}

func (*RPC) Respond

func (r *RPC) Respond(resp any, err error)

Respond is used to respond with a response, error or both

type RPCResponse

type RPCResponse struct {
	Response any
	Error    error
}

type State

type State uint32

State is used to represent the state of the leader election

const (
	// Follower is initial state of the leader election
	Follower State = iota
	// Candidate is one of the valid states of the leader election
	Candidate
	// Leader is one of the valid states of the leader election
	Leader
)

func (State) String

func (s State) String() string

type Transport

type Transport interface {
	// SendVoteRequest sends a vote request message to the given peer.
	SendVoteRequest(ctx context.Context, peer string, msg *VoteRequest, resp *VoteResponse) error
	// SendHeartbeat sends a heartbeat message to the given peer.
	SendHeartbeat(ctx context.Context, peer string, msg *HeartBeatMsg, resp *HeartbeatResponse) error
	// Consumer returns a channel that can be used to
	// consume and respond to RPC requests.
	Consumer() <-chan RPC
}

Transport provides an interface for network transports to allow node to communicate with other nodes.

type VoteRequest

type VoteRequest struct {
	Candidate uint64 // Candidate requesting vote.
	Term      uint64 // Candidate's term.
}

VoteRequest is the message sent by the candidate to the followers.

type VoteResponse

type VoteResponse struct {
	VoterID uint64
	Term    uint64 // Voter term.
	Granted bool   // True means candidate received vote.
	Reason  string // Used for debugging.
}

VoteResponse is the response send by the follower to the candidate.

Jump to

Keyboard shortcuts

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