Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/JsonRpc/RequestRouterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,22 @@ public virtual async Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Re
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();

var id = GetId(request.Id);
var cts = new CancellationTokenSource();
if (!_requests.TryGetValue(id, out var cts))
{
cts = new CancellationTokenSource();
_requests.TryAdd(id, cts);
}
token.Register(cts.Cancel);
_requests.TryAdd(id, cts);

// TODO: Try / catch for Internal Error
try
{
if (cts.IsCancellationRequested)
{
_logger.LogDebug("Request {Id} was cancelled", id);
return new RequestCancelled();
}

// To avoid boxing, the best way to compare generics for equality is with EqualityComparer<T>.Default.
// This respects IEquatable<T> (without boxing) as well as object.Equals, and handles all the Nullable<T> "lifted" nuances.
// https://stackoverflow.com/a/864860
Expand Down Expand Up @@ -176,13 +185,17 @@ public virtual async Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Re

public void CancelRequest(object id)
{
if (_requests.TryGetValue(GetId(id), out var cts))
var idValue = GetId(id);
if (_requests.TryGetValue(idValue, out var cts))
{
cts.Cancel();
}
else
{
_logger.LogDebug("Request {Id} was not found to cancel", id);
cts = new CancellationTokenSource();
_requests.TryAdd(idValue, cts);
cts.Cancel();
_logger.LogDebug("Request {Id} was not found to cancel, stubbing it in.", id);
}
}

Expand Down