Skip to content

Commit

Permalink
fix: decrease queue once on reject
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagodaraujo committed Mar 27, 2024
1 parent 28fd483 commit bb61dd7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
7 changes: 2 additions & 5 deletions src/Farfetch.LoadShedding/Tasks/ConcurrentCounter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Farfetch.LoadShedding.Tasks
namespace Farfetch.LoadShedding.Tasks
{
internal class ConcurrentCounter
{
Expand Down Expand Up @@ -64,10 +64,7 @@ public int Decrement()
{
lock (this._locker)
{
if (this._count > 0)
{
this._count--;
}
this._count--;

return this._count;
}
Expand Down
27 changes: 16 additions & 11 deletions src/Farfetch.LoadShedding/Tasks/TaskQueue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
Expand All @@ -9,7 +9,7 @@ namespace Farfetch.LoadShedding.Tasks
{
internal class TaskQueue
{
private readonly ConcurrentCounter _counter = new ConcurrentCounter();
private readonly ConcurrentCounter _counter = new();

private readonly IDictionary<Priority, TaskItemList> _queues = new SortedDictionary<Priority, TaskItemList>()
{
Expand Down Expand Up @@ -80,11 +80,7 @@ private int EnqueueItem(TaskItem item)
{
this._queues[item.Priority].Add(item);

var count = this._counter.Increment();

this.OnItemEnqueued?.Invoke(count, item);

return count;
return IncrementCounter(item);
}

private void RejectLastItem()
Expand All @@ -99,16 +95,25 @@ private void RejectLastItem()
return;
}

this._counter.Decrement();

this.DecrementCounter(lastItem);

lastItem.Reject();
}

private void DecrementCounter(TaskItem nextQueueItem)
private int IncrementCounter(TaskItem item)
{
this.OnItemDequeued?.Invoke(this._counter.Decrement(), nextQueueItem);
var count = this._counter.Increment();
this.OnItemEnqueued?.Invoke(count, item);

return count;
}

private int DecrementCounter(TaskItem nextQueueItem)
{
var count = this._counter.Decrement();
this.OnItemDequeued?.Invoke(count, nextQueueItem);

return count;
}
}
}

0 comments on commit bb61dd7

Please sign in to comment.