added queue optimizations and depth monitoring.

This commit is contained in:
Jay
2026-05-08 14:47:28 -04:00
parent c87a8cce6f
commit 56c2539249
5 changed files with 21 additions and 9 deletions
+7 -4
View File
@@ -13,13 +13,14 @@ func RunQueue(
out chan<- types.ReceivedMessage,
maxQueueSize int,
droppedCount *atomic.Uint64,
bufferDepth *atomic.Int64,
) {
var next types.ReceivedMessage
var queue messageQueue
if maxQueueSize > 0 {
queue = newBoundedRing(maxQueueSize)
} else {
queue = newUnboundedRing(64)
queue = newUnboundedRing(1024)
}
for {
@@ -40,12 +41,15 @@ func RunQueue(
// drop oldest message
_ = queue.pop()
droppedCount.Add(1)
bufferDepth.Add(-1)
}
// add new message
queue.push(msg)
bufferDepth.Add(1)
// send next message to out channel
case outOrNil <- next:
_ = queue.pop()
bufferDepth.Add(-1)
}
}
}
@@ -118,9 +122,8 @@ func newUnboundedRing(initialCap int) *unboundedRing {
func (u *unboundedRing) push(m types.ReceivedMessage) {
if u.size == len(u.buf) {
bigger := make([]types.ReceivedMessage, len(u.buf)*2)
for i := 0; i < u.size; i++ {
bigger[i] = u.buf[(u.head+i)%len(u.buf)]
}
n := copy(bigger, u.buf[u.head:])
copy(bigger[n:], u.buf[:u.head])
u.buf = bigger
u.head = 0
}
+3 -3
View File
@@ -18,7 +18,7 @@ func TestRunQueue(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go RunQueue(id, ctx, inChan, outChan, 0, &atomic.Uint64{})
go RunQueue(id, ctx, inChan, outChan, 0, &atomic.Uint64{}, &atomic.Int64{})
inChan <- types.ReceivedMessage{Data: []byte("hello"), ReceivedAt: time.Now()}
@@ -50,7 +50,7 @@ func TestRunQueue(t *testing.T) {
}
}()
go RunQueue(id, ctx, inChan, gatedInbox, 2, &atomic.Uint64{})
go RunQueue(id, ctx, inChan, gatedInbox, 2, &atomic.Uint64{}, &atomic.Int64{})
// send three messages while the gated inbox is blocked
inChan <- types.ReceivedMessage{Data: []byte("first"), ReceivedAt: time.Now()}
@@ -88,7 +88,7 @@ func TestRunQueue(t *testing.T) {
done := make(chan struct{})
go func() {
RunQueue(id, ctx, inChan, outChan, 0, &atomic.Uint64{})
RunQueue(id, ctx, inChan, outChan, 0, &atomic.Uint64{}, &atomic.Int64{})
close(done)
}()