File tree 2 files changed +29
-14
lines changed
2 files changed +29
-14
lines changed Original file line number Diff line number Diff line change @@ -584,7 +584,16 @@ func (q *BufferedChannelQueue) Put(val interface{}) error {
584
584
// return ErrQueueIsClosed
585
585
// }
586
586
//
587
- // return q.blockingQueue.Put(val)
587
+ // q.lock.Lock()
588
+ // poolCount := q.pool.Count()
589
+ //
590
+ // // If appearing nothing in the pool
591
+ // if poolCount == 0 {
592
+ // defer q.lock.Unlock()
593
+ // // Try channel
594
+ // return q.blockingQueue.Put(val)
595
+ // }
596
+ // q.lock.Unlock()
588
597
589
598
return q .Offer (val )
590
599
}
@@ -638,23 +647,31 @@ func (q *BufferedChannelQueue) Offer(val interface{}) error {
638
647
return ErrQueueIsClosed
639
648
}
640
649
650
+ poolCount := q .pool .Count ()
651
+
652
+ // If appearing nothing in the pool
653
+ if poolCount == 0 {
654
+ // Try channel
655
+ err := q .blockingQueue .Offer (val )
656
+ if err == nil {
657
+ // Success
658
+ return nil
659
+ } else if err == ErrQueueIsFull {
660
+ // Do nothing and let pool.Offer(val)
661
+ } else {
662
+ // Other
663
+ return err
664
+ }
665
+ }
666
+
641
667
// Before +1: >=, After +1: >
642
- if q . pool . Count () >= q .bufferSizeMaximum {
668
+ if poolCount >= q .bufferSizeMaximum {
643
669
return ErrQueueIsFull
644
670
}
645
671
646
672
q .pool .Offer (val )
647
673
q .loadWorkerCh .Offer (1 )
648
674
return nil
649
-
650
- // err := q.blockingQueue.Offer(val)
651
- // if err == ErrQueueIsFull {
652
- // q.pool.Offer(val)
653
- // q.loadWorkerCh.Offer(1)
654
- // return nil
655
- // }
656
- //
657
- // return err
658
675
}
659
676
660
677
// Poll Poll the val(non-blocking)
Original file line number Diff line number Diff line change @@ -275,13 +275,10 @@ func TestNewBufferedChannelQueue(t *testing.T) {
275
275
276
276
err = queue .Offer (1 )
277
277
assert .Equal (t , nil , err )
278
- time .Sleep (1 * timeout )
279
278
err = queue .Offer (2 )
280
279
assert .Equal (t , nil , err )
281
- time .Sleep (1 * timeout )
282
280
err = queue .Offer (3 )
283
281
assert .Equal (t , nil , err )
284
- time .Sleep (1 * timeout )
285
282
// Channel: only 3 positions & Buffer: 1 position, now `4` is inserted into the buffer(buffer size: 1)
286
283
err = queue .Offer (4 )
287
284
assert .Equal (t , nil , err )
@@ -319,6 +316,7 @@ func TestNewBufferedChannelQueue(t *testing.T) {
319
316
go func () {
320
317
for i := 1 ; i <= 10000 ; i ++ {
321
318
// err := bufferedChannelQueue.PutWithTimeout(i, timeout)
319
+ // err := bufferedChannelQueue.Put(i)
322
320
err := bufferedChannelQueue .Offer (i )
323
321
assert .Equal (t , nil , err )
324
322
}
You can’t perform that action at this time.
0 commit comments