Renamed package, set up for worker session pattern.

This commit is contained in:
Jay
2026-04-18 12:44:49 -04:00
parent 2d5e55ccaa
commit 4c17b9539a
7 changed files with 117 additions and 58 deletions

View File

@@ -1,4 +1,4 @@
package initiator package initiatorpool
import ( import (
"git.wisehodl.dev/jay/go-honeybee/transport" "git.wisehodl.dev/jay/go-honeybee/transport"
@@ -99,7 +99,7 @@ func WithWorkerFactory(wf WorkerFactory) PoolOption {
// Worker Config // Worker Config
type WorkerConfig struct { type WorkerConfig struct {
ReconnectTimeout time.Duration IdleTimeout time.Duration
MaxQueueSize int MaxQueueSize int
} }
@@ -118,7 +118,7 @@ func NewWorkerConfig(options ...WorkerOption) (*WorkerConfig, error) {
func GetDefaultWorkerConfig() *WorkerConfig { func GetDefaultWorkerConfig() *WorkerConfig {
return &WorkerConfig{ return &WorkerConfig{
ReconnectTimeout: 20 * time.Second, IdleTimeout: 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 := validateReconnectTimeout(config.ReconnectTimeout) err := validateIdleTimeout(config.IdleTimeout)
if err != nil { if err != nil {
return err return err
} }
@@ -153,21 +153,21 @@ func validateMaxQueueSize(value int) error {
return nil return nil
} }
func validateReconnectTimeout(value time.Duration) error { func validateIdleTimeout(value time.Duration) error {
if value < 0 { if value < 0 {
return InvalidReconnectTimeout return InvalidIdleTimeout
} }
return nil return nil
} }
// When ReconnectTimeout is set to zero, idle timeouts are disabled. // When IdleTimeout is set to zero, idle timeouts are disabled.
func WithReconnectTimeout(value time.Duration) WorkerOption { func WithIdleTimeout(value time.Duration) WorkerOption {
return func(c *WorkerConfig) error { return func(c *WorkerConfig) error {
err := validateReconnectTimeout(value) err := validateIdleTimeout(value)
if err != nil { if err != nil {
return err return err
} }
c.ReconnectTimeout = value c.IdleTimeout = value
return nil return nil
} }
} }

View File

@@ -1,4 +1,4 @@
package initiator package initiatorpool
import ( import (
"git.wisehodl.dev/jay/go-honeybee/transport" "git.wisehodl.dev/jay/go-honeybee/transport"

View File

@@ -1,10 +1,10 @@
package initiator package initiatorpool
import "errors" import "errors"
import "fmt" import "fmt"
var ( var (
InvalidReconnectTimeout = errors.New("idle timeout cannot be negative") InvalidIdleTimeout = 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")
) )

View File

@@ -1,4 +1,4 @@
package initiator package initiatorpool
import ( import (
"git.wisehodl.dev/jay/go-honeybee/transport" "git.wisehodl.dev/jay/go-honeybee/transport"
@@ -111,6 +111,9 @@ func (p *Pool) Errors() chan error {
} }
func (p *Pool) SetDialer(d types.Dialer) { func (p *Pool) SetDialer(d types.Dialer) {
if d == nil {
panic("dialer cannot be nil")
}
p.dialer = d p.dialer = d
} }

View File

@@ -1,4 +1,4 @@
package initiator package initiatorpool
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package initiator package initiatorpool
import ( import (
"container/list" "container/list"
@@ -18,7 +18,7 @@ type Worker struct {
id string id string
stop <-chan struct{} stop <-chan struct{}
config *WorkerConfig config *WorkerConfig
conn *transport.Connection outbound chan []byte
} }
func NewWorker( func NewWorker(
@@ -39,14 +39,25 @@ func NewWorker(
w := &Worker{ w := &Worker{
id: id, id: id,
stop: stop, stop: stop,
outbound: make(chan []byte, 64),
config: config, config: config,
} }
return w, nil return w, nil
} }
func (w *Worker) dial(ctx WorkerContext) (*transport.Connection, error) {
conn, err := transport.NewConnection(w.id, ctx.ConnectionConfig, ctx.Logger)
if err != nil {
return nil, err
}
conn.SetDialer(ctx.Dialer)
return conn, conn.Connect()
}
func (w *Worker) Send(data []byte) error { func (w *Worker) Send(data []byte) error {
return w.conn.Send(data) return nil
} }
func (w *Worker) Start( func (w *Worker) Start(
@@ -55,15 +66,61 @@ func (w *Worker) Start(
) { ) {
} }
func (w *Worker) runReader( func (w *Worker) runSession(
conn *transport.Connection, conn *transport.Connection,
messages chan<- receivedMessage, messages chan<- receivedMessage,
heartbeat chan<- struct{}, heartbeat chan<- struct{},
reconnect chan<- struct{}, reconnect chan<- struct{},
newConn <-chan *transport.Connection,
stop <-chan struct{},
poolDone <-chan struct{},
outbound <-chan []byte,
idle <-chan struct{},
newConn <-chan *transport.Connection,
ctx WorkerContext,
workerDone <-chan struct{},
poolDone <-chan struct{},
)
func (w *Worker) runReader(
conn *transport.Connection,
messages chan<- receivedMessage,
heartbeat chan<- struct{},
workerDone <-chan struct{},
poolDone <-chan struct{},
sessionDone <-chan struct{},
onStop func(),
) {
}
func (w *Worker) runWriter(
conn *transport.Connection,
outbound <-chan []byte,
heartbeat chan<- struct{},
workerDone <-chan struct{},
poolDone <-chan struct{},
sessionDone <-chan struct{},
onStop func(),
) {
}
func (w *Worker) runStopMonitor(
conn *transport.Connection,
stop <-chan struct{},
workerDone <-chan struct{},
poolDone <-chan struct{},
sessionDone <-chan struct{},
onStop func(),
) { ) {
} }
@@ -113,14 +170,14 @@ func (w *Worker) runForwarder(
} }
} }
func (w *Worker) runHealthMonitor( func (w *Worker) runIdleMonitor(
heartbeat <-chan struct{}, heartbeat <-chan struct{},
reconnect chan<- struct{}, idle chan<- struct{},
stop <-chan struct{}, stop <-chan struct{},
poolDone <-chan struct{}, poolDone <-chan struct{},
) { ) {
// disable if reconnect timeout is disabled // disable idle timeout if not configured
if w.config.ReconnectTimeout <= 0 { if w.config.IdleTimeout <= 0 {
// wait for stop signal and exit // wait for stop signal and exit
select { select {
case <-stop: case <-stop:
@@ -129,7 +186,7 @@ func (w *Worker) runHealthMonitor(
return return
} }
timer := time.NewTimer(w.config.ReconnectTimeout) timer := time.NewTimer(w.config.IdleTimeout)
defer timer.Stop() defer timer.Stop()
for { for {
@@ -146,16 +203,15 @@ func (w *Worker) runHealthMonitor(
default: default:
} }
} }
timer.Reset(w.config.ReconnectTimeout) timer.Reset(w.config.IdleTimeout)
// timer completed // timer completed
case <-timer.C: case <-timer.C:
// initiate reconnect, then reset the timer // send idle signal, then reset the timer
// multiple reconnect signals during reconnection is idempotent
select { select {
case reconnect <- struct{}{}: case idle <- struct{}{}:
default: default:
} }
timer.Reset(w.config.ReconnectTimeout) timer.Reset(w.config.IdleTimeout)
} }
} }
} }

View File

@@ -1,4 +1,4 @@
package initiator package initiatorpool
import ( import (
"git.wisehodl.dev/jay/go-honeybee/honeybeetest" "git.wisehodl.dev/jay/go-honeybee/honeybeetest"
@@ -105,15 +105,15 @@ func TestRunForwarder(t *testing.T) {
}) })
} }
func TestRunHealthMonitor(t *testing.T) { func TestRunIdleMonitor(t *testing.T) {
t.Run("heartbeat resets timer, no reconnect fired", func(t *testing.T) { t.Run("heartbeat resets timer, no idle signal fired", func(t *testing.T) {
heartbeat := make(chan struct{}, 3) heartbeat := make(chan struct{}, 3)
reconnect := make(chan struct{}, 1) idle := make(chan struct{}, 1)
stop := make(chan struct{}) stop := make(chan struct{})
defer close(stop) defer close(stop)
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 100 * time.Millisecond}} w := &Worker{config: &WorkerConfig{IdleTimeout: 100 * time.Millisecond}}
go w.runHealthMonitor(heartbeat, reconnect, stop, nil) go w.runIdleMonitor(heartbeat, idle, stop, nil)
// send heartbeats faster than the timeout // send heartbeats faster than the timeout
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
@@ -121,10 +121,10 @@ func TestRunHealthMonitor(t *testing.T) {
heartbeat <- struct{}{} heartbeat <- struct{}{}
} }
// because the timer is being reset, reconnect should never occur // because the timer is being reset, idle signal should not be sent
assert.Never(t, func() bool { assert.Never(t, func() bool {
select { select {
case <-reconnect: case <-idle:
return true return true
default: default:
return false return false
@@ -132,19 +132,19 @@ func TestRunHealthMonitor(t *testing.T) {
}, honeybeetest.NegativeTestTimeout, honeybeetest.TestTick) }, honeybeetest.NegativeTestTimeout, honeybeetest.TestTick)
}) })
t.Run("reconnect timeout fires reconnect", func(t *testing.T) { t.Run("idle timeout fires signal", func(t *testing.T) {
heartbeat := make(chan struct{}) heartbeat := make(chan struct{})
reconnect := make(chan struct{}, 1) idle := make(chan struct{}, 1)
stop := make(chan struct{}) stop := make(chan struct{})
defer close(stop) defer close(stop)
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 20 * time.Millisecond}} w := &Worker{config: &WorkerConfig{IdleTimeout: 20 * time.Millisecond}}
go w.runHealthMonitor(heartbeat, reconnect, stop, nil) go w.runIdleMonitor(heartbeat, idle, stop, nil)
// send no heartbeats, wait for timeout and reconnect signal // send no heartbeats, wait for timeout and idle signal
assert.Eventually(t, func() bool { assert.Eventually(t, func() bool {
select { select {
case <-reconnect: case <-idle:
return true return true
default: default:
return false return false
@@ -154,13 +154,13 @@ func TestRunHealthMonitor(t *testing.T) {
t.Run("exits on stop", func(t *testing.T) { t.Run("exits on stop", func(t *testing.T) {
heartbeat := make(chan struct{}) heartbeat := make(chan struct{})
reconnect := make(chan struct{}, 1) idle := make(chan struct{}, 1)
stop := make(chan struct{}) stop := make(chan struct{})
w := &Worker{config: &WorkerConfig{ReconnectTimeout: 20 * time.Second}} w := &Worker{config: &WorkerConfig{IdleTimeout: 20 * time.Second}}
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
w.runHealthMonitor(heartbeat, reconnect, stop, nil) w.runIdleMonitor(heartbeat, idle, stop, nil)
close(done) close(done)
}() }()