Update session code outline.
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
|
KeepaliveTimeout 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,
|
KeepaliveTimeout: 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 := validateKeepaliveTimeout(config.KeepaliveTimeout)
|
||||||
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 validateKeepaliveTimeout(value time.Duration) error {
|
||||||
if value < 0 {
|
if value < 0 {
|
||||||
return InvalidIdleTimeout
|
return InvalidKeepaliveTimeout
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// When IdleTimeout is set to zero, idle timeouts are disabled.
|
// When KeepaliveTimeout is set to zero, keepalive timeouts are disabled.
|
||||||
func WithIdleTimeout(value time.Duration) WorkerOption {
|
func WithKeepaliveTimeout(value time.Duration) WorkerOption {
|
||||||
return func(c *WorkerConfig) error {
|
return func(c *WorkerConfig) error {
|
||||||
err := validateIdleTimeout(value)
|
err := validateKeepaliveTimeout(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.IdleTimeout = value
|
c.KeepaliveTimeout = 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")
|
InvalidKeepaliveTimeout = errors.New("keepalive 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 {
|
||||||
@@ -15,3 +15,7 @@ func NewConfigError(text string) error {
|
|||||||
func NewPoolError(text string) error {
|
func NewPoolError(text string) error {
|
||||||
return fmt.Errorf("pool error: %s", text)
|
return fmt.Errorf("pool error: %s", text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewWorkerError(id string, text string) error {
|
||||||
|
return fmt.Errorf("worker %q error: %s", id, text)
|
||||||
|
}
|
||||||
|
|||||||
@@ -57,7 +57,14 @@ func (w *Worker) dial(ctx WorkerContext) (*transport.Connection, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) Send(data []byte) error {
|
func (w *Worker) Send(data []byte) error {
|
||||||
return nil
|
select {
|
||||||
|
case w.outbound <- data:
|
||||||
|
return nil
|
||||||
|
case <-w.stop:
|
||||||
|
return NewWorkerError(w.id, "worker is stopped")
|
||||||
|
default:
|
||||||
|
return NewWorkerError(w.id, "outbound queue full")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) Start(
|
func (w *Worker) Start(
|
||||||
@@ -67,59 +74,45 @@ func (w *Worker) Start(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) runSession(
|
func (w *Worker) runSession(
|
||||||
conn *transport.Connection,
|
|
||||||
|
|
||||||
messages chan<- receivedMessage,
|
messages chan<- receivedMessage,
|
||||||
heartbeat chan<- struct{},
|
heartbeat chan<- struct{},
|
||||||
reconnect chan<- struct{},
|
dial chan<- struct{},
|
||||||
|
|
||||||
|
keepalive <-chan struct{},
|
||||||
outbound <-chan []byte,
|
outbound <-chan []byte,
|
||||||
idle <-chan struct{},
|
|
||||||
newConn <-chan *transport.Connection,
|
newConn <-chan *transport.Connection,
|
||||||
|
|
||||||
ctx WorkerContext,
|
ctx WorkerContext,
|
||||||
|
|
||||||
workerDone <-chan struct{},
|
workerStop <-chan struct{},
|
||||||
poolDone <-chan struct{},
|
poolDone <-chan struct{},
|
||||||
)
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Worker) runReader(
|
func (w *Worker) runReader(
|
||||||
conn *transport.Connection,
|
conn *transport.Connection,
|
||||||
|
|
||||||
messages chan<- receivedMessage,
|
messages chan<- receivedMessage,
|
||||||
heartbeat chan<- struct{},
|
heartbeat chan<- struct{},
|
||||||
|
|
||||||
workerDone <-chan struct{},
|
|
||||||
poolDone <-chan struct{},
|
|
||||||
sessionDone <-chan struct{},
|
sessionDone <-chan struct{},
|
||||||
|
|
||||||
onStop func(),
|
onStop func(),
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) runWriter(
|
func (w *Worker) runWriter(
|
||||||
conn *transport.Connection,
|
conn *transport.Connection,
|
||||||
|
|
||||||
outbound <-chan []byte,
|
outbound <-chan []byte,
|
||||||
heartbeat chan<- struct{},
|
heartbeat chan<- struct{},
|
||||||
|
|
||||||
workerDone <-chan struct{},
|
|
||||||
poolDone <-chan struct{},
|
|
||||||
sessionDone <-chan struct{},
|
sessionDone <-chan struct{},
|
||||||
|
|
||||||
onStop func(),
|
onStop func(),
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) runStopMonitor(
|
func (w *Worker) runStopMonitor(
|
||||||
conn *transport.Connection,
|
conn *transport.Connection,
|
||||||
|
keepalive <-chan struct{},
|
||||||
stop <-chan struct{},
|
workerStop <-chan struct{},
|
||||||
|
|
||||||
workerDone <-chan struct{},
|
|
||||||
poolDone <-chan struct{},
|
poolDone <-chan struct{},
|
||||||
sessionDone <-chan struct{},
|
sessionDone <-chan struct{},
|
||||||
|
|
||||||
onStop func(),
|
onStop func(),
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
@@ -170,14 +163,14 @@ func (w *Worker) runForwarder(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) runIdleMonitor(
|
func (w *Worker) runKeepalive(
|
||||||
heartbeat <-chan struct{},
|
heartbeat <-chan struct{},
|
||||||
idle chan<- struct{},
|
keepalive chan<- struct{},
|
||||||
stop <-chan struct{},
|
stop <-chan struct{},
|
||||||
poolDone <-chan struct{},
|
poolDone <-chan struct{},
|
||||||
) {
|
) {
|
||||||
// disable idle timeout if not configured
|
// disable keepalive timeout if not configured
|
||||||
if w.config.IdleTimeout <= 0 {
|
if w.config.KeepaliveTimeout <= 0 {
|
||||||
// wait for stop signal and exit
|
// wait for stop signal and exit
|
||||||
select {
|
select {
|
||||||
case <-stop:
|
case <-stop:
|
||||||
@@ -186,7 +179,7 @@ func (w *Worker) runIdleMonitor(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
timer := time.NewTimer(w.config.IdleTimeout)
|
timer := time.NewTimer(w.config.KeepaliveTimeout)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -203,22 +196,23 @@ func (w *Worker) runIdleMonitor(
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer.Reset(w.config.IdleTimeout)
|
timer.Reset(w.config.KeepaliveTimeout)
|
||||||
// timer completed
|
// timer completed
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
// send idle signal, then reset the timer
|
// send keepalive signal, then reset the timer
|
||||||
select {
|
select {
|
||||||
case idle <- struct{}{}:
|
case keepalive <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
timer.Reset(w.config.IdleTimeout)
|
timer.Reset(w.config.KeepaliveTimeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Worker) runReconnector(
|
func (w *Worker) runDialer(
|
||||||
reconnect <-chan struct{},
|
dial <-chan struct{},
|
||||||
newConn chan<- *transport.Connection,
|
newConn chan<- *transport.Connection,
|
||||||
|
ctx WorkerContext,
|
||||||
stop <-chan struct{},
|
stop <-chan struct{},
|
||||||
poolDone <-chan struct{},
|
poolDone <-chan struct{},
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -105,15 +105,15 @@ func TestRunForwarder(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunIdleMonitor(t *testing.T) {
|
func TestRunKeepalive(t *testing.T) {
|
||||||
t.Run("heartbeat resets timer, no idle signal fired", func(t *testing.T) {
|
t.Run("heartbeat resets timer, no keepalive signal fired", func(t *testing.T) {
|
||||||
heartbeat := make(chan struct{}, 3)
|
heartbeat := make(chan struct{}, 3)
|
||||||
idle := make(chan struct{}, 1)
|
keepalive := make(chan struct{}, 1)
|
||||||
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{KeepaliveTimeout: 100 * time.Millisecond}}
|
||||||
go w.runIdleMonitor(heartbeat, idle, stop, nil)
|
go w.runKeepalive(heartbeat, keepalive, 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 TestRunIdleMonitor(t *testing.T) {
|
|||||||
heartbeat <- struct{}{}
|
heartbeat <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// because the timer is being reset, idle signal should not be sent
|
// because the timer is being reset, keepalive signal should not be sent
|
||||||
assert.Never(t, func() bool {
|
assert.Never(t, func() bool {
|
||||||
select {
|
select {
|
||||||
case <-idle:
|
case <-keepalive:
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
@@ -132,19 +132,19 @@ func TestRunIdleMonitor(t *testing.T) {
|
|||||||
}, honeybeetest.NegativeTestTimeout, honeybeetest.TestTick)
|
}, honeybeetest.NegativeTestTimeout, honeybeetest.TestTick)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("idle timeout fires signal", func(t *testing.T) {
|
t.Run("keepalive timeout fires signal", func(t *testing.T) {
|
||||||
heartbeat := make(chan struct{})
|
heartbeat := make(chan struct{})
|
||||||
idle := make(chan struct{}, 1)
|
keepalive := 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{KeepaliveTimeout: 20 * time.Millisecond}}
|
||||||
go w.runIdleMonitor(heartbeat, idle, stop, nil)
|
go w.runKeepalive(heartbeat, keepalive, stop, nil)
|
||||||
|
|
||||||
// send no heartbeats, wait for timeout and idle signal
|
// send no heartbeats, wait for timeout and keepalive signal
|
||||||
assert.Eventually(t, func() bool {
|
assert.Eventually(t, func() bool {
|
||||||
select {
|
select {
|
||||||
case <-idle:
|
case <-keepalive:
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
@@ -154,13 +154,13 @@ func TestRunIdleMonitor(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{})
|
||||||
idle := make(chan struct{}, 1)
|
keepalive := make(chan struct{}, 1)
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
|
|
||||||
w := &Worker{config: &WorkerConfig{IdleTimeout: 20 * time.Second}}
|
w := &Worker{config: &WorkerConfig{KeepaliveTimeout: 20 * time.Second}}
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
w.runIdleMonitor(heartbeat, idle, stop, nil)
|
w.runKeepalive(heartbeat, keepalive, stop, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -177,9 +177,3 @@ func TestRunIdleMonitor(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