Giter Club home page Giter Club logo

goelect's Introduction

goelect

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 to Raft 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.
type ElectConfig struct {
    // Timeout for heartbeat messages, in milliseconds
    HeartBeatInterval uint
    // Timeout for election messages, in milliseconds
    ElectTimeout uint
    // Timeout for connecting to peers, in seconds
    ConnectTimeout uint
    // List of peers in the network
    Peers []Node
    // Node represents the information of this node
    Node Node
    // State callbacks
    CallBacks *StateCallBacks
    // Timeout for callbacks, in seconds
    CallBackTimeout int
}

Example

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

Create an Elect instance:

e, err := goelect.NewElect(&goelect.ElectConfig{
		ElectTimeout:      200,
		HeartBeatInterval: 150,
		ConnectTimeout:    10,
		Peers:             peerNodes,
		// state transition callbacks
		CallBacks: &goelect.StateCallBacks{
			EnterLeader:    enterLeader,
			LeaveLeader:    leaveLeader,
			EnterFollower:  enterFollower,
			LeaveFollower:  leaveFollower,
			EnterCandidate: enterCandidate,
			LeaveCandidate: leaveCandidate,
		},
		// self node
		Node: goelect.Node{
			Address: *nodeAddress,
			ID:      *nodeAddress,
		},
	}, slog.Default())

Start the Elect:

err = e.Run()

This is everything we need to do :)

goelect's People

Contributors

danl5 avatar danli001 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.