-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f82900
commit 99b3995
Showing
5 changed files
with
148 additions
and
76 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
|
||
namespace YesSql.Data; | ||
|
||
internal sealed class WorkDispatcher<TKey, TValue> where TKey : notnull | ||
{ | ||
private readonly ConcurrentDictionary<TKey, Task<TValue?>> _workers = new(); | ||
|
||
public async Task<TValue?> ScheduleAsync(TKey key, Func<TKey, Task<TValue?>> valueFactory) | ||
{ | ||
ArgumentNullException.ThrowIfNull(key); | ||
|
||
while (true) | ||
{ | ||
if (_workers.TryGetValue(key, out var task)) | ||
{ | ||
return await task; | ||
} | ||
|
||
// This is the task that we'll return to all waiters. We'll complete it when the factory is complete | ||
var tcs = new TaskCompletionSource<TValue?>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
||
if (_workers.TryAdd(key, tcs.Task)) | ||
{ | ||
try | ||
{ | ||
var value = await valueFactory(key); | ||
tcs.TrySetResult(value); | ||
return await tcs.Task; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Make sure all waiters see the exception | ||
tcs.SetException(ex); | ||
|
||
throw; | ||
} | ||
finally | ||
{ | ||
// We remove the entry if the factory failed so it's not a permanent failure | ||
// and future gets can retry (this could be a pluggable policy) | ||
_workers.TryRemove(key, out _); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public async Task<TValue?> ScheduleAsync<TState>(TKey key, TState state, Func<TKey, TState, Task<TValue?>> valueFactory) | ||
{ | ||
ArgumentNullException.ThrowIfNull(key); | ||
|
||
while (true) | ||
{ | ||
if (_workers.TryGetValue(key, out var task)) | ||
{ | ||
return await task; | ||
} | ||
|
||
// This is the task that we'll return to all waiters. We'll complete it when the factory is complete | ||
var tcs = new TaskCompletionSource<TValue?>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
||
if (_workers.TryAdd(key, tcs.Task)) | ||
{ | ||
try | ||
{ | ||
var value = await valueFactory(key, state); | ||
tcs.TrySetResult(value); | ||
return await tcs.Task; | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Make sure all waiters see the exception | ||
tcs.SetException(ex); | ||
|
||
throw; | ||
} | ||
finally | ||
{ | ||
// We remove the entry if the factory failed so it's not a permanent failure | ||
// and future gets can retry (this could be a pluggable policy) | ||
_workers.TryRemove(key, out _); | ||
} | ||
} | ||
} | ||
} | ||
} |
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