Skip to content

Commit

Permalink
Merge pull request #82 from jmsadair/80-why-are-there-restrictions-on…
Browse files Browse the repository at this point in the history
…-the-election-timeout-heartbeat-and-lease

Remove lease, election, and heartbeat time restrictions
  • Loading branch information
jmsadair authored Jun 9, 2024
2 parents 430f988 + 7fc570c commit 8a0bfeb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 69 deletions.
25 changes: 2 additions & 23 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@ package raft

import (
"errors"
"fmt"
"time"

"github.com/jmsadair/raft/logging"
)

const (
minElectionTimeout = time.Duration(100 * time.Millisecond)
maxElectionTimeout = time.Duration(3000 * time.Millisecond)
defaultElectionTimeout = time.Duration(300 * time.Millisecond)

minHeartbeat = time.Duration(25 * time.Millisecond)
maxHeartbeat = time.Duration(1000 * time.Millisecond)
defaultHeartbeat = time.Duration(50 * time.Millisecond)

minLeaseDuration = time.Duration(25 * time.Millisecond)
maxLeaseDuration = time.Duration(1500 * time.Millisecond)
defaultLeaseDuration = time.Duration(100 * time.Millisecond)
defaultHeartbeat = time.Duration(50 * time.Millisecond)
defaultLeaseDuration = time.Duration(100 * time.Millisecond)
)

type options struct {
Expand Down Expand Up @@ -60,10 +51,6 @@ type Option func(options *options) error
// WithElectionTimeout sets the election timeout for raft.
func WithElectionTimeout(time time.Duration) Option {
return func(options *options) error {
if time < minElectionTimeout || time > maxElectionTimeout {
return fmt.Errorf("election timeout value is invalid: minimum = %v, maximum = %v",
minElectionTimeout, maxElectionTimeout)
}
options.electionTimeout = time
return nil
}
Expand All @@ -72,10 +59,6 @@ func WithElectionTimeout(time time.Duration) Option {
// WithHeartbeatInterval sets the heartbeat interval for raft.
func WithHeartbeatInterval(time time.Duration) Option {
return func(options *options) error {
if time < minHeartbeat || time > maxHeartbeat {
return fmt.Errorf("heartbeat interval value is invalid: minimum = %v, maximum = %v",
minHeartbeat, maxHeartbeat)
}
options.heartbeatInterval = time
return nil
}
Expand All @@ -86,10 +69,6 @@ func WithHeartbeatInterval(time time.Duration) Option {
// time than the election timeout.
func WithLeaseDuration(leaseDuration time.Duration) Option {
return func(options *options) error {
if leaseDuration < minLeaseDuration || leaseDuration > maxLeaseDuration {
return fmt.Errorf("lease duration is invalid: minimum = %v, maximum = %v",
minLeaseDuration, maxLeaseDuration)
}
options.leaseDuration = leaseDuration
return nil
}
Expand Down
46 changes: 0 additions & 46 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,10 @@ package raft

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)

// TestWithElectionTimeout checks that the election timeout option only accepts values within the
// defined range.
func TestWithElectionTimeout(t *testing.T) {
options := &options{}

// Test minimum bound
require.Error(t, WithElectionTimeout(minElectionTimeout-time.Millisecond)(options))

// Test maximum bound
require.Error(t, WithElectionTimeout(maxElectionTimeout+time.Millisecond)(options))

// Test valid input
require.NoError(t, WithElectionTimeout(500*time.Millisecond)(options))
}

// TestWithHeartbeatInterval checks that the heartbeat interval option only accepts values within the
// defined range.
func TestWithHeartbeatInterval(t *testing.T) {
options := &options{}

// Test minimum bound
require.Error(t, WithHeartbeatInterval(minHeartbeat-time.Millisecond)(options))

// Test maximum bound
require.Error(t, WithHeartbeatInterval(maxHeartbeat+time.Millisecond)(options))

// Test valid input
require.NoError(t, WithHeartbeatInterval(250*time.Millisecond)(options))
}

// TestWithLeaseDuration checks that the lease duration option only accepts values within the
// defined range.
func TestWithLeaseDuration(t *testing.T) {
options := &options{}

// Test minimum bound
require.Error(t, WithLeaseDuration(minLeaseDuration-time.Millisecond)(options))

// Test maximum bound
require.Error(t, WithLeaseDuration(maxLeaseDuration+time.Millisecond)(options))

// Test valid input
require.NoError(t, WithLeaseDuration(500*time.Millisecond)(options))
}

// TestWithLog checks that the log option only accepts non-nil logs.
func TestWithLog(t *testing.T) {
options := &options{}
Expand Down

0 comments on commit 8a0bfeb

Please sign in to comment.