Skip to content

Commit 120c467

Browse files
committed
Reject too large elements to the queue
Without this change, the requests without deadline will block forever until the space is available which will never be. Signed-off-by: Bogdan Drutu <[email protected]>
1 parent cf18559 commit 120c467

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

exporter/exporterhelper/internal/queuebatch/memory_queue.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var blockingDonePool = sync.Pool{
2121
}
2222

2323
var errInvalidSize = errors.New("invalid element size")
24+
var errSizeTooLarge = errors.New("element size too large")
2425

2526
// memoryQueueSettings defines internal parameters for boundedMemoryQueue creation.
2627
type memoryQueueSettings[T any] struct {
@@ -74,6 +75,11 @@ func (mq *memoryQueue[T]) Offer(ctx context.Context, el T) error {
7475
return errInvalidSize
7576
}
7677

78+
// If element larger than the capacity, will never been able to add it.
79+
if elSize > mq.cap {
80+
return errSizeTooLarge
81+
}
82+
7783
done, err := mq.add(ctx, el, elSize)
7884
if err != nil {
7985
return err

exporter/exporterhelper/internal/queuebatch/memory_queue_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func TestMemoryQueueOfferInvalidSize(t *testing.T) {
9797
require.ErrorIs(t, q.Offer(context.Background(), -1), errInvalidSize)
9898
}
9999

100+
func TestMemoryQueueRejectOverCapacityElements(t *testing.T) {
101+
q := newMemoryQueue[int64](memoryQueueSettings[int64]{sizer: sizerInt64{}, capacity: 7, blockOnOverflow: true})
102+
require.ErrorIs(t, q.Offer(context.Background(), 8), errSizeTooLarge)
103+
}
104+
100105
func TestMemoryQueueOfferZeroSize(t *testing.T) {
101106
q := newMemoryQueue[int64](memoryQueueSettings[int64]{sizer: sizerInt64{}, capacity: 1})
102107
require.NoError(t, q.Offer(context.Background(), 0))

0 commit comments

Comments
 (0)