File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -10,15 +10,18 @@ import (
10
10
var ErrBlockingQueueStopped = errors .New ("blocking queue stopped" )
11
11
12
12
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
15
17
}
16
18
17
19
// NewBlockingQueue initializes a new BlockingQueue with the given capacity
18
20
func NewBlockingQueue [T any ](capacity int ) * BlockingQueue [T ] {
19
21
return & BlockingQueue [T ]{
20
- queue : make (chan T , capacity ),
21
- status : 0 ,
22
+ queue : make (chan T , capacity ),
23
+ capacity : capacity ,
24
+ status : 0 ,
22
25
}
23
26
}
24
27
@@ -27,6 +30,7 @@ func (bq *BlockingQueue[T]) Enqueue(item T) error {
27
30
if bq .IsStopped () {
28
31
return ErrBlockingQueueStopped
29
32
}
33
+ bq .lastUpdate = time .Now ()
30
34
bq .queue <- item
31
35
32
36
return nil
@@ -59,6 +63,13 @@ func (bq *BlockingQueue[T]) IsEmpty() bool {
59
63
return len (bq .queue ) == 0
60
64
}
61
65
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
+
62
73
// Stop stops the queue from accepting new items
63
74
// but allows some pending items.
64
75
// Stop is different from Close in that it allows the
Original file line number Diff line number Diff line change @@ -287,6 +287,9 @@ func (producer *Producer) closeConfirmationStatus() {
287
287
func (producer * Producer ) processPendingSequencesQueue () {
288
288
289
289
maxFrame := producer .options .client .getTuneState ().requestedMaxFrameSize
290
+ var avarage int32
291
+ iterations := 0
292
+
290
293
// the buffer is initialized with the size of the header
291
294
sequenceToSend := make ([]* messageSequence , 0 )
292
295
go func () {
@@ -316,8 +319,16 @@ func (producer *Producer) processPendingSequencesQueue() {
316
319
317
320
// if producer.pendingSequencesQueue.IsEmpty() means that the queue is empty so the producer is not sending
318
321
// 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 {
320
323
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
+
321
332
lastError = producer .internalBatchSend (sequenceToSend )
322
333
sequenceToSend = sequenceToSend [:0 ]
323
334
totalBufferToSend += initBufferPublishSize
You can’t perform that action at this time.
0 commit comments