Skip to content

Commit

Permalink
raft: improve base interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Login <batazor@evrone.com>
  • Loading branch information
batazor committed Feb 14, 2023
1 parent 4ffa7aa commit 5d7a238
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions pkg/raft/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,64 @@ import (
"time"
)

// Raft represents a distributed consensus algorithm.
type Raft interface {
Connect(config Config) error
// Start starts the Raft node.
// It returns an error if the node fails to start.
Start() error

// Stop stops the Raft node.
// It returns an error if the node fails to stop.
Stop() error

// AddNode adds a new node to the cluster.
// It takes a node ID and returns an error if the ID is invalid or
// the node fails to be added to the cluster.
AddNode(nodeID uint64) error

// RemoveNode removes a node from the cluster.
// It takes a node ID and returns an error if the ID is invalid or
// the node fails to be removed from the cluster.
RemoveNode(nodeID uint64) error

// Propose proposes a new command to the Raft cluster.
// It takes a node ID and a command value, which can be any type.
// The method returns an error if the ID is invalid or the proposal
// fails to be processed by the specified node.
Propose(nodeID uint64, command interface{}) error
}

type Vote struct {
Timeout time.Duration
NextVote time.Time
// RaftRole represents the role of a Raft node.
type RaftRole uint8

const (
Follower RaftRole = iota
Candidate
Leader
)

// RaftClient represents a client that interacts with a Raft cluster.
type RaftClient interface {
// Connect connects the client to the Raft cluster.
// It takes a Config struct and returns an error if the connection fails.
Connect(config Config) error

// Close closes the connection to the Raft cluster.
// It returns an error if the connection fails to be closed.
Close() error
}

// Config represents the configuration for a Raft client.
type Config struct {
Name string
Weight int
URI string
// NodeAddrs is a list of addresses of the nodes in the Raft cluster.
NodeAddrs []string

// Timeout is the timeout duration for requests to the Raft nodes.
Timeout time.Duration

Vote Vote
// RetryLimit is the number of times to retry failed requests to the Raft nodes.
RetryLimit int

Status string
// RetryInterval is the interval duration between retries of failed requests to the Raft nodes.
RetryInterval time.Duration
}

0 comments on commit 5d7a238

Please sign in to comment.