introduce statistics collection
This commit is contained in:
+84
-12
@@ -7,6 +7,7 @@ import (
|
||||
"git.wisehodl.dev/jay/go-honeybee/types"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -24,6 +25,23 @@ 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
|
||||
}
|
||||
|
||||
type InboxMessage struct {
|
||||
ID string
|
||||
Data []byte
|
||||
@@ -35,6 +53,7 @@ type PoolPlugin struct {
|
||||
Inbox chan<- InboxMessage
|
||||
Events chan<- PoolEvent
|
||||
Errors chan<- error
|
||||
InboxCounter *atomic.Uint64
|
||||
Dialer types.Dialer
|
||||
ConnectionConfig *transport.ConnectionConfig
|
||||
Handler slog.Handler
|
||||
@@ -58,6 +77,9 @@ type Pool struct {
|
||||
events chan PoolEvent
|
||||
errors chan error
|
||||
|
||||
inboxCounter *atomic.Uint64
|
||||
outgoingCount *atomic.Uint64
|
||||
|
||||
dialer types.Dialer
|
||||
config *PoolConfig
|
||||
handler slog.Handler
|
||||
@@ -101,17 +123,19 @@ 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),
|
||||
dialer: transport.NewDialer(),
|
||||
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{},
|
||||
dialer: transport.NewDialer(),
|
||||
config: config,
|
||||
handler: handler,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -138,6 +162,47 @@ 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(),
|
||||
})
|
||||
}
|
||||
|
||||
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(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *Pool) SetDialer(d types.Dialer) {
|
||||
if d == nil {
|
||||
panic("dialer cannot be nil")
|
||||
@@ -214,6 +279,7 @@ func (p *Pool) Connect(id string) error {
|
||||
Inbox: p.inbox,
|
||||
Events: p.events,
|
||||
Errors: p.errors,
|
||||
InboxCounter: p.inboxCounter,
|
||||
Dialer: p.dialer,
|
||||
ConnectionConfig: p.config.ConnectionConfig,
|
||||
Handler: p.handler,
|
||||
@@ -284,5 +350,11 @@ func (p *Pool) Send(id string, data []byte) error {
|
||||
return NewPoolError(ErrPeerNotFound)
|
||||
}
|
||||
|
||||
return peer.worker.Send(data)
|
||||
err = peer.worker.Send(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.outgoingCount.Add(1)
|
||||
return nil
|
||||
}
|
||||
|
||||
+84
-18
@@ -18,16 +18,41 @@ type Worker interface {
|
||||
Start(pool PoolPlugin)
|
||||
Stop()
|
||||
Send(data []byte) error
|
||||
Stats() WorkerStats
|
||||
}
|
||||
|
||||
type WorkerStats struct {
|
||||
IncomingAvailable bool
|
||||
ChanIncoming int
|
||||
ChanQueue int
|
||||
ChanForwarder int
|
||||
|
||||
ConnectionAvailable bool
|
||||
Connection transport.ConnectionStats
|
||||
|
||||
TotalProcessed uint64
|
||||
TotalDropped uint64
|
||||
TotalSent uint64
|
||||
TotalRestarts uint64
|
||||
}
|
||||
|
||||
type DefaultWorker struct {
|
||||
id string
|
||||
conn atomic.Pointer[transport.Connection]
|
||||
heartbeat chan struct{}
|
||||
config *WorkerConfig
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
logger *slog.Logger
|
||||
id string
|
||||
conn atomic.Pointer[transport.Connection]
|
||||
|
||||
heartbeat chan struct{}
|
||||
toQueue chan types.ReceivedMessage
|
||||
toForwarder chan types.ReceivedMessage
|
||||
|
||||
processedCount *atomic.Uint64
|
||||
droppedCount *atomic.Uint64
|
||||
outgoingCount *atomic.Uint64
|
||||
restartCount *atomic.Uint64
|
||||
|
||||
config *WorkerConfig
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewWorker(
|
||||
@@ -45,12 +70,18 @@ func NewWorker(
|
||||
|
||||
wctx, wcancel := context.WithCancel(ctx)
|
||||
w := &DefaultWorker{
|
||||
id: id,
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
ctx: wctx,
|
||||
cancel: wcancel,
|
||||
logger: logger,
|
||||
id: id,
|
||||
config: config,
|
||||
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{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
ctx: wctx,
|
||||
cancel: wcancel,
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
return w, nil
|
||||
@@ -63,8 +94,6 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
|
||||
dial := make(chan struct{}, 1)
|
||||
newConn := make(chan *transport.Connection, 1)
|
||||
toQueue := make(chan types.ReceivedMessage, 256)
|
||||
toForwarder := make(chan types.ReceivedMessage, 256)
|
||||
keepalive := make(chan struct{}, 1)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
@@ -82,12 +111,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() {
|
||||
@@ -95,12 +124,13 @@ func (w *DefaultWorker) Start(pool PoolPlugin) {
|
||||
session := &Session{
|
||||
id: w.id,
|
||||
connPtr: &w.conn,
|
||||
messages: toQueue,
|
||||
messages: w.toQueue,
|
||||
heartbeat: w.heartbeat,
|
||||
dial: dial,
|
||||
keepalive: keepalive,
|
||||
newConn: newConn,
|
||||
reconnectDelay: w.config.ReconnectDelay,
|
||||
restartCount: w.restartCount,
|
||||
logger: w.logger,
|
||||
}
|
||||
session.Start(w.ctx, pool)
|
||||
@@ -140,9 +170,39 @@ func (w *DefaultWorker) Send(data []byte) error {
|
||||
case <-w.ctx.Done():
|
||||
}
|
||||
|
||||
w.outgoingCount.Add(1)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *DefaultWorker) Stats() WorkerStats {
|
||||
connectionAvailable := false
|
||||
incomingLen := 0
|
||||
connStats := transport.ConnectionStats{}
|
||||
|
||||
conn := w.conn.Load()
|
||||
if conn != nil {
|
||||
connectionAvailable = true
|
||||
incomingLen = len(conn.Incoming())
|
||||
connStats = conn.Stats()
|
||||
}
|
||||
|
||||
return WorkerStats{
|
||||
IncomingAvailable: connectionAvailable,
|
||||
ChanIncoming: incomingLen,
|
||||
ChanQueue: len(w.toQueue),
|
||||
ChanForwarder: len(w.toForwarder),
|
||||
|
||||
ConnectionAvailable: connectionAvailable,
|
||||
Connection: connStats,
|
||||
|
||||
TotalProcessed: w.processedCount.Load(),
|
||||
TotalDropped: w.droppedCount.Load(),
|
||||
TotalRestarts: w.restartCount.Load(),
|
||||
TotalSent: w.outgoingCount.Load(),
|
||||
}
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
id string
|
||||
connPtr *atomic.Pointer[transport.Connection]
|
||||
@@ -155,6 +215,7 @@ type Session struct {
|
||||
newConn <-chan *transport.Connection
|
||||
|
||||
reconnectDelay time.Duration
|
||||
restartCount *atomic.Uint64
|
||||
|
||||
logger *slog.Logger
|
||||
}
|
||||
@@ -246,6 +307,7 @@ func (s *Session) Start(
|
||||
|
||||
// refresh session
|
||||
time.Sleep(s.reconnectDelay)
|
||||
s.restartCount.Add(1)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -346,6 +408,8 @@ func RunForwarder(
|
||||
ctx context.Context,
|
||||
messages <-chan types.ReceivedMessage,
|
||||
inbox chan<- InboxMessage,
|
||||
workerProcessedCount *atomic.Uint64,
|
||||
poolInboxCount *atomic.Uint64,
|
||||
) {
|
||||
for {
|
||||
select {
|
||||
@@ -364,6 +428,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()}
|
||||
|
||||
|
||||
@@ -20,10 +20,11 @@ func TestWorkerSend(t *testing.T) {
|
||||
heartbeatCount := atomic.Int32{}
|
||||
|
||||
w := &DefaultWorker{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
heartbeat: heartbeat,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
heartbeat: heartbeat,
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
}
|
||||
w.conn.Store(conn)
|
||||
defer w.cancel()
|
||||
@@ -64,10 +65,11 @@ func TestWorkerSend(t *testing.T) {
|
||||
heartbeatCount := atomic.Int32{}
|
||||
|
||||
w := &DefaultWorker{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
heartbeat: heartbeat,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
heartbeat: heartbeat,
|
||||
outgoingCount: &atomic.Uint64{},
|
||||
}
|
||||
w.conn.Store(conn)
|
||||
defer w.cancel()
|
||||
|
||||
@@ -211,13 +211,14 @@ func TestRunSessionDisconnect(t *testing.T) {
|
||||
events := make(chan PoolEvent, 10)
|
||||
pool := PoolPlugin{Events: events}
|
||||
session := &Session{
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
restartCount: &atomic.Uint64{},
|
||||
}
|
||||
|
||||
go session.Start(ctx, pool)
|
||||
@@ -237,13 +238,14 @@ func TestRunSessionDisconnect(t *testing.T) {
|
||||
events := make(chan PoolEvent, 10)
|
||||
pool := PoolPlugin{Events: events}
|
||||
session := &Session{
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
restartCount: &atomic.Uint64{},
|
||||
}
|
||||
|
||||
go session.Start(ctx, pool)
|
||||
@@ -266,13 +268,14 @@ func TestRunSessionDisconnect(t *testing.T) {
|
||||
events := make(chan PoolEvent, 10)
|
||||
pool := PoolPlugin{Events: events}
|
||||
session := &Session{
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
restartCount: &atomic.Uint64{},
|
||||
}
|
||||
|
||||
go session.Start(ctx, pool)
|
||||
@@ -303,13 +306,14 @@ func TestRunSessionDisconnect(t *testing.T) {
|
||||
events := make(chan PoolEvent, 10)
|
||||
pool := PoolPlugin{Events: events}
|
||||
session := &Session{
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
id: v.id,
|
||||
connPtr: v.connPtr,
|
||||
messages: v.messages,
|
||||
heartbeat: v.heartbeat,
|
||||
dial: v.dial,
|
||||
keepalive: v.keepalive,
|
||||
newConn: v.newConn,
|
||||
restartCount: &atomic.Uint64{},
|
||||
}
|
||||
|
||||
go session.Start(ctx, pool)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -25,9 +26,10 @@ func makeWorkerContext(t *testing.T) (
|
||||
events = make(chan PoolEvent, 10)
|
||||
errors = make(chan error, 10)
|
||||
pool = PoolPlugin{
|
||||
Inbox: inbox,
|
||||
Events: events,
|
||||
Errors: errors,
|
||||
Inbox: inbox,
|
||||
Events: events,
|
||||
Errors: errors,
|
||||
InboxCounter: &atomic.Uint64{},
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -38,11 +40,17 @@ func makeWorker(t *testing.T, ctx context.Context, cancel context.CancelFunc) *D
|
||||
WithReconnectDelay(0 * time.Second),
|
||||
)
|
||||
return &DefaultWorker{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
heartbeat: make(chan struct{}),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
id: "wss://test",
|
||||
config: config,
|
||||
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{},
|
||||
restartCount: &atomic.Uint64{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user