Rename to reconnect timeout.
This commit is contained in:
@@ -99,8 +99,8 @@ func WithWorkerFactory(wf WorkerFactory) PoolOption {
|
|||||||
// Worker Config
|
// Worker Config
|
||||||
|
|
||||||
type WorkerConfig struct {
|
type WorkerConfig struct {
|
||||||
IdleTimeout time.Duration
|
ReconnectTimeout time.Duration
|
||||||
MaxQueueSize int
|
MaxQueueSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkerOption func(*WorkerConfig) error
|
type WorkerOption func(*WorkerConfig) error
|
||||||
@@ -118,8 +118,8 @@ func NewWorkerConfig(options ...WorkerOption) (*WorkerConfig, error) {
|
|||||||
|
|
||||||
func GetDefaultWorkerConfig() *WorkerConfig {
|
func GetDefaultWorkerConfig() *WorkerConfig {
|
||||||
return &WorkerConfig{
|
return &WorkerConfig{
|
||||||
IdleTimeout: 20 * time.Second,
|
ReconnectTimeout: 20 * time.Second,
|
||||||
MaxQueueSize: 0, // disabled by default
|
MaxQueueSize: 0, // disabled by default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ func applyWorkerOptions(config *WorkerConfig, options ...WorkerOption) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ValidateWorkerConfig(config *WorkerConfig) error {
|
func ValidateWorkerConfig(config *WorkerConfig) error {
|
||||||
err := validateIdleTimeout(config.IdleTimeout)
|
err := validateReconnectTimeout(config.ReconnectTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -153,21 +153,21 @@ func validateMaxQueueSize(value int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateIdleTimeout(value time.Duration) error {
|
func validateReconnectTimeout(value time.Duration) error {
|
||||||
if value < 0 {
|
if value < 0 {
|
||||||
return InvalidIdleTimeout
|
return InvalidReconnectTimeout
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// When IdleTimeout is set to zero, idle timeouts are disabled.
|
// When ReconnectTimeout is set to zero, idle timeouts are disabled.
|
||||||
func WithIdleTimeout(value time.Duration) WorkerOption {
|
func WithReconnectTimeout(value time.Duration) WorkerOption {
|
||||||
return func(c *WorkerConfig) error {
|
return func(c *WorkerConfig) error {
|
||||||
err := validateIdleTimeout(value)
|
err := validateReconnectTimeout(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.IdleTimeout = value
|
c.ReconnectTimeout = value
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import "errors"
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
var (
|
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")
|
InvalidMaxQueueSize = errors.New("maximum queue size cannot be negative")
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewConfigError(text string) error {
|
func NewConfigError(text string) error {
|
||||||
|
|||||||
@@ -119,8 +119,8 @@ func (w *Worker) runHealthMonitor(
|
|||||||
stop <-chan struct{},
|
stop <-chan struct{},
|
||||||
poolDone <-chan struct{},
|
poolDone <-chan struct{},
|
||||||
) {
|
) {
|
||||||
// disable if idle timeout is disabled
|
// disable if reconnect timeout is disabled
|
||||||
if w.config.IdleTimeout <= 0 {
|
if w.config.ReconnectTimeout <= 0 {
|
||||||
// wait for stop signal and exit
|
// wait for stop signal and exit
|
||||||
select {
|
select {
|
||||||
case <-stop:
|
case <-stop:
|
||||||
@@ -129,7 +129,7 @@ func (w *Worker) runHealthMonitor(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
timer := time.NewTimer(w.config.IdleTimeout)
|
timer := time.NewTimer(w.config.ReconnectTimeout)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -146,7 +146,7 @@ func (w *Worker) runHealthMonitor(
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer.Reset(w.config.IdleTimeout)
|
timer.Reset(w.config.ReconnectTimeout)
|
||||||
// timer completed
|
// timer completed
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
// initiate reconnect, then reset the timer
|
// initiate reconnect, then reset the timer
|
||||||
@@ -155,7 +155,7 @@ func (w *Worker) runHealthMonitor(
|
|||||||
case reconnect <- struct{}{}:
|
case reconnect <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
timer.Reset(w.config.IdleTimeout)
|
timer.Reset(w.config.ReconnectTimeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ func TestRunHealthMonitor(t *testing.T) {
|
|||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
defer close(stop)
|
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)
|
go w.runHealthMonitor(heartbeat, reconnect, stop, nil)
|
||||||
|
|
||||||
// send heartbeats faster than the timeout
|
// send heartbeats faster than the timeout
|
||||||
@@ -132,13 +132,13 @@ func TestRunHealthMonitor(t *testing.T) {
|
|||||||
}, honeybeetest.NegativeTestTimeout, honeybeetest.TestTick)
|
}, 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{})
|
heartbeat := make(chan struct{})
|
||||||
reconnect := make(chan struct{}, 1)
|
reconnect := make(chan struct{}, 1)
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
defer close(stop)
|
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)
|
go w.runHealthMonitor(heartbeat, reconnect, stop, nil)
|
||||||
|
|
||||||
// send no heartbeats, wait for timeout and reconnect signal
|
// send no heartbeats, wait for timeout and reconnect signal
|
||||||
@@ -157,7 +157,7 @@ func TestRunHealthMonitor(t *testing.T) {
|
|||||||
reconnect := make(chan struct{}, 1)
|
reconnect := make(chan struct{}, 1)
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
|
|
||||||
w := &Worker{config: &WorkerConfig{IdleTimeout: 20 * time.Second}}
|
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 20 * time.Second}}
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
w.runHealthMonitor(heartbeat, reconnect, stop, nil)
|
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) {})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user