Added logging to pools and workers.
This commit is contained in:
+29
-4
@@ -13,12 +13,12 @@ import (
|
|||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
type PoolEventKind int
|
type PoolEventKind string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EventDisconnected PoolEventKind = iota
|
EventDisconnected PoolEventKind = "disconnected"
|
||||||
EventDropped
|
EventDropped PoolEventKind = "dropped"
|
||||||
EventEvicted
|
EventEvicted PoolEventKind = "evicted"
|
||||||
)
|
)
|
||||||
|
|
||||||
var workerToPoolEvent = map[WorkerExitKind]PoolEventKind{
|
var workerToPoolEvent = map[WorkerExitKind]PoolEventKind{
|
||||||
@@ -152,6 +152,10 @@ func (p *Pool) Errors() <-chan error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Close() {
|
func (p *Pool) Close() {
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Debug("closing")
|
||||||
|
}
|
||||||
|
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
if p.closed {
|
if p.closed {
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
@@ -177,6 +181,10 @@ func (p *Pool) Close() {
|
|||||||
close(p.inbox)
|
close(p.inbox)
|
||||||
close(p.events)
|
close(p.events)
|
||||||
close(p.errors)
|
close(p.errors)
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("closed")
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,6 +213,11 @@ func (p *Pool) Replace(id string, socket types.Socket) error {
|
|||||||
|
|
||||||
if peer, exists := p.peers[id]; exists {
|
if peer, exists := p.peers[id]; exists {
|
||||||
p.removeLocked(peer)
|
p.removeLocked(peer)
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("removed peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ErrPeerNotFound
|
return ErrPeerNotFound
|
||||||
}
|
}
|
||||||
@@ -213,6 +226,10 @@ func (p *Pool) Replace(id string, socket types.Socket) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Remove(id string) error {
|
func (p *Pool) Remove(id string) error {
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Debug("removing peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
@@ -227,6 +244,10 @@ func (p *Pool) Remove(id string) error {
|
|||||||
|
|
||||||
p.removeLocked(peer)
|
p.removeLocked(peer)
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("removed peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,6 +337,10 @@ func (p *Pool) addLocked(id string, socket types.Socket) error {
|
|||||||
|
|
||||||
p.peers[id] = peer
|
p.peers[id] = peer
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("added peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+44
-7
@@ -17,12 +17,12 @@ type Worker interface {
|
|||||||
Send(data []byte) error
|
Send(data []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkerExitKind int
|
type WorkerExitKind string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ExitDisconnected WorkerExitKind = iota
|
ExitDisconnected WorkerExitKind = "disconnected"
|
||||||
ExitError
|
ExitError WorkerExitKind = "error"
|
||||||
ExitPolicy
|
ExitPolicy WorkerExitKind = "policy"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DefaultWorker struct {
|
type DefaultWorker struct {
|
||||||
@@ -62,6 +62,10 @@ func NewWorker(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *DefaultWorker) Start(pool PoolPlugin) {
|
func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Debug("starting")
|
||||||
|
}
|
||||||
|
|
||||||
toQueue := make(chan types.ReceivedMessage, 256)
|
toQueue := make(chan types.ReceivedMessage, 256)
|
||||||
toForwarder := make(chan types.ReceivedMessage, 256)
|
toForwarder := make(chan types.ReceivedMessage, 256)
|
||||||
|
|
||||||
@@ -70,7 +74,7 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
RunReader(w.ctx, pool.OnExit, w.conn, toQueue, w.heartbeat)
|
RunReader(w.ctx, pool.OnExit, w.conn, toQueue, w.heartbeat, w.logger)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -85,13 +89,24 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
RunWatchdog(w.ctx, pool.OnExit, w.heartbeat, w.config.InactivityTimeout)
|
RunWatchdog(w.ctx, pool.OnExit, w.heartbeat, w.config.InactivityTimeout, w.logger)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Info("started")
|
||||||
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Info("stopped")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *DefaultWorker) Stop() {
|
func (w *DefaultWorker) Stop() {
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Debug("shutting down")
|
||||||
|
}
|
||||||
w.cancel()
|
w.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,6 +130,8 @@ func RunReader(
|
|||||||
conn *transport.Connection,
|
conn *transport.Connection,
|
||||||
messages chan<- types.ReceivedMessage,
|
messages chan<- types.ReceivedMessage,
|
||||||
heartbeat chan<- struct{},
|
heartbeat chan<- struct{},
|
||||||
|
|
||||||
|
logger *slog.Logger,
|
||||||
) {
|
) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -122,6 +139,7 @@ func RunReader(
|
|||||||
return
|
return
|
||||||
case data, ok := <-conn.Incoming():
|
case data, ok := <-conn.Incoming():
|
||||||
if !ok {
|
if !ok {
|
||||||
|
var err error
|
||||||
// determine exit kind
|
// determine exit kind
|
||||||
// by default, the peer dropped unexpectedly
|
// by default, the peer dropped unexpectedly
|
||||||
kind := ExitError
|
kind := ExitError
|
||||||
@@ -129,13 +147,21 @@ func RunReader(
|
|||||||
// the peer-side error is sent before the connection is closed,
|
// the peer-side error is sent before the connection is closed,
|
||||||
// so a non-blocking call here is correct
|
// so a non-blocking call here is correct
|
||||||
// if an error is not sent, then assume the default event kind
|
// if an error is not sent, then assume the default event kind
|
||||||
case err := <-conn.Errors():
|
case err = <-conn.Errors():
|
||||||
if errors.Is(err, transport.ErrPeerClosedClean) {
|
if errors.Is(err, transport.ErrPeerClosedClean) {
|
||||||
kind = ExitDisconnected
|
kind = ExitDisconnected
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if logger != nil {
|
||||||
|
if kind == ExitError {
|
||||||
|
logger.Error("reader: peer dropped", "event", kind, "error", err)
|
||||||
|
} else {
|
||||||
|
logger.Info("reader: peer disconnected", "event", kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onPeerClose(kind)
|
onPeerClose(kind)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -184,9 +210,13 @@ func RunWatchdog(
|
|||||||
onInactive OnExitFunction,
|
onInactive OnExitFunction,
|
||||||
heartbeat <-chan struct{},
|
heartbeat <-chan struct{},
|
||||||
timeout time.Duration,
|
timeout time.Duration,
|
||||||
|
logger *slog.Logger,
|
||||||
) {
|
) {
|
||||||
// disable watchdog timeout if not configured
|
// disable watchdog timeout if not configured
|
||||||
if timeout <= 0 {
|
if timeout <= 0 {
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("watchdog: disabled")
|
||||||
|
}
|
||||||
// drain heartbeats
|
// drain heartbeats
|
||||||
// wait for cancel and exit
|
// wait for cancel and exit
|
||||||
for {
|
for {
|
||||||
@@ -198,6 +228,10 @@ func RunWatchdog(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("watchdog: enabled", "timeout", timeout)
|
||||||
|
}
|
||||||
|
|
||||||
timer := time.NewTimer(timeout)
|
timer := time.NewTimer(timeout)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
@@ -217,6 +251,9 @@ func RunWatchdog(
|
|||||||
// timer completed
|
// timer completed
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
// signal peer is inactive
|
// signal peer is inactive
|
||||||
|
if logger != nil {
|
||||||
|
logger.Info("watchdog: peer is inactive")
|
||||||
|
}
|
||||||
onInactive(ExitPolicy)
|
onInactive(ExitPolicy)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
go RunReader(ctx, func(WorkerExitKind) {}, conn, messages, heartbeat)
|
go RunReader(ctx, func(WorkerExitKind) {}, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
incoming <- honeybeetest.MockIncomingData{MsgType: websocket.TextMessage, Data: []byte("hello")}
|
incoming <- honeybeetest.MockIncomingData{MsgType: websocket.TextMessage, Data: []byte("hello")}
|
||||||
@@ -57,7 +57,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
for range messages {
|
for range messages {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go RunReader(ctx, func(WorkerExitKind) {}, conn, messages, heartbeat)
|
go RunReader(ctx, func(WorkerExitKind) {}, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
const n = 3
|
const n = 3
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
@@ -92,7 +92,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
go RunReader(ctx, func(kind WorkerExitKind) {
|
go RunReader(ctx, func(kind WorkerExitKind) {
|
||||||
gotKind = kind
|
gotKind = kind
|
||||||
close(done)
|
close(done)
|
||||||
}, conn, messages, heartbeat)
|
}, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
select {
|
select {
|
||||||
@@ -129,7 +129,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
go RunReader(ctx, func(kind WorkerExitKind) {
|
go RunReader(ctx, func(kind WorkerExitKind) {
|
||||||
gotKind = kind
|
gotKind = kind
|
||||||
close(done)
|
close(done)
|
||||||
}, conn, messages, heartbeat)
|
}, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
select {
|
select {
|
||||||
@@ -166,7 +166,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
go RunReader(ctx, func(kind WorkerExitKind) {
|
go RunReader(ctx, func(kind WorkerExitKind) {
|
||||||
gotKind = kind
|
gotKind = kind
|
||||||
close(done)
|
close(done)
|
||||||
}, conn, messages, heartbeat)
|
}, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
select {
|
select {
|
||||||
@@ -193,7 +193,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
RunReader(ctx, func(WorkerExitKind) {
|
RunReader(ctx, func(WorkerExitKind) {
|
||||||
called.Store(true)
|
called.Store(true)
|
||||||
}, conn, messages, heartbeat)
|
}, conn, messages, heartbeat, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func TestRunWatchdog(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
called := atomic.Bool{}
|
called := atomic.Bool{}
|
||||||
go RunWatchdog(ctx, func(WorkerExitKind) { called.Store(true) }, heartbeat, 200*time.Millisecond)
|
go RunWatchdog(ctx, func(WorkerExitKind) { called.Store(true) }, heartbeat, 200*time.Millisecond, nil)
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
time.Sleep(20 * time.Millisecond)
|
time.Sleep(20 * time.Millisecond)
|
||||||
@@ -40,7 +40,7 @@ func TestRunWatchdog(t *testing.T) {
|
|||||||
count.Add(1)
|
count.Add(1)
|
||||||
gotKind = kind
|
gotKind = kind
|
||||||
close(done)
|
close(done)
|
||||||
}, heartbeat, 20*time.Millisecond)
|
}, heartbeat, 20*time.Millisecond, nil)
|
||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
select {
|
select {
|
||||||
@@ -62,7 +62,7 @@ func TestRunWatchdog(t *testing.T) {
|
|||||||
called := atomic.Bool{}
|
called := atomic.Bool{}
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
RunWatchdog(ctx, func(WorkerExitKind) { called.Store(true) }, heartbeat, 20*time.Second)
|
RunWatchdog(ctx, func(WorkerExitKind) { called.Store(true) }, heartbeat, 20*time.Second, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ func TestRunWatchdog(t *testing.T) {
|
|||||||
called := atomic.Bool{}
|
called := atomic.Bool{}
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
RunWatchdog(ctx, func(WorkerExitKind) { called.Store(true) }, heartbeat, 0)
|
RunWatchdog(ctx, func(WorkerExitKind) { called.Store(true) }, heartbeat, 0, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -112,7 +112,7 @@ func TestRunWatchdog(t *testing.T) {
|
|||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
RunWatchdog(ctx, func(WorkerExitKind) {}, heartbeat, 0)
|
RunWatchdog(ctx, func(WorkerExitKind) {}, heartbeat, 0, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -146,6 +146,10 @@ func (p *Pool) SetDialer(d types.Dialer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Close() {
|
func (p *Pool) Close() {
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Debug("closing")
|
||||||
|
}
|
||||||
|
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
if p.closed {
|
if p.closed {
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
@@ -165,10 +169,18 @@ func (p *Pool) Close() {
|
|||||||
close(p.inbox)
|
close(p.inbox)
|
||||||
close(p.events)
|
close(p.events)
|
||||||
close(p.errors)
|
close(p.errors)
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("closed")
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Connect(id string) error {
|
func (p *Pool) Connect(id string) error {
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Debug("connecting to peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
id, err := transport.NormalizeURL(id)
|
id, err := transport.NormalizeURL(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -215,10 +227,18 @@ func (p *Pool) Connect(id string) error {
|
|||||||
|
|
||||||
p.peers[id] = &Peer{id: id, worker: worker}
|
p.peers[id] = &Peer{id: id, worker: worker}
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("connected to peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Remove(id string) error {
|
func (p *Pool) Remove(id string) error {
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Debug("disconnecting from peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
id, err := transport.NormalizeURL(id)
|
id, err := transport.NormalizeURL(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -239,6 +259,10 @@ func (p *Pool) Remove(id string) error {
|
|||||||
|
|
||||||
peer.worker.Stop()
|
peer.worker.Stop()
|
||||||
|
|
||||||
|
if p.logger != nil {
|
||||||
|
p.logger.Info("disconnected from peer", "peer", id)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+80
-4
@@ -57,6 +57,10 @@ func NewWorker(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *DefaultWorker) Start(pool PoolPlugin) {
|
func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Debug("starting")
|
||||||
|
}
|
||||||
|
|
||||||
dial := make(chan struct{}, 1)
|
dial := make(chan struct{}, 1)
|
||||||
newConn := make(chan *transport.Connection, 1)
|
newConn := make(chan *transport.Connection, 1)
|
||||||
toQueue := make(chan types.ReceivedMessage, 256)
|
toQueue := make(chan types.ReceivedMessage, 256)
|
||||||
@@ -68,12 +72,12 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
RunDialer(w.id, w.ctx, pool, dial, newConn)
|
RunDialer(w.id, w.ctx, pool, dial, newConn, w.logger)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
RunKeepalive(w.ctx, w.heartbeat, keepalive, w.config.KeepaliveTimeout)
|
RunKeepalive(w.ctx, w.heartbeat, keepalive, w.config.KeepaliveTimeout, w.logger)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -96,14 +100,26 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
|||||||
dial: dial,
|
dial: dial,
|
||||||
keepalive: keepalive,
|
keepalive: keepalive,
|
||||||
newConn: newConn,
|
newConn: newConn,
|
||||||
|
logger: w.logger,
|
||||||
}
|
}
|
||||||
session.Start(w.ctx, pool)
|
session.Start(w.ctx, pool)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Info("started")
|
||||||
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Info("stopped")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *DefaultWorker) Stop() {
|
func (w *DefaultWorker) Stop() {
|
||||||
|
if w.logger != nil {
|
||||||
|
w.logger.Debug("shutting down")
|
||||||
|
}
|
||||||
w.cancel()
|
w.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,6 +152,8 @@ type Session struct {
|
|||||||
|
|
||||||
keepalive <-chan struct{}
|
keepalive <-chan struct{}
|
||||||
newConn <-chan *transport.Connection
|
newConn <-chan *transport.Connection
|
||||||
|
|
||||||
|
logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Start(
|
func (s *Session) Start(
|
||||||
@@ -143,6 +161,10 @@ func (s *Session) Start(
|
|||||||
pool PoolPlugin,
|
pool PoolPlugin,
|
||||||
) {
|
) {
|
||||||
for {
|
for {
|
||||||
|
if s.logger != nil {
|
||||||
|
s.logger.Debug("session: requesting connection")
|
||||||
|
}
|
||||||
|
|
||||||
// request new connection
|
// request new connection
|
||||||
select {
|
select {
|
||||||
case s.dial <- struct{}{}:
|
case s.dial <- struct{}{}:
|
||||||
@@ -159,9 +181,16 @@ func (s *Session) Start(
|
|||||||
case <-s.keepalive:
|
case <-s.keepalive:
|
||||||
select {
|
select {
|
||||||
case s.dial <- struct{}{}:
|
case s.dial <- struct{}{}:
|
||||||
|
if s.logger != nil {
|
||||||
|
s.logger.Debug("session: requesting connection")
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
case conn = <-s.newConn:
|
case conn = <-s.newConn:
|
||||||
|
if s.logger != nil {
|
||||||
|
s.logger.Debug("session: connected")
|
||||||
|
}
|
||||||
break preConn
|
break preConn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,16 +208,24 @@ func (s *Session) Start(
|
|||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
RunReader(sctx, onStop, conn, s.messages, s.heartbeat)
|
RunReader(sctx, onStop, conn, s.messages, s.heartbeat, s.logger)
|
||||||
}()
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
RunStopMonitor(sctx, onStop, conn, s.keepalive)
|
RunStopMonitor(sctx, onStop, conn, s.keepalive, s.logger)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if s.logger != nil {
|
||||||
|
s.logger.Info("session: started")
|
||||||
|
}
|
||||||
|
|
||||||
// complete session
|
// complete session
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
|
if s.logger != nil {
|
||||||
|
s.logger.Info("session: ended")
|
||||||
|
}
|
||||||
|
|
||||||
// tear down connection
|
// tear down connection
|
||||||
s.connPtr.Store(nil)
|
s.connPtr.Store(nil)
|
||||||
pool.Events <- PoolEvent{ID: s.id, Kind: EventDisconnected}
|
pool.Events <- PoolEvent{ID: s.id, Kind: EventDisconnected}
|
||||||
@@ -211,8 +248,13 @@ func RunReader(
|
|||||||
conn *transport.Connection,
|
conn *transport.Connection,
|
||||||
messages chan<- types.ReceivedMessage,
|
messages chan<- types.ReceivedMessage,
|
||||||
heartbeat chan<- struct{},
|
heartbeat chan<- struct{},
|
||||||
|
logger *slog.Logger,
|
||||||
) {
|
) {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("reader: stopping")
|
||||||
|
}
|
||||||
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
onStop()
|
onStop()
|
||||||
}()
|
}()
|
||||||
@@ -224,6 +266,9 @@ func RunReader(
|
|||||||
case data, ok := <-conn.Incoming():
|
case data, ok := <-conn.Incoming():
|
||||||
if !ok {
|
if !ok {
|
||||||
// connection has closed
|
// connection has closed
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("reader: disconnected")
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,8 +290,13 @@ func RunStopMonitor(
|
|||||||
onStop func(),
|
onStop func(),
|
||||||
conn *transport.Connection,
|
conn *transport.Connection,
|
||||||
keepalive <-chan struct{},
|
keepalive <-chan struct{},
|
||||||
|
logger *slog.Logger,
|
||||||
) {
|
) {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("stop monitor: stopping")
|
||||||
|
}
|
||||||
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
onStop()
|
onStop()
|
||||||
}()
|
}()
|
||||||
@@ -254,6 +304,9 @@ func RunStopMonitor(
|
|||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
case <-keepalive:
|
case <-keepalive:
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("stop monitor: stopping: keepalive")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,9 +343,13 @@ func RunKeepalive(
|
|||||||
heartbeat <-chan struct{},
|
heartbeat <-chan struct{},
|
||||||
keepalive chan<- struct{},
|
keepalive chan<- struct{},
|
||||||
timeout time.Duration,
|
timeout time.Duration,
|
||||||
|
logger *slog.Logger,
|
||||||
) {
|
) {
|
||||||
// disable keepalive timeout if not configured
|
// disable keepalive timeout if not configured
|
||||||
if timeout <= 0 {
|
if timeout <= 0 {
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("keepalive: disabled")
|
||||||
|
}
|
||||||
// drain heartbeats
|
// drain heartbeats
|
||||||
// wait for cancel and exit
|
// wait for cancel and exit
|
||||||
for {
|
for {
|
||||||
@@ -304,6 +361,10 @@ func RunKeepalive(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("keepalive: enabled", "timeout", timeout)
|
||||||
|
}
|
||||||
|
|
||||||
timer := time.NewTimer(timeout)
|
timer := time.NewTimer(timeout)
|
||||||
defer timer.Stop()
|
defer timer.Stop()
|
||||||
|
|
||||||
@@ -323,6 +384,9 @@ func RunKeepalive(
|
|||||||
// timer completed
|
// timer completed
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
// send keepalive signal, then reset the timer
|
// send keepalive signal, then reset the timer
|
||||||
|
if logger != nil {
|
||||||
|
logger.Info("keepalive: peer is inactive")
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case keepalive <- struct{}{}:
|
case keepalive <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
@@ -359,6 +423,8 @@ func RunDialer(
|
|||||||
|
|
||||||
dial <-chan struct{},
|
dial <-chan struct{},
|
||||||
newConn chan<- *transport.Connection,
|
newConn chan<- *transport.Connection,
|
||||||
|
|
||||||
|
logger *slog.Logger,
|
||||||
) {
|
) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -377,12 +443,18 @@ func RunDialer(
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("dialer: dialing")
|
||||||
|
}
|
||||||
// dial a new connection
|
// dial a new connection
|
||||||
conn, err := connect(id, ctx, pool)
|
conn, err := connect(id, ctx, pool)
|
||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
// send error if dial failed and continue
|
// send error if dial failed and continue
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if logger != nil {
|
||||||
|
logger.Warn("dialer: dial failed")
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case pool.Errors <- err:
|
case pool.Errors <- err:
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -390,6 +462,10 @@ func RunDialer(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if logger != nil {
|
||||||
|
logger.Debug("dialer: connected")
|
||||||
|
}
|
||||||
|
|
||||||
// send the new connection or close and exit
|
// send the new connection or close and exit
|
||||||
select {
|
select {
|
||||||
case newConn <- conn:
|
case newConn <- conn:
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func TestRunDialer(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
go RunDialer(url, ctx, pool, dial, newConn)
|
go RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||||
dial <- struct{}{}
|
dial <- struct{}{}
|
||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
@@ -73,7 +73,7 @@ func TestRunDialer(t *testing.T) {
|
|||||||
ConnectionConfig: connConfig,
|
ConnectionConfig: connConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
go RunDialer(url, ctx, pool, dial, newConn)
|
go RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||||
dial <- struct{}{}
|
dial <- struct{}{}
|
||||||
|
|
||||||
// wait for dial to start blocking on gate
|
// wait for dial to start blocking on gate
|
||||||
@@ -141,7 +141,7 @@ func TestRunDialer(t *testing.T) {
|
|||||||
ConnectionConfig: connConfig,
|
ConnectionConfig: connConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
go RunDialer(url, ctx, pool, dial, newConn)
|
go RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||||
dial <- struct{}{}
|
dial <- struct{}{}
|
||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
@@ -175,7 +175,7 @@ func TestRunDialer(t *testing.T) {
|
|||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
RunDialer(url, ctx, pool, dial, newConn)
|
RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -213,7 +213,7 @@ func TestRunDialer(t *testing.T) {
|
|||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
RunDialer(url, ctx, pool, dial, newConn)
|
RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func TestRunKeepalive(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
go RunKeepalive(ctx, heartbeat, keepalive, timeout)
|
go RunKeepalive(ctx, heartbeat, keepalive, timeout, 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++ {
|
||||||
@@ -41,7 +41,7 @@ func TestRunKeepalive(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
go RunKeepalive(ctx, heartbeat, keepalive, timeout)
|
go RunKeepalive(ctx, heartbeat, keepalive, timeout, nil)
|
||||||
|
|
||||||
// send no heartbeats, wait for timeout and keepalive signal
|
// send no heartbeats, wait for timeout and keepalive signal
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
@@ -62,7 +62,7 @@ func TestRunKeepalive(t *testing.T) {
|
|||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
RunKeepalive(ctx, heartbeat, keepalive, timeout)
|
RunKeepalive(ctx, heartbeat, keepalive, timeout, nil)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ func TestRunKeepalive(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
go RunKeepalive(ctx, heartbeat, keepalive, 0)
|
go RunKeepalive(ctx, heartbeat, keepalive, 0, nil)
|
||||||
|
|
||||||
// these must not block
|
// these must not block
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
for range heartbeat {
|
for range heartbeat {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
incomingData <- honeybeetest.MockIncomingData{
|
incomingData <- honeybeetest.MockIncomingData{
|
||||||
@@ -65,7 +65,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
for range messages {
|
for range messages {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
const count = 3
|
const count = 3
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
@@ -96,7 +96,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
for range messages {
|
for range messages {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
// induce connection closure via reader
|
// induce connection closure via reader
|
||||||
incomingData <- honeybeetest.MockIncomingData{Err: io.EOF}
|
incomingData <- honeybeetest.MockIncomingData{Err: io.EOF}
|
||||||
@@ -125,7 +125,7 @@ func TestRunReader(t *testing.T) {
|
|||||||
heartbeat := make(chan struct{})
|
heartbeat := make(chan struct{})
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ func TestRunStopMonitor(t *testing.T) {
|
|||||||
|
|
||||||
keepalive := make(chan struct{}, 1)
|
keepalive := make(chan struct{}, 1)
|
||||||
|
|
||||||
go RunStopMonitor(ctx, cancel, conn, keepalive)
|
go RunStopMonitor(ctx, cancel, conn, keepalive, nil)
|
||||||
|
|
||||||
keepalive <- struct{}{}
|
keepalive <- struct{}{}
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ func TestRunStopMonitor(t *testing.T) {
|
|||||||
|
|
||||||
keepalive := make(chan struct{})
|
keepalive := make(chan struct{})
|
||||||
|
|
||||||
go RunStopMonitor(ctx, cancel, conn, keepalive)
|
go RunStopMonitor(ctx, cancel, conn, keepalive, nil)
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ func (c *Connection) shutdownSignalDone() {
|
|||||||
|
|
||||||
func (c *Connection) shutdownLogStart() {
|
func (c *Connection) shutdownLogStart() {
|
||||||
if c.logger != nil {
|
if c.logger != nil {
|
||||||
c.logger.Info("shutting down")
|
c.logger.Info("closing")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +260,7 @@ func (c *Connection) shutdownCloseChannels() {
|
|||||||
|
|
||||||
func (c *Connection) shutdownLogComplete() {
|
func (c *Connection) shutdownLogComplete() {
|
||||||
if c.logger != nil {
|
if c.logger != nil {
|
||||||
c.logger.Info("connection closed")
|
c.logger.Info("closed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -161,14 +161,14 @@ func TestCloseLogging(t *testing.T) {
|
|||||||
|
|
||||||
honeybeetest.Eventually(t, func() bool {
|
honeybeetest.Eventually(t, func() bool {
|
||||||
return honeybeetest.FindLogRecord(
|
return honeybeetest.FindLogRecord(
|
||||||
mockHandler.GetRecords(), slog.LevelInfo, "connection closed") != nil
|
mockHandler.GetRecords(), slog.LevelInfo, "closed") != nil
|
||||||
}, "expected log")
|
}, "expected log")
|
||||||
|
|
||||||
records := mockHandler.GetRecords()
|
records := mockHandler.GetRecords()
|
||||||
|
|
||||||
expected := []honeybeetest.ExpectedLog{
|
expected := []honeybeetest.ExpectedLog{
|
||||||
log(slog.LevelInfo, "shutting down", map[string]any{}),
|
log(slog.LevelInfo, "closing", map[string]any{}),
|
||||||
log(slog.LevelInfo, "connection closed", map[string]any{}),
|
log(slog.LevelInfo, "closed", map[string]any{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
honeybeetest.AssertLogSequence(t, records, expected)
|
honeybeetest.AssertLogSequence(t, records, expected)
|
||||||
@@ -197,7 +197,7 @@ func TestCloseLogging(t *testing.T) {
|
|||||||
records := mockHandler.GetRecords()
|
records := mockHandler.GetRecords()
|
||||||
|
|
||||||
expected := []honeybeetest.ExpectedLog{
|
expected := []honeybeetest.ExpectedLog{
|
||||||
log(slog.LevelInfo, "shutting down", map[string]any{}),
|
log(slog.LevelInfo, "closing", map[string]any{}),
|
||||||
log(slog.LevelError, "socket close failed", map[string]any{"error": closeErr}),
|
log(slog.LevelError, "socket close failed", map[string]any{"error": closeErr}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user