Rename to reconnect timeout.

This commit is contained in:
Jay
2026-04-18 10:59:45 -04:00
parent a8fb478971
commit 2d5e55ccaa
4 changed files with 28 additions and 22 deletions

View File

@@ -99,7 +99,7 @@ func WithWorkerFactory(wf WorkerFactory) PoolOption {
// Worker Config
type WorkerConfig struct {
IdleTimeout time.Duration
ReconnectTimeout time.Duration
MaxQueueSize int
}
@@ -118,7 +118,7 @@ func NewWorkerConfig(options ...WorkerOption) (*WorkerConfig, error) {
func GetDefaultWorkerConfig() *WorkerConfig {
return &WorkerConfig{
IdleTimeout: 20 * time.Second,
ReconnectTimeout: 20 * time.Second,
MaxQueueSize: 0, // disabled by default
}
}
@@ -133,7 +133,7 @@ func applyWorkerOptions(config *WorkerConfig, options ...WorkerOption) error {
}
func ValidateWorkerConfig(config *WorkerConfig) error {
err := validateIdleTimeout(config.IdleTimeout)
err := validateReconnectTimeout(config.ReconnectTimeout)
if err != nil {
return err
}
@@ -153,21 +153,21 @@ func validateMaxQueueSize(value int) error {
return nil
}
func validateIdleTimeout(value time.Duration) error {
func validateReconnectTimeout(value time.Duration) error {
if value < 0 {
return InvalidIdleTimeout
return InvalidReconnectTimeout
}
return nil
}
// When IdleTimeout is set to zero, idle timeouts are disabled.
func WithIdleTimeout(value time.Duration) WorkerOption {
// When ReconnectTimeout is set to zero, idle timeouts are disabled.
func WithReconnectTimeout(value time.Duration) WorkerOption {
return func(c *WorkerConfig) error {
err := validateIdleTimeout(value)
err := validateReconnectTimeout(value)
if err != nil {
return err
}
c.IdleTimeout = value
c.ReconnectTimeout = value
return nil
}
}

View File

@@ -4,7 +4,7 @@ import "errors"
import "fmt"
var (
InvalidIdleTimeout = errors.New("idle timeout cannot be negative")
InvalidReconnectTimeout = errors.New("idle timeout cannot be negative")
InvalidMaxQueueSize = errors.New("maximum queue size cannot be negative")
)

View File

@@ -119,8 +119,8 @@ func (w *Worker) runHealthMonitor(
stop <-chan struct{},
poolDone <-chan struct{},
) {
// disable if idle timeout is disabled
if w.config.IdleTimeout <= 0 {
// disable if reconnect timeout is disabled
if w.config.ReconnectTimeout <= 0 {
// wait for stop signal and exit
select {
case <-stop:
@@ -129,7 +129,7 @@ func (w *Worker) runHealthMonitor(
return
}
timer := time.NewTimer(w.config.IdleTimeout)
timer := time.NewTimer(w.config.ReconnectTimeout)
defer timer.Stop()
for {
@@ -146,7 +146,7 @@ func (w *Worker) runHealthMonitor(
default:
}
}
timer.Reset(w.config.IdleTimeout)
timer.Reset(w.config.ReconnectTimeout)
// timer completed
case <-timer.C:
// initiate reconnect, then reset the timer
@@ -155,7 +155,7 @@ func (w *Worker) runHealthMonitor(
case reconnect <- struct{}{}:
default:
}
timer.Reset(w.config.IdleTimeout)
timer.Reset(w.config.ReconnectTimeout)
}
}
}

View File

@@ -112,7 +112,7 @@ func TestRunHealthMonitor(t *testing.T) {
stop := make(chan struct{})
defer close(stop)
w := &Worker{config: &WorkerConfig{IdleTimeout: 100 * time.Millisecond}}
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 100 * time.Millisecond}}
go w.runHealthMonitor(heartbeat, reconnect, stop, nil)
// send heartbeats faster than the timeout
@@ -132,13 +132,13 @@ func TestRunHealthMonitor(t *testing.T) {
}, honeybeetest.NegativeTestTimeout, honeybeetest.TestTick)
})
t.Run("idle timeout fires reconnect", func(t *testing.T) {
t.Run("reconnect timeout fires reconnect", func(t *testing.T) {
heartbeat := make(chan struct{})
reconnect := make(chan struct{}, 1)
stop := make(chan struct{})
defer close(stop)
w := &Worker{config: &WorkerConfig{IdleTimeout: 20 * time.Millisecond}}
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 20 * time.Millisecond}}
go w.runHealthMonitor(heartbeat, reconnect, stop, nil)
// send no heartbeats, wait for timeout and reconnect signal
@@ -157,7 +157,7 @@ func TestRunHealthMonitor(t *testing.T) {
reconnect := make(chan struct{}, 1)
stop := make(chan struct{})
w := &Worker{config: &WorkerConfig{IdleTimeout: 20 * time.Second}}
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 20 * time.Second}}
done := make(chan struct{})
go func() {
w.runHealthMonitor(heartbeat, reconnect, stop, nil)
@@ -177,3 +177,9 @@ func TestRunHealthMonitor(t *testing.T) {
})
}
func TestRunReconnector(t *testing.T) {
t.Run("reconnect emits events, new connection", func(t *testing.T) {})
t.Run("dial failure emits error", func(t *testing.T) {})
t.Run("exits on stop", func(t *testing.T) {})
}