introduce statistics collection
This commit is contained in:
+96
-21
@@ -8,6 +8,7 @@ import (
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -36,6 +37,24 @@ type PoolEvent struct {
|
||||
Kind PoolEventKind
|
||||
}
|
||||
|
||||
type PoolStats struct {
|
||||
ChanInbox int
|
||||
ChanEvents int
|
||||
ChanErrors int
|
||||
|
||||
TotalReceived uint64
|
||||
TotalSent uint64
|
||||
|
||||
PeerCount int
|
||||
PeerStats []PeerStats
|
||||
}
|
||||
|
||||
type PeerStats struct {
|
||||
ID string
|
||||
Worker WorkerStats
|
||||
Connection transport.ConnectionStats
|
||||
}
|
||||
|
||||
type InboxMessage struct {
|
||||
ID string
|
||||
Data []byte
|
||||
@@ -43,11 +62,12 @@ type InboxMessage struct {
|
||||
}
|
||||
|
||||
type PoolPlugin struct {
|
||||
Inbox chan<- InboxMessage
|
||||
Events chan<- PoolEvent
|
||||
Errors chan<- error
|
||||
OnExit OnExitFunction
|
||||
Handler slog.Handler
|
||||
Inbox chan<- InboxMessage
|
||||
Events chan<- PoolEvent
|
||||
Errors chan<- error
|
||||
InboxCounter *atomic.Uint64
|
||||
OnExit OnExitFunction
|
||||
Handler slog.Handler
|
||||
}
|
||||
|
||||
// Pool
|
||||
@@ -70,6 +90,9 @@ type Pool struct {
|
||||
events chan PoolEvent
|
||||
errors chan error
|
||||
|
||||
inboxCounter *atomic.Uint64
|
||||
outgoingCount *atomic.Uint64
|
||||
|
||||
config *PoolConfig
|
||||
handler slog.Handler
|
||||
logger *slog.Logger
|
||||
@@ -116,16 +139,18 @@ func NewPool(ctx context.Context, id string, config *PoolConfig, handler slog.Ha
|
||||
}
|
||||
|
||||
return &Pool{
|
||||
ctx: pctx,
|
||||
cancel: cancel,
|
||||
id: id,
|
||||
peers: make(map[string]*Peer),
|
||||
inbox: make(chan InboxMessage, config.InboxBufferSize),
|
||||
events: make(chan PoolEvent, config.EventsBufferSize),
|
||||
errors: make(chan error, config.ErrorsBufferSize),
|
||||
config: config,
|
||||
handler: handler,
|
||||
logger: logger,
|
||||
ctx: pctx,
|
||||
cancel: cancel,
|
||||
id: id,
|
||||
peers: make(map[string]*Peer),
|
||||
inbox: make(chan InboxMessage, config.InboxBufferSize),
|
||||
events: make(chan PoolEvent, config.EventsBufferSize),
|
||||
errors: make(chan error, config.ErrorsBufferSize),
|
||||
inboxCounter: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
config: config,
|
||||
handler: handler,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -153,6 +178,49 @@ func (p *Pool) Errors() <-chan error {
|
||||
return p.errors
|
||||
}
|
||||
|
||||
func (p *Pool) Stats() PoolStats {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
count := len(p.peers)
|
||||
peerStats := make([]PeerStats, 0, count)
|
||||
for id, peer := range p.peers {
|
||||
peerStats = append(peerStats, PeerStats{
|
||||
ID: id,
|
||||
Worker: peer.worker.Stats(),
|
||||
Connection: peer.conn.Stats(),
|
||||
})
|
||||
}
|
||||
|
||||
return PoolStats{
|
||||
ChanInbox: len(p.inbox),
|
||||
ChanEvents: len(p.events),
|
||||
ChanErrors: len(p.errors),
|
||||
|
||||
TotalReceived: p.inboxCounter.Load(),
|
||||
TotalSent: p.outgoingCount.Load(),
|
||||
|
||||
PeerCount: len(p.peers),
|
||||
PeerStats: peerStats,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pool) PeerStats(id string) (PeerStats, error) {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
peer, exists := p.peers[id]
|
||||
if !exists {
|
||||
return PeerStats{}, ErrPeerNotFound
|
||||
}
|
||||
|
||||
return PeerStats{
|
||||
ID: id,
|
||||
Worker: peer.worker.Stats(),
|
||||
Connection: peer.conn.Stats(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Pool) Close() {
|
||||
if p.logger != nil {
|
||||
p.logger.Debug("closing")
|
||||
@@ -266,7 +334,13 @@ func (p *Pool) Send(id string, data []byte) error {
|
||||
return ErrPeerNotFound
|
||||
}
|
||||
|
||||
return peer.worker.Send(data)
|
||||
err := peer.worker.Send(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.outgoingCount.Add(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
// addLocked constructs and registers a peer. Caller must hold p.mu write lock.
|
||||
@@ -315,11 +389,12 @@ func (p *Pool) addLocked(id string, socket types.Socket) error {
|
||||
}
|
||||
|
||||
pool := PoolPlugin{
|
||||
Inbox: p.inbox,
|
||||
Events: p.events,
|
||||
Errors: p.errors,
|
||||
OnExit: onExit,
|
||||
Handler: p.handler,
|
||||
Inbox: p.inbox,
|
||||
Events: p.events,
|
||||
Errors: p.errors,
|
||||
InboxCounter: p.inboxCounter,
|
||||
OnExit: onExit,
|
||||
Handler: p.handler,
|
||||
}
|
||||
|
||||
peer := &Peer{
|
||||
|
||||
+60
-20
@@ -8,6 +8,7 @@ import (
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -15,6 +16,7 @@ type Worker interface {
|
||||
Start(pool PoolPlugin)
|
||||
Stop()
|
||||
Send(data []byte) error
|
||||
Stats() WorkerStats
|
||||
}
|
||||
|
||||
type WorkerExitKind string
|
||||
@@ -26,14 +28,32 @@ const (
|
||||
ExitPolicy WorkerExitKind = "policy"
|
||||
)
|
||||
|
||||
type WorkerStats struct {
|
||||
ChanIncoming int
|
||||
ChanQueue int
|
||||
ChanForwarder int
|
||||
|
||||
TotalProcessed uint64
|
||||
TotalDropped uint64
|
||||
TotalSent uint64
|
||||
}
|
||||
|
||||
type DefaultWorker struct {
|
||||
id string
|
||||
conn *transport.Connection
|
||||
heartbeat chan struct{}
|
||||
config *WorkerConfig
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
logger *slog.Logger
|
||||
id string
|
||||
conn *transport.Connection
|
||||
|
||||
heartbeat chan struct{}
|
||||
toQueue chan types.ReceivedMessage
|
||||
toForwarder chan types.ReceivedMessage
|
||||
|
||||
processedCount *atomic.Uint64
|
||||
droppedCount *atomic.Uint64
|
||||
outgoingCount *atomic.Uint64
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
config *WorkerConfig
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewWorker(
|
||||
@@ -52,13 +72,18 @@ func NewWorker(
|
||||
|
||||
wctx, cancel := context.WithCancel(ctx)
|
||||
return &DefaultWorker{
|
||||
id: id,
|
||||
conn: conn,
|
||||
heartbeat: make(chan struct{}),
|
||||
config: config,
|
||||
ctx: wctx,
|
||||
cancel: cancel,
|
||||
logger: logger,
|
||||
id: id,
|
||||
conn: conn,
|
||||
heartbeat: make(chan struct{}),
|
||||
toQueue: make(chan types.ReceivedMessage, 256),
|
||||
toForwarder: make(chan types.ReceivedMessage, 256),
|
||||
processedCount: &atomic.Uint64{},
|
||||
droppedCount: &atomic.Uint64{},
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
config: config,
|
||||
ctx: wctx,
|
||||
cancel: cancel,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -67,15 +92,12 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
w.logger.Debug("starting")
|
||||
}
|
||||
|
||||
toQueue := make(chan types.ReceivedMessage, 256)
|
||||
toForwarder := make(chan types.ReceivedMessage, 256)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(5)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
RunReader(w.ctx, pool.OnExit, w.conn, toQueue, w.heartbeat, w.logger)
|
||||
RunReader(w.ctx, pool.OnExit, w.conn, w.toQueue, w.heartbeat, w.logger)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
@@ -85,12 +107,12 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
queue.RunQueue(w.id, w.ctx, toQueue, toForwarder, w.config.MaxQueueSize)
|
||||
queue.RunQueue(w.id, w.ctx, w.toQueue, w.toForwarder, w.config.MaxQueueSize, w.droppedCount)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
RunForwarder(w.id, w.ctx, toForwarder, pool.Inbox)
|
||||
RunForwarder(w.id, w.ctx, w.toForwarder, pool.Inbox, w.processedCount, pool.InboxCounter)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
@@ -126,9 +148,23 @@ func (w *DefaultWorker) Send(data []byte) error {
|
||||
case <-w.ctx.Done():
|
||||
}
|
||||
|
||||
w.outgoingCount.Add(1)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *DefaultWorker) Stats() WorkerStats {
|
||||
return WorkerStats{
|
||||
ChanIncoming: len(w.conn.Incoming()),
|
||||
ChanQueue: len(w.toQueue),
|
||||
ChanForwarder: len(w.toForwarder),
|
||||
|
||||
TotalProcessed: w.processedCount.Load(),
|
||||
TotalDropped: w.droppedCount.Load(),
|
||||
TotalSent: w.outgoingCount.Load(),
|
||||
}
|
||||
}
|
||||
|
||||
func RunReader(
|
||||
ctx context.Context,
|
||||
onPeerClose OnExitFunction,
|
||||
@@ -217,6 +253,8 @@ func RunForwarder(
|
||||
ctx context.Context,
|
||||
messages <-chan types.ReceivedMessage,
|
||||
inbox chan<- InboxMessage,
|
||||
workerProcessedCount *atomic.Uint64,
|
||||
poolInboxCount *atomic.Uint64,
|
||||
) {
|
||||
for {
|
||||
select {
|
||||
@@ -235,6 +273,8 @@ func RunForwarder(
|
||||
Data: msg.Data,
|
||||
ReceivedAt: msg.ReceivedAt,
|
||||
}:
|
||||
workerProcessedCount.Add(1)
|
||||
poolInboxCount.Add(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"git.wisehodl.dev/jay/go-honeybee/honeybeetest"
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -16,7 +17,7 @@ func TestRunForwarder(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go RunForwarder(id, ctx, messages, inbox)
|
||||
go RunForwarder(id, ctx, messages, inbox, &atomic.Uint64{}, &atomic.Uint64{})
|
||||
|
||||
messages <- types.ReceivedMessage{Data: []byte("hello"), ReceivedAt: time.Now()}
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ func setupWorkerTest(t *testing.T) workerTestVars {
|
||||
OnExit: func(kind WorkerExitKind) {
|
||||
once.Do(func() { exitKind.Store(kind) })
|
||||
},
|
||||
InboxCounter: &atomic.Uint64{},
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
Reference in New Issue
Block a user