-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor request invocation to be extensible. #641
Merged
david-driscoll
merged 4 commits into
OmniSharp:master
from
NTaylorMullen:nimullen/refactor.requestinvocation
Aug 27, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
using System; | ||
using System.Reactive; | ||
using System.Reactive.Concurrency; | ||
using System.Reactive.Disposables; | ||
using System.Reactive.Linq; | ||
using Microsoft.Extensions.Logging; | ||
using OmniSharp.Extensions.JsonRpc.Server; | ||
using OmniSharp.Extensions.JsonRpc.Server.Messages; | ||
using Notification = OmniSharp.Extensions.JsonRpc.Server.Notification; | ||
|
||
namespace OmniSharp.Extensions.JsonRpc | ||
{ | ||
public class DefaultRequestInvoker : RequestInvoker | ||
{ | ||
private readonly IRequestRouter<IHandlerDescriptor?> _requestRouter; | ||
private readonly IOutputHandler _outputHandler; | ||
private readonly ProcessScheduler _processScheduler; | ||
private readonly IRequestProcessIdentifier _requestProcessIdentifier; | ||
private readonly RequestInvokerOptions _options; | ||
private readonly ILogger<DefaultRequestInvoker> _logger; | ||
|
||
public DefaultRequestInvoker( | ||
IRequestRouter<IHandlerDescriptor?> requestRouter, | ||
IOutputHandler outputHandler, | ||
IRequestProcessIdentifier requestProcessIdentifier, | ||
RequestInvokerOptions options, | ||
ILoggerFactory loggerFactory, | ||
IScheduler scheduler) | ||
{ | ||
_requestRouter = requestRouter; | ||
_outputHandler = outputHandler; | ||
_requestProcessIdentifier = requestProcessIdentifier; | ||
_options = options; | ||
_processScheduler = new ProcessScheduler(loggerFactory, _options.SupportContentModified, _options.Concurrency, scheduler); | ||
_logger = loggerFactory.CreateLogger<DefaultRequestInvoker>(); | ||
} | ||
|
||
public override RequestInvocationHandle InvokeRequest(IRequestDescriptor<IHandlerDescriptor?> descriptor, Request request) | ||
{ | ||
if (descriptor.Default is null) | ||
{ | ||
throw new ArgumentNullException(nameof(descriptor.Default)); | ||
} | ||
|
||
var handle = new RequestInvocationHandle(request); | ||
var type = _requestProcessIdentifier.Identify(descriptor.Default); | ||
|
||
var schedulerDelegate = RouteRequest(descriptor, request, handle); | ||
_processScheduler.Add(type, $"{request.Method}:{request.Id}", schedulerDelegate); | ||
|
||
return handle; | ||
} | ||
|
||
public override void InvokeNotification(IRequestDescriptor<IHandlerDescriptor?> descriptor, Notification notification) | ||
{ | ||
if (descriptor.Default is null) | ||
{ | ||
throw new ArgumentNullException(nameof(descriptor.Default)); | ||
} | ||
|
||
var type = _requestProcessIdentifier.Identify(descriptor.Default); | ||
var schedulerDelegate = RouteNotification(descriptor, notification); | ||
_processScheduler.Add(type, notification.Method, schedulerDelegate); | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
_processScheduler.Dispose(); | ||
} | ||
|
||
private SchedulerDelegate RouteRequest( | ||
IRequestDescriptor<IHandlerDescriptor?> descriptor, | ||
Request request, | ||
RequestInvocationHandle handle) | ||
{ | ||
var cts = handle.CancellationTokenSource; | ||
return (contentModifiedToken, scheduler) => | ||
Observable.Create<ErrorResponse>( | ||
observer => { | ||
// ITS A RACE! | ||
var sub = Observable.Amb( | ||
contentModifiedToken.Select( | ||
_ => { | ||
_logger.LogTrace( | ||
"Request {Id} was abandoned due to content be modified", request.Id | ||
); | ||
return new ErrorResponse( | ||
new ContentModified(request.Id, request.Method) | ||
); | ||
} | ||
), | ||
Observable.Timer(_options.RequestTimeout, scheduler).Select( | ||
_ => new ErrorResponse(new RequestCancelled(request.Id, request.Method)) | ||
), | ||
Observable.FromAsync( | ||
async ct => { | ||
using var timer = _logger.TimeDebug( | ||
"Processing request {Method} {ResponseId}", request.Method, | ||
request.Id | ||
); | ||
ct.Register(cts.Cancel); | ||
// ObservableToToken(contentModifiedToken).Register(cts.Cancel); | ||
try | ||
{ | ||
var result = await _requestRouter.RouteRequest( | ||
descriptor, request, cts.Token | ||
).ConfigureAwait(false); | ||
return result; | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
_logger.LogTrace("Request {Id} was cancelled", request.Id); | ||
return new RequestCancelled(request.Id, request.Method); | ||
} | ||
catch (RpcErrorException e) | ||
{ | ||
_logger.LogCritical( | ||
Events.UnhandledRequest, e, | ||
"Failed to handle request {Method} {RequestId}", request.Method, | ||
request.Id | ||
); | ||
return new RpcError( | ||
request.Id, request.Method, | ||
new ErrorMessage(e.Code, e.Message, e.Error) | ||
); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogCritical( | ||
Events.UnhandledRequest, e, | ||
"Failed to handle request {Method} {RequestId}", request.Method, | ||
request.Id | ||
); | ||
return new InternalError(request.Id, request.Method, e.ToString()); | ||
} | ||
} | ||
) | ||
) | ||
.Subscribe(observer); | ||
return new CompositeDisposable(sub, handle); | ||
} | ||
) | ||
.Select( | ||
response => { | ||
_outputHandler.Send(response.Value); | ||
return Unit.Default; | ||
} | ||
); | ||
} | ||
|
||
private SchedulerDelegate RouteNotification( | ||
IRequestDescriptor<IHandlerDescriptor?> descriptors, | ||
Notification notification) => | ||
(_, scheduler) => | ||
// ITS A RACE! | ||
Observable.Amb( | ||
Observable.Timer(_options.RequestTimeout, scheduler) | ||
.Select(_ => Unit.Default) | ||
.Do( | ||
_ => _logger.LogTrace("Notification was cancelled due to timeout") | ||
), | ||
Observable.FromAsync( | ||
async ct => { | ||
using var timer = _logger.TimeDebug("Processing notification {Method}", notification.Method); | ||
try | ||
{ | ||
await _requestRouter.RouteNotification(descriptors, notification, ct).ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
_logger.LogTrace("Notification was cancelled"); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogCritical(Events.UnhandledRequest, e, "Failed to handle request {Method}", notification.Method); | ||
} | ||
} | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All this is verbatim from the original RouteRequest method in InputHandler.