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{
|
||||
|
||||
Reference in New Issue
Block a user