Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Goelect is an open-source, self-contained Golang library designed to facilitate leader election within a distributed system.

License

NotificationsYou must be signed in to change notification settings

danl5/goelect

Repository files navigation

goelect

Go Report CardGitHub release (latest SemVer)

Goelect is an open-source Go (Golang) library for leader election. It is heavily influenced by the election component of the Raft implementation. For more details, you can refer toRaft Wiki.

Features

  • Independent Operation: No third-party services are required. You don't need to set up or rely on external systems like ZooKeeper or etcd.
  • Simplified Integration: Easy to integrate into your existing Golang projects with minimal configuration.
  • Supports Novote role:The no-vote node does not participate in the election.
  • Highly Available: Built to be fault-tolerant, suitable for systems that require high availability.

How to use

Config

// ElectConfig is a struct that represents the configuration for an election.typeElectConfigstruct {// Timeout for heartbeat messages, in millisecondsHeartBeatIntervaluint// Timeout for election messages, in millisecondsElectTimeoutuint// Timeout for connecting to peers, in secondsConnectTimeoutuint// List of peers in the networkPeers []Node// Node represents the information of this nodeNodeNode// State callbacksCallBacks*StateCallBacks// Timeout for callbacks, in secondsCallBackTimeoutint}

Example

examples/onenode/node.go is a great example of using this goelect package.

Create an Elect instance:

// use the built-in RPC as the transport layer.rpcTransport,err:=rpc.NewRPC(logger)iferr!=nil {returnnil,err }e,err:=goelect.NewElect(rpcTransport,// rpc transport config&rpc.Config{},&goelect.ElectConfig{ElectTimeout:200,HeartBeatInterval:150,ConnectTimeout:10,Peers:peerNodes,// state transition callbacksCallBacks:&goelect.StateCallBacks{EnterLeader:enterLeader,LeaveLeader:leaveLeader,EnterFollower:enterFollower,LeaveFollower:leaveFollower,EnterCandidate:enterCandidate,LeaveCandidate:leaveCandidate,        },// self nodeNode: goelect.Node{Address:*nodeAddress,ID:*nodeAddress,        },    },logger)

Start the Elect:

err=e.Run()

This is everything we need to do :)

About

Goelect is an open-source, self-contained Golang library designed to facilitate leader election within a distributed system.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp