@@ -97,13 +97,14 @@ type Bulker struct {
97
97
}
98
98
99
99
const (
100
- defaultFlushInterval = time .Second * 5
101
- defaultFlushThresholdCnt = 32768
102
- defaultFlushThresholdSz = 1024 * 1024 * 10
103
- defaultMaxPending = 32
104
- defaultBlockQueueSz = 32 // Small capacity to allow multiOp to spin fast
105
- defaultAPIKeyMaxParallel = 32
106
- defaultApikeyMaxReqSize = 100 * 1024 * 1024
100
+ defaultFlushInterval = time .Second * 5
101
+ defaultFlushThresholdCnt = 32768
102
+ defaultFlushThresholdSz = 1024 * 1024 * 10
103
+ defaultMaxPending = 32
104
+ defaultBlockQueueSz = 32 // Small capacity to allow multiOp to spin fast
105
+ defaultAPIKeyMaxParallel = 32
106
+ defaultApikeyMaxReqSize = 100 * 1024 * 1024
107
+ defaultFlushContextTimeout = time .Minute * 1
107
108
)
108
109
109
110
func NewBulker (es esapi.Transport , tracer * apm.Tracer , opts ... BulkOpt ) * Bulker {
@@ -416,6 +417,7 @@ func (b *Bulker) Run(ctx context.Context) error {
416
417
Int ("itemCnt" , itemCnt ).
417
418
Int ("byteCnt" , byteCnt ).
418
419
Msg ("Flush on timer" )
420
+
419
421
err = doFlush ()
420
422
421
423
case <- ctx .Done ():
@@ -443,7 +445,11 @@ func (b *Bulker) flushQueue(ctx context.Context, w *semaphore.Weighted, queue qu
443
445
Str ("queue" , queue .Type ()).
444
446
Msg ("flushQueue Wait" )
445
447
446
- if err := w .Acquire (ctx , 1 ); err != nil {
448
+ acquireCtx , cancel := context .WithTimeout (ctx , defaultFlushContextTimeout )
449
+ defer cancel ()
450
+
451
+ if err := w .Acquire (acquireCtx , 1 ); err != nil {
452
+ zerolog .Ctx (ctx ).Error ().Err (err ).Msg ("flushQueue Wait error" )
447
453
return err
448
454
}
449
455
@@ -458,6 +464,10 @@ func (b *Bulker) flushQueue(ctx context.Context, w *semaphore.Weighted, queue qu
458
464
go func () {
459
465
start := time .Now ()
460
466
467
+ // deadline prevents bulker being blocked on flush
468
+ flushCtx , cancel := context .WithTimeout (ctx , defaultFlushContextTimeout )
469
+ defer cancel ()
470
+
461
471
if b .tracer != nil {
462
472
trans := b .tracer .StartTransaction (fmt .Sprintf ("Flush queue %s" , queue .Type ()), "bulker" )
463
473
trans .Context .SetLabel ("queue.size" , queue .cnt )
@@ -471,13 +481,13 @@ func (b *Bulker) flushQueue(ctx context.Context, w *semaphore.Weighted, queue qu
471
481
var err error
472
482
switch queue .ty {
473
483
case kQueueRead , kQueueRefreshRead :
474
- err = b .flushRead (ctx , queue )
484
+ err = b .flushRead (flushCtx , queue )
475
485
case kQueueSearch , kQueueFleetSearch :
476
- err = b .flushSearch (ctx , queue )
486
+ err = b .flushSearch (flushCtx , queue )
477
487
case kQueueAPIKeyUpdate :
478
- err = b .flushUpdateAPIKey (ctx , queue )
488
+ err = b .flushUpdateAPIKey (flushCtx , queue )
479
489
default :
480
- err = b .flushBulk (ctx , queue )
490
+ err = b .flushBulk (flushCtx , queue )
481
491
}
482
492
483
493
if err != nil {
@@ -502,8 +512,12 @@ func (b *Bulker) flushQueue(ctx context.Context, w *semaphore.Weighted, queue qu
502
512
func failQueue (queue queueT , err error ) {
503
513
for n := queue .head ; n != nil ; {
504
514
next := n .next // 'n' is invalid immediately on channel send
505
- n .ch <- respT {
515
+ select {
516
+ case n .ch <- respT {
506
517
err : err ,
518
+ }:
519
+ default :
520
+ panic ("Unexpected blocked response channel on failQueue" )
507
521
}
508
522
n = next
509
523
}
0 commit comments