Added logging to pools and workers.
This commit is contained in:
@@ -146,6 +146,10 @@ func (p *Pool) SetDialer(d types.Dialer) {
|
||||
}
|
||||
|
||||
func (p *Pool) Close() {
|
||||
if p.logger != nil {
|
||||
p.logger.Debug("closing")
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
@@ -165,10 +169,18 @@ func (p *Pool) Close() {
|
||||
close(p.inbox)
|
||||
close(p.events)
|
||||
close(p.errors)
|
||||
|
||||
if p.logger != nil {
|
||||
p.logger.Info("closed")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (p *Pool) Connect(id string) error {
|
||||
if p.logger != nil {
|
||||
p.logger.Debug("connecting to peer", "peer", id)
|
||||
}
|
||||
|
||||
id, err := transport.NormalizeURL(id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -215,10 +227,18 @@ func (p *Pool) Connect(id string) error {
|
||||
|
||||
p.peers[id] = &Peer{id: id, worker: worker}
|
||||
|
||||
if p.logger != nil {
|
||||
p.logger.Info("connected to peer", "peer", id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Pool) Remove(id string) error {
|
||||
if p.logger != nil {
|
||||
p.logger.Debug("disconnecting from peer", "peer", id)
|
||||
}
|
||||
|
||||
id, err := transport.NormalizeURL(id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -239,6 +259,10 @@ func (p *Pool) Remove(id string) error {
|
||||
|
||||
peer.worker.Stop()
|
||||
|
||||
if p.logger != nil {
|
||||
p.logger.Info("disconnected from peer", "peer", id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+80
-4
@@ -57,6 +57,10 @@ func NewWorker(
|
||||
}
|
||||
|
||||
func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
if w.logger != nil {
|
||||
w.logger.Debug("starting")
|
||||
}
|
||||
|
||||
dial := make(chan struct{}, 1)
|
||||
newConn := make(chan *transport.Connection, 1)
|
||||
toQueue := make(chan types.ReceivedMessage, 256)
|
||||
@@ -68,12 +72,12 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
RunDialer(w.id, w.ctx, pool, dial, newConn)
|
||||
RunDialer(w.id, w.ctx, pool, dial, newConn, w.logger)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
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() {
|
||||
@@ -96,14 +100,26 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
dial: dial,
|
||||
keepalive: keepalive,
|
||||
newConn: newConn,
|
||||
logger: w.logger,
|
||||
}
|
||||
session.Start(w.ctx, pool)
|
||||
}()
|
||||
|
||||
if w.logger != nil {
|
||||
w.logger.Info("started")
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if w.logger != nil {
|
||||
w.logger.Info("stopped")
|
||||
}
|
||||
}
|
||||
|
||||
func (w *DefaultWorker) Stop() {
|
||||
if w.logger != nil {
|
||||
w.logger.Debug("shutting down")
|
||||
}
|
||||
w.cancel()
|
||||
}
|
||||
|
||||
@@ -136,6 +152,8 @@ type Session struct {
|
||||
|
||||
keepalive <-chan struct{}
|
||||
newConn <-chan *transport.Connection
|
||||
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (s *Session) Start(
|
||||
@@ -143,6 +161,10 @@ func (s *Session) Start(
|
||||
pool PoolPlugin,
|
||||
) {
|
||||
for {
|
||||
if s.logger != nil {
|
||||
s.logger.Debug("session: requesting connection")
|
||||
}
|
||||
|
||||
// request new connection
|
||||
select {
|
||||
case s.dial <- struct{}{}:
|
||||
@@ -159,9 +181,16 @@ func (s *Session) Start(
|
||||
case <-s.keepalive:
|
||||
select {
|
||||
case s.dial <- struct{}{}:
|
||||
if s.logger != nil {
|
||||
s.logger.Debug("session: requesting connection")
|
||||
}
|
||||
|
||||
default:
|
||||
}
|
||||
case conn = <-s.newConn:
|
||||
if s.logger != nil {
|
||||
s.logger.Debug("session: connected")
|
||||
}
|
||||
break preConn
|
||||
}
|
||||
}
|
||||
@@ -179,16 +208,24 @@ func (s *Session) Start(
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
RunReader(sctx, onStop, conn, s.messages, s.heartbeat)
|
||||
RunReader(sctx, onStop, conn, s.messages, s.heartbeat, s.logger)
|
||||
}()
|
||||
go func() {
|
||||
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
|
||||
wg.Wait()
|
||||
|
||||
if s.logger != nil {
|
||||
s.logger.Info("session: ended")
|
||||
}
|
||||
|
||||
// tear down connection
|
||||
s.connPtr.Store(nil)
|
||||
pool.Events <- PoolEvent{ID: s.id, Kind: EventDisconnected}
|
||||
@@ -211,8 +248,13 @@ func RunReader(
|
||||
conn *transport.Connection,
|
||||
messages chan<- types.ReceivedMessage,
|
||||
heartbeat chan<- struct{},
|
||||
logger *slog.Logger,
|
||||
) {
|
||||
defer func() {
|
||||
if logger != nil {
|
||||
logger.Debug("reader: stopping")
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
onStop()
|
||||
}()
|
||||
@@ -224,6 +266,9 @@ func RunReader(
|
||||
case data, ok := <-conn.Incoming():
|
||||
if !ok {
|
||||
// connection has closed
|
||||
if logger != nil {
|
||||
logger.Debug("reader: disconnected")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -245,8 +290,13 @@ func RunStopMonitor(
|
||||
onStop func(),
|
||||
conn *transport.Connection,
|
||||
keepalive <-chan struct{},
|
||||
logger *slog.Logger,
|
||||
) {
|
||||
defer func() {
|
||||
if logger != nil {
|
||||
logger.Debug("stop monitor: stopping")
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
onStop()
|
||||
}()
|
||||
@@ -254,6 +304,9 @@ func RunStopMonitor(
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-keepalive:
|
||||
if logger != nil {
|
||||
logger.Debug("stop monitor: stopping: keepalive")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,9 +343,13 @@ func RunKeepalive(
|
||||
heartbeat <-chan struct{},
|
||||
keepalive chan<- struct{},
|
||||
timeout time.Duration,
|
||||
logger *slog.Logger,
|
||||
) {
|
||||
// disable keepalive timeout if not configured
|
||||
if timeout <= 0 {
|
||||
if logger != nil {
|
||||
logger.Debug("keepalive: disabled")
|
||||
}
|
||||
// drain heartbeats
|
||||
// wait for cancel and exit
|
||||
for {
|
||||
@@ -304,6 +361,10 @@ func RunKeepalive(
|
||||
}
|
||||
}
|
||||
|
||||
if logger != nil {
|
||||
logger.Debug("keepalive: enabled", "timeout", timeout)
|
||||
}
|
||||
|
||||
timer := time.NewTimer(timeout)
|
||||
defer timer.Stop()
|
||||
|
||||
@@ -323,6 +384,9 @@ func RunKeepalive(
|
||||
// timer completed
|
||||
case <-timer.C:
|
||||
// send keepalive signal, then reset the timer
|
||||
if logger != nil {
|
||||
logger.Info("keepalive: peer is inactive")
|
||||
}
|
||||
select {
|
||||
case keepalive <- struct{}{}:
|
||||
default:
|
||||
@@ -359,6 +423,8 @@ func RunDialer(
|
||||
|
||||
dial <-chan struct{},
|
||||
newConn chan<- *transport.Connection,
|
||||
|
||||
logger *slog.Logger,
|
||||
) {
|
||||
for {
|
||||
select {
|
||||
@@ -377,12 +443,18 @@ func RunDialer(
|
||||
}
|
||||
}()
|
||||
|
||||
if logger != nil {
|
||||
logger.Debug("dialer: dialing")
|
||||
}
|
||||
// dial a new connection
|
||||
conn, err := connect(id, ctx, pool)
|
||||
close(done)
|
||||
|
||||
// send error if dial failed and continue
|
||||
if err != nil {
|
||||
if logger != nil {
|
||||
logger.Warn("dialer: dial failed")
|
||||
}
|
||||
select {
|
||||
case pool.Errors <- err:
|
||||
case <-ctx.Done():
|
||||
@@ -390,6 +462,10 @@ func RunDialer(
|
||||
continue
|
||||
}
|
||||
|
||||
if logger != nil {
|
||||
logger.Debug("dialer: connected")
|
||||
}
|
||||
|
||||
// send the new connection or close and exit
|
||||
select {
|
||||
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{}{}
|
||||
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
@@ -73,7 +73,7 @@ func TestRunDialer(t *testing.T) {
|
||||
ConnectionConfig: connConfig,
|
||||
}
|
||||
|
||||
go RunDialer(url, ctx, pool, dial, newConn)
|
||||
go RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||
dial <- struct{}{}
|
||||
|
||||
// wait for dial to start blocking on gate
|
||||
@@ -141,7 +141,7 @@ func TestRunDialer(t *testing.T) {
|
||||
ConnectionConfig: connConfig,
|
||||
}
|
||||
|
||||
go RunDialer(url, ctx, pool, dial, newConn)
|
||||
go RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||
dial <- struct{}{}
|
||||
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
@@ -175,7 +175,7 @@ func TestRunDialer(t *testing.T) {
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
RunDialer(url, ctx, pool, dial, newConn)
|
||||
RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
@@ -213,7 +213,7 @@ func TestRunDialer(t *testing.T) {
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
RunDialer(url, ctx, pool, dial, newConn)
|
||||
RunDialer(url, ctx, pool, dial, newConn, nil)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ func TestRunKeepalive(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go RunKeepalive(ctx, heartbeat, keepalive, timeout)
|
||||
go RunKeepalive(ctx, heartbeat, keepalive, timeout, nil)
|
||||
|
||||
// send heartbeats faster than the timeout
|
||||
for i := 0; i < 5; i++ {
|
||||
@@ -41,7 +41,7 @@ func TestRunKeepalive(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go RunKeepalive(ctx, heartbeat, keepalive, timeout)
|
||||
go RunKeepalive(ctx, heartbeat, keepalive, timeout, nil)
|
||||
|
||||
// send no heartbeats, wait for timeout and keepalive signal
|
||||
honeybeetest.Eventually(t, func() bool {
|
||||
@@ -62,7 +62,7 @@ func TestRunKeepalive(t *testing.T) {
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
RunKeepalive(ctx, heartbeat, keepalive, timeout)
|
||||
RunKeepalive(ctx, heartbeat, keepalive, timeout, nil)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
@@ -83,7 +83,7 @@ func TestRunKeepalive(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go RunKeepalive(ctx, heartbeat, keepalive, 0)
|
||||
go RunKeepalive(ctx, heartbeat, keepalive, 0, nil)
|
||||
|
||||
// these must not block
|
||||
for i := 0; i < 5; i++ {
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestRunReader(t *testing.T) {
|
||||
for range heartbeat {
|
||||
}
|
||||
}()
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||
|
||||
before := time.Now()
|
||||
incomingData <- honeybeetest.MockIncomingData{
|
||||
@@ -65,7 +65,7 @@ func TestRunReader(t *testing.T) {
|
||||
for range messages {
|
||||
}
|
||||
}()
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||
|
||||
const count = 3
|
||||
for i := 0; i < count; i++ {
|
||||
@@ -96,7 +96,7 @@ func TestRunReader(t *testing.T) {
|
||||
for range messages {
|
||||
}
|
||||
}()
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||
|
||||
// induce connection closure via reader
|
||||
incomingData <- honeybeetest.MockIncomingData{Err: io.EOF}
|
||||
@@ -125,7 +125,7 @@ func TestRunReader(t *testing.T) {
|
||||
heartbeat := make(chan struct{})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat)
|
||||
go RunReader(ctx, cancel, conn, messages, heartbeat, nil)
|
||||
|
||||
cancel()
|
||||
|
||||
@@ -152,7 +152,7 @@ func TestRunStopMonitor(t *testing.T) {
|
||||
|
||||
keepalive := make(chan struct{}, 1)
|
||||
|
||||
go RunStopMonitor(ctx, cancel, conn, keepalive)
|
||||
go RunStopMonitor(ctx, cancel, conn, keepalive, nil)
|
||||
|
||||
keepalive <- struct{}{}
|
||||
|
||||
@@ -177,7 +177,7 @@ func TestRunStopMonitor(t *testing.T) {
|
||||
|
||||
keepalive := make(chan struct{})
|
||||
|
||||
go RunStopMonitor(ctx, cancel, conn, keepalive)
|
||||
go RunStopMonitor(ctx, cancel, conn, keepalive, nil)
|
||||
|
||||
cancel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user