-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: refactor to use file-scoped namespaces; rename internal classes…
…/properties for consistent style
- Loading branch information
1 parent
c45da45
commit a5b7154
Showing
12 changed files
with
537 additions
and
538 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
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
11 changes: 0 additions & 11 deletions
11
ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/APIType.cs
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
ShopifySharp/Infrastructure/Policies/LeakyBucketPolicy/ApiType.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.Infrastructure.Policies.LeakyBucketPolicy; | ||
|
||
internal enum ApiType : byte | ||
{ | ||
RestAdmin, | ||
GraphQlAdmin, | ||
GraphQlPartner, | ||
} |
77 changes: 38 additions & 39 deletions
77
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 |
---|---|---|
@@ -1,62 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ShopifySharp | ||
namespace ShopifySharp; | ||
|
||
internal class ContextAwareQueue<T> | ||
{ | ||
internal class ContextAwareQueue<T> | ||
private Queue<T> _BackgroundQueue { get; set; }= new Queue<T>(); | ||
|
||
private Queue<T> _ForegroundQueue { get; set;} = new Queue<T>(); | ||
|
||
private readonly Func<RequestContext> _getContext; | ||
|
||
public int Count => _BackgroundQueue.Count + _ForegroundQueue.Count; | ||
|
||
public ContextAwareQueue(Func<RequestContext> getContext) | ||
{ | ||
private Queue<T> _BackgroundQueue { get; set; }= new Queue<T>(); | ||
_getContext = getContext; | ||
} | ||
|
||
private Queue<T> _ForegroundQueue { get; set;} = new Queue<T>(); | ||
public void Enqueue(T i) | ||
{ | ||
(_getContext() == RequestContext.Background ? _BackgroundQueue : _ForegroundQueue).Enqueue(i); | ||
} | ||
|
||
private readonly Func<RequestContext> _getContext; | ||
public T Peek() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Peek() : _BackgroundQueue.Peek(); | ||
|
||
public int Count => _BackgroundQueue.Count + _ForegroundQueue.Count; | ||
public T Dequeue() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Dequeue() : _BackgroundQueue.Dequeue(); | ||
|
||
public ContextAwareQueue(Func<RequestContext> getContext) | ||
/// Removes the item and updates the queue. | ||
public void RemoveAndUpdateQueue(T itemToRemove) | ||
{ | ||
if (_ForegroundQueue.Contains(itemToRemove)) | ||
{ | ||
_getContext = getContext; | ||
_ForegroundQueue = CopyQueueExcludingItem(_ForegroundQueue, itemToRemove); | ||
return; | ||
} | ||
|
||
public void Enqueue(T i) | ||
if (_BackgroundQueue.Contains(itemToRemove)) | ||
{ | ||
(_getContext() == RequestContext.Background ? _BackgroundQueue : _ForegroundQueue).Enqueue(i); | ||
_BackgroundQueue = CopyQueueExcludingItem(_BackgroundQueue, itemToRemove); | ||
} | ||
} | ||
|
||
public T Peek() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Peek() : _BackgroundQueue.Peek(); | ||
|
||
public T Dequeue() => _ForegroundQueue.Count > 0 ? _ForegroundQueue.Dequeue() : _BackgroundQueue.Dequeue(); | ||
/// Copies the items in <paramref name="existingQueue"/> to a new queue, excluding <paramref name="itemToExclude"/>. | ||
private static Queue<T> CopyQueueExcludingItem(Queue<T> existingQueue, T itemToExclude) | ||
{ | ||
var newQueue = new Queue<T>(); | ||
|
||
/// Removes the item and updates the queue. | ||
public void RemoveAndUpdateQueue(T itemToRemove) | ||
while (existingQueue.Count > 0) | ||
{ | ||
if (_ForegroundQueue.Contains(itemToRemove)) | ||
var itemToAdd = existingQueue.Dequeue(); | ||
if (!itemToAdd.Equals(itemToExclude)) | ||
{ | ||
_ForegroundQueue = CopyQueueExcludingItem(_ForegroundQueue, itemToRemove); | ||
return; | ||
} | ||
|
||
if (_BackgroundQueue.Contains(itemToRemove)) | ||
{ | ||
_BackgroundQueue = CopyQueueExcludingItem(_BackgroundQueue, itemToRemove); | ||
newQueue.Enqueue(itemToAdd); | ||
} | ||
} | ||
|
||
/// Copies the items in <paramref name="existingQueue"/> to a new queue, excluding <paramref name="itemToExclude"/>. | ||
private static Queue<T> CopyQueueExcludingItem(Queue<T> existingQueue, T itemToExclude) | ||
{ | ||
var newQueue = new Queue<T>(); | ||
|
||
while (existingQueue.Count > 0) | ||
{ | ||
var itemToAdd = existingQueue.Dequeue(); | ||
if (!itemToAdd.Equals(itemToExclude)) | ||
{ | ||
newQueue.Enqueue(itemToAdd); | ||
} | ||
} | ||
|
||
return newQueue; | ||
} | ||
return newQueue; | ||
} | ||
} |
Oops, something went wrong.