Skip to content

Commit c3c52cb

Browse files
committed
wip
Signed-off-by: Gabriele Santomaggio <G.santomaggio@gmail.com>
1 parent 1bf978b commit c3c52cb

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

pkg/stream/blocking_queue.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ import (
1010
var ErrBlockingQueueStopped = errors.New("blocking queue stopped")
1111

1212
type BlockingQueue[T any] struct {
13-
queue chan T
14-
status int32
13+
queue chan T
14+
status int32
15+
capacity int
16+
lastUpdate time.Time
1517
}
1618

1719
// NewBlockingQueue initializes a new BlockingQueue with the given capacity
1820
func NewBlockingQueue[T any](capacity int) *BlockingQueue[T] {
1921
return &BlockingQueue[T]{
20-
queue: make(chan T, capacity),
21-
status: 0,
22+
queue: make(chan T, capacity),
23+
capacity: capacity,
24+
status: 0,
2225
}
2326
}
2427

@@ -27,6 +30,7 @@ func (bq *BlockingQueue[T]) Enqueue(item T) error {
2730
if bq.IsStopped() {
2831
return ErrBlockingQueueStopped
2932
}
33+
bq.lastUpdate = time.Now()
3034
bq.queue <- item
3135

3236
return nil
@@ -59,6 +63,13 @@ func (bq *BlockingQueue[T]) IsEmpty() bool {
5963
return len(bq.queue) == 0
6064
}
6165

66+
func (bq *BlockingQueue[T]) IsReadyToSend() bool {
67+
if bq.lastUpdate.IsZero() {
68+
return true
69+
}
70+
return time.Since(bq.lastUpdate) > 10*time.Millisecond && len(bq.queue) == 0
71+
}
72+
6273
// Stop stops the queue from accepting new items
6374
// but allows some pending items.
6475
// Stop is different from Close in that it allows the

pkg/stream/producer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ func (producer *Producer) closeConfirmationStatus() {
287287
func (producer *Producer) processPendingSequencesQueue() {
288288

289289
maxFrame := producer.options.client.getTuneState().requestedMaxFrameSize
290+
var avarage int32
291+
iterations := 0
292+
290293
// the buffer is initialized with the size of the header
291294
sequenceToSend := make([]*messageSequence, 0)
292295
go func() {
@@ -316,8 +319,16 @@ func (producer *Producer) processPendingSequencesQueue() {
316319

317320
// if producer.pendingSequencesQueue.IsEmpty() means that the queue is empty so the producer is not sending
318321
// the messages during the checks of the buffer. In this case
319-
if producer.pendingSequencesQueue.IsEmpty() || len(sequenceToSend) >= producer.options.BatchSize {
322+
if producer.pendingSequencesQueue.IsReadyToSend() || len(sequenceToSend) >= producer.options.BatchSize {
320323
if len(sequenceToSend) > 0 {
324+
avarage = atomic.AddInt32(&avarage, int32(len(sequenceToSend)))
325+
iterations++
326+
if iterations > 10000 {
327+
logs.LogInfo("producer %d avarage: %d", producer.id, avarage/int32(iterations))
328+
avarage = 0
329+
iterations = 0
330+
}
331+
321332
lastError = producer.internalBatchSend(sequenceToSend)
322333
sequenceToSend = sequenceToSend[:0]
323334
totalBufferToSend += initBufferPublishSize

0 commit comments

Comments
 (0)