-
Notifications
You must be signed in to change notification settings - Fork 5
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 #125 from Poltuu/dep_chanel
UP dependencies
- Loading branch information
Showing
11 changed files
with
34 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,31 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
using System.Threading.Channels; | ||
using System.Threading.Tasks; | ||
|
||
namespace Harpoon.Background | ||
{ | ||
internal class BackgroundQueue<T> | ||
{ | ||
private readonly ConcurrentQueue<T> _workItems = new ConcurrentQueue<T>(); | ||
private readonly SemaphoreSlim _signal = new SemaphoreSlim(0); | ||
private readonly Channel<T> _channel = Channel.CreateUnbounded<T>(new UnboundedChannelOptions | ||
{ | ||
SingleReader = true, | ||
SingleWriter = true | ||
}); | ||
|
||
public void QueueWebHook(T workItem) | ||
public ValueTask QueueWebHookAsync(T workItem) | ||
{ | ||
if (workItem == null) | ||
{ | ||
throw new ArgumentException(nameof(workItem)); | ||
} | ||
|
||
_workItems.Enqueue(workItem); | ||
_signal.Release(); | ||
return _channel.Writer.WriteAsync(workItem); | ||
} | ||
|
||
public async Task<T> DequeueAsync(CancellationToken cancellationToken) | ||
public ValueTask<T> DequeueAsync(CancellationToken cancellationToken) | ||
{ | ||
await _signal.WaitAsync(cancellationToken); | ||
_workItems.TryDequeue(out var workItem); | ||
|
||
return workItem; | ||
return _channel.Reader.ReadAsync(cancellationToken); | ||
} | ||
} | ||
} |
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
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
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
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