-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #672 from clement911/master
- Loading branch information
Showing
6 changed files
with
96 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/ContextAwareQueue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ShopifySharp | ||
{ | ||
internal class ContextAwareQueue<T> | ||
{ | ||
private readonly Queue<T> _BackgroundQueue = new Queue<T>(); | ||
|
||
private readonly Queue<T> _ForegroundQueue = new Queue<T>(); | ||
|
||
private readonly Func<RequestContext> _getContext; | ||
|
||
public int Count => _BackgroundQueue.Count + _ForegroundQueue.Count; | ||
|
||
public ContextAwareQueue(Func<RequestContext> getContext) | ||
{ | ||
_getContext = getContext; | ||
} | ||
|
||
public void Enqueue(T i) | ||
{ | ||
(_getContext() == RequestContext.Background ? _BackgroundQueue : _ForegroundQueue).Enqueue(i); | ||
} | ||
|
||
public T Peek() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Peek() : _BackgroundQueue.Peek(); | ||
|
||
public T Dequeue() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Dequeue() : _BackgroundQueue.Dequeue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/RequestContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace ShopifySharp | ||
{ | ||
public enum RequestContext | ||
{ | ||
Foreground, | ||
Background | ||
} | ||
} |