Skip to content

QueueProcessingOrder.NewestFirst WaitAsync should drop the oldest item when the queue is full #62817

@Tratcher

Description

@Tratcher

Description

QueueProcessingOrder.NewestFirst is used for a stack pattern (FILO), so the newest entry in line always gets processed next. However, when the queue fills up it starts refusing to add new entries, keeping older entries in the queue.

Instead, it should add the new entry to the front and drop the oldest entry from the back.

Reproduction Steps

        var limiter = new System.Threading.RateLimiting.ConcurrencyLimiter(
            new System.Threading.RateLimiting.ConcurrencyLimiterOptions(
                permitLimit: 1, queueProcessingOrder: QueueProcessingOrder.NewestFirst, queueLimit: 2));

        var lease1 = limiter.Acquire(); // Succeeds, exhausts permitLimit.
        var lease2Task = limiter.WaitAsync(); // Incomplete, queued
        var lease3Task = limiter.WaitAsync(); // Incomplete, queued, exhausts queueLimit.

        var lease4Task = limiter.WaitAsync();
        // Expected: Incomplete, lease2Task completes without acquiring.
        // Actual: Completes immediately without acquiring. lease2Task remains queued.

Expected behavior

WaitAsync should queue a new item and drop the oldest item when the queue is full

Actual behavior

WaitAsync does not queue a new item and maintains the oldest item when the queue is full

Regression?

No response

Known Workarounds

No response

Configuration

.NET 7

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions