Skip to content

Refactor deadline to apply more gracefully #301

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Grpc.AspNetCore.Server.Internal;
using Microsoft.AspNetCore.Http;

namespace Grpc.AspNetCore.Microbenchmarks
{
public class DeadlineUnaryServerCallHandlerBenchmark : UnaryServerCallHandlerBenchmarkBase
{
protected override void ConfigureHttpContext(DefaultHttpContext httpContext)
{
httpContext.Request.Headers[GrpcProtocolConstants.TimeoutHeader] = "1H";
}

[Benchmark]
public Task DeadlineHandleCallAsync()
{
return InvokeUnaryRequestAsync();
}
}
}
11 changes: 7 additions & 4 deletions perf/Grpc.AspNetCore.Microbenchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#endregion

using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Running;

namespace Grpc.AspNetCore.Microbenchmarks
Expand All @@ -31,18 +33,19 @@ static void Main(string[] args)
// Profiling option. This will call methods explicitly, in-process
static async Task Main(string[] args)
{
UnaryServerCallHandlerBenchmark benchmark = new UnaryServerCallHandlerBenchmark();
var benchmark = new DeadlineUnaryServerCallHandlerBenchmark();
benchmark.GlobalSetup();
for (int i = 0; i < 100; i++)
Console.WriteLine("Warming up.");
for (int i = 0; i < 100000; i++)
{
await benchmark.HandleCallAsync();
await benchmark.DeadlineHandleCallAsync();
}

Console.WriteLine("Press any key to start.");
Console.ReadKey();
for (int i = 0; i < 1; i++)
{
await benchmark.HandleCallAsync();
await benchmark.DeadlineHandleCallAsync();
}

Console.WriteLine("Done. Press any key to exit.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#endregion

using System;
using System.Buffers;
using System.IO;
using System.IO.Pipelines;
Expand Down Expand Up @@ -88,6 +89,11 @@ public void GlobalSetup()
{
Trailers = _trailers
});
ConfigureHttpContext(_httpContext);
}

protected virtual void ConfigureHttpContext(DefaultHttpContext httpContext)
{
}

protected Task InvokeUnaryRequestAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,67 +71,48 @@ public ClientStreamingServerCallHandler(
}
}

protected override async Task HandleCallAsyncCore(HttpContext httpContext)
protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
{
// Disable request body data rate for client streaming
DisableMinRequestBodyDataRate(httpContext);

var serverCallContext = CreateServerCallContext(httpContext);

GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

TResponse? response = null;

try
if (_pipelineInvoker == null)
{
serverCallContext.Initialize();

if (_pipelineInvoker == null)
var activator = httpContext.RequestServices.GetRequiredService<IGrpcServiceActivator<TService>>();
TService? service = null;
try
{
var activator = httpContext.RequestServices.GetRequiredService<IGrpcServiceActivator<TService>>();
TService? service = null;
try
{
service = activator.Create();
response = await _invoker(
service,
new HttpContextStreamReader<TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
serverCallContext);
}
finally
{
if (service != null)
{
activator.Release(service);
}
}
}
else
{
response = await _pipelineInvoker(
service = activator.Create();
response = await _invoker(
service,
new HttpContextStreamReader<TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
serverCallContext);
}

if (response == null)
finally
{
// This is consistent with Grpc.Core when a null value is returned
throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
if (service != null)
{
activator.Release(service);
}
}

var responseBodyWriter = httpContext.Response.BodyWriter;
await responseBodyWriter.WriteMessageAsync(response, serverCallContext, Method.ResponseMarshaller.Serializer);

await serverCallContext.EndCallAsync();
}
catch (Exception ex)
else
{
serverCallContext.ProcessHandlerError(ex, Method.Name);
response = await _pipelineInvoker(
new HttpContextStreamReader<TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
serverCallContext);
}
finally

if (response == null)
{
serverCallContext.Dispose();
// This is consistent with Grpc.Core when a null value is returned
throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
}

var responseBodyWriter = httpContext.Response.BodyWriter;
await responseBodyWriter.WriteMessageAsync(response, serverCallContext, Method.ResponseMarshaller.Serializer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,57 +72,38 @@ await _invoker(
}
}

protected override async Task HandleCallAsyncCore(HttpContext httpContext)
protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpContextServerCallContext serverCallContext)
{
// Disable request body data rate for client streaming
DisableMinRequestBodyDataRate(httpContext);

var serverCallContext = CreateServerCallContext(httpContext);

GrpcProtocolHelpers.AddProtocolHeaders(httpContext.Response);

try
if (_pipelineInvoker == null)
{
serverCallContext.Initialize();

if (_pipelineInvoker == null)
{
var activator = httpContext.RequestServices.GetRequiredService<IGrpcServiceActivator<TService>>();
TService? service = null;
try
{
service = activator.Create();
await _invoker(
service,
new HttpContextStreamReader<TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
new HttpContextStreamWriter<TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
serverCallContext);
}
finally
{
if (service != null)
{
activator.Release(service);
}
}
}
else
var activator = httpContext.RequestServices.GetRequiredService<IGrpcServiceActivator<TService>>();
TService? service = null;
try
{
await _pipelineInvoker(
service = activator.Create();
await _invoker(
service,
new HttpContextStreamReader<TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
new HttpContextStreamWriter<TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
serverCallContext);
}

await serverCallContext.EndCallAsync();
}
catch (Exception ex)
{
serverCallContext.ProcessHandlerError(ex, Method.Name);
finally
{
if (service != null)
{
activator.Release(service);
}
}
}
finally
else
{
serverCallContext.Dispose();
await _pipelineInvoker(
new HttpContextStreamReader<TRequest>(serverCallContext, Method.RequestMarshaller.Deserializer),
new HttpContextStreamWriter<TResponse>(serverCallContext, Method.ResponseMarshaller.Serializer),
serverCallContext);
}
}
}
Expand Down
Loading