Skip to content

Commit

Permalink
Merged changes #352
Browse files Browse the repository at this point in the history
  • Loading branch information
rstaib committed Jan 9, 2019
2 parents 862e9d5 + 194c394 commit ad0eafd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
31 changes: 20 additions & 11 deletions src/Core/Core/Execution/Middleware/RequestTimeoutMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution.Configuration;
Expand Down Expand Up @@ -26,33 +27,41 @@ public RequestTimeoutMiddleware(

public async Task InvokeAsync(IQueryContext context)
{
var requestTimeoutCts = new CancellationTokenSource(
_options.ExecutionTimeout);
CancellationTokenSource requestTimeoutCts = null;
CancellationTokenSource combinedCts = null;

CancellationToken requestAborted = context.RequestAborted;

if (!Debugger.IsAttached)
{
requestTimeoutCts = new CancellationTokenSource(
_options.ExecutionTimeout);

combinedCts = CancellationTokenSource.CreateLinkedTokenSource(
requestTimeoutCts.Token,
context.RequestAborted);

context.RequestAborted = combinedCts.Token;
}

try
{
using (var combinedCts = CancellationTokenSource
.CreateLinkedTokenSource(requestTimeoutCts.Token,
context.RequestAborted))
{
context.RequestAborted = combinedCts.Token;
await _next(context);
}
await _next(context);
}
catch (TaskCanceledException ex)
{
if (!requestTimeoutCts.IsCancellationRequested)
if (requestAborted.IsCancellationRequested)
{
throw;
}

context.Exception = ex;
context.Result = QueryResult.CreateError(new QueryError(
"Execution timeout has been exceeded."));
return;
}
finally
{
combinedCts.Dispose();
requestTimeoutCts.Dispose();
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Core/Core/Execution/QueryExecuter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ private IRequestServiceScope CreateServiceScope(
IServiceScope serviceScope =
_applicationServices.CreateScope();

IServiceProvider services = serviceScope.ServiceProvider.Include(
requestServices ?? Schema.Services);
IServiceProvider services = requestServices ?? Schema.Services;

services = services == null
? serviceScope.ServiceProvider
: serviceScope.ServiceProvider.Include(requestServices);

return new RequestServiceScope(services, serviceScope);
}
Expand Down

0 comments on commit ad0eafd

Please sign in to comment.