Skip to content

Commit

Permalink
refactor: introduce dedicated ApplyTimeout constant for Raft operations
Browse files Browse the repository at this point in the history
Replace usage of LeaderElectionTimeout with a new dedicated ApplyTimeout constant
(2 seconds) for Raft command applications across different load balancing
strategies (ConsistentHash, RoundRobin, WeightedRoundRobin).

This change provides better separation of concerns by using a more appropriate
timeout value for command applications rather than reusing the leader election
timeout.
  • Loading branch information
sinadarbouy committed Dec 20, 2024
1 parent f9c2a7b commit 8fbdb8a
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion network/consistenthash.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (ch *ConsistentHash) NextProxy(conn IConnWrapper) (IProxy, *gerr.GatewayDEr
}

// Apply the command through Raft
if err := ch.server.RaftNode.Apply(cmdBytes, raft.LeaderElectionTimeout); err != nil {
if err := ch.server.RaftNode.Apply(cmdBytes, raft.ApplyTimeout); err != nil {
return nil, gerr.ErrNoProxiesAvailable.Wrap(err)
}

Expand Down
2 changes: 1 addition & 1 deletion network/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (r *RoundRobin) NextProxy(_ IConnWrapper) (IProxy, *gerr.GatewayDError) {
}

// Apply through Raft
if err := r.server.RaftNode.Apply(data, raft.LeaderElectionTimeout); err != nil {
if err := r.server.RaftNode.Apply(data, raft.ApplyTimeout); err != nil {
return nil, gerr.ErrNoProxiesAvailable.Wrap(err)
}

Expand Down
2 changes: 1 addition & 1 deletion network/roundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestNextProxyOverflow(t *testing.T) {
data, err := json.Marshal(cmd)
require.NoError(t, err)

require.NoError(t, raftHelper.Node.Apply(data, raft.LeaderElectionTimeout))
require.NoError(t, raftHelper.Node.Apply(data, raft.ApplyTimeout))

// Call NextProxy multiple times to trigger the overflow
for range 4 {
Expand Down
2 changes: 1 addition & 1 deletion network/weightedroundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r *WeightedRoundRobin) NextProxy(_ IConnWrapper) (IProxy, *gerr.GatewayDEr
if err != nil {
return nil, gerr.ErrNoProxiesAvailable.Wrap(err)
}
if err := r.raftNode.Apply(data, raft.LeaderElectionTimeout); err != nil {
if err := r.raftNode.Apply(data, raft.ApplyTimeout); err != nil {
return nil, gerr.ErrNoProxiesAvailable.Wrap(err)
}

Expand Down
1 change: 1 addition & 0 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
maxPool = 3 // Maximum number of connections to pool
transportTimeout = 10 * time.Second // Timeout for transport operations
leadershipCheckInterval = 10 * time.Second // Interval for checking leadership status
ApplyTimeout = 2 * time.Second // Timeout for applying commands
)

// Command represents a general command structure for all operations.
Expand Down

0 comments on commit 8fbdb8a

Please sign in to comment.