Skip to content

Commit

Permalink
Optimize deadline logic (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Nov 11, 2020
1 parent e7e779f commit dd4dca5
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 409 deletions.
1 change: 1 addition & 0 deletions perf/benchmarkapps/GrpcClient/ClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ public class ClientOptions
public int ResponseSize { get; set; }
public GrpcClientType GrpcClientType { get; set; }
public int Streams { get; set; }
public int Deadline { get; set; }
}
}
20 changes: 17 additions & 3 deletions perf/benchmarkapps/GrpcClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static async Task<int> Main(string[] args)
rootCommand.AddOption(new Option<GrpcClientType>(new string[] { "--grpcClientType" }, () => GrpcClientType.GrpcNetClient, "Whether to use Grpc.NetClient or Grpc.Core client"));
rootCommand.AddOption(new Option<int>(new string[] { "--streams" }, () => 1, "Maximum concurrent streams per connection"));
rootCommand.AddOption(new Option<bool>(new string[] { "--enableCertAuth" }, () => false, "Flag indicating whether client sends a client certificate"));
rootCommand.AddOption(new Option<int>(new string[] { "--deadline" }, "Duration of deadline in seconds"));

rootCommand.Handler = CommandHandler.Create<ClientOptions>(async (options) =>
{
Expand Down Expand Up @@ -519,13 +520,24 @@ private static void HandleError(int connectionId)
}
}

private static CallOptions CreateCallOptions()
{
var callOptions = new CallOptions();
if (_options.Deadline > 0)
{
callOptions = callOptions.WithDeadline(DateTime.UtcNow.AddSeconds(_options.Deadline));
}

return callOptions;
}

private static async Task PingPongStreaming(CancellationTokenSource cts, int connectionId, int streamId)
{
Log(connectionId, streamId, $"Starting {_options.Scenario}");

var client = new BenchmarkService.BenchmarkServiceClient(_channels[connectionId]);
var request = CreateSimpleRequest();
using var call = client.StreamingCall();
using var call = client.StreamingCall(CreateCallOptions());

while (!cts.IsCancellationRequested)
{
Expand Down Expand Up @@ -570,7 +582,9 @@ private static async Task ServerStreamingCall(CancellationTokenSource cts, int c
Log(connectionId, streamId, $"Starting {_options.Scenario}");

var client = new BenchmarkService.BenchmarkServiceClient(_channels[connectionId]);
using var call = client.StreamingFromServer(CreateSimpleRequest(), cancellationToken: cts.Token);
var callOptions = CreateCallOptions();
callOptions.WithCancellationToken(cts.Token);
using var call = client.StreamingFromServer(CreateSimpleRequest(), callOptions);

while (!cts.IsCancellationRequested)
{
Expand Down Expand Up @@ -622,7 +636,7 @@ private static async Task UnaryCall(CancellationTokenSource cts, int connectionI
try
{
var start = DateTime.UtcNow;
var response = await client.UnaryCallAsync(CreateSimpleRequest());
var response = await client.UnaryCallAsync(CreateSimpleRequest(), CreateCallOptions());
var end = DateTime.UtcNow;

ReceivedDateTime(start, end, connectionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,12 @@ protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpC
throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
}

if (serverCallContext.DeadlineManager != null && serverCallContext.DeadlineManager.CancellationToken.IsCancellationRequested)
// Check if deadline exceeded while method was invoked. If it has then skip trying to write
// the response message because it will always fail.
// Note that the call is still going so the deadline could still be exceeded after this point.
if (serverCallContext.DeadlineManager?.IsDeadlineExceededStarted ?? false)
{
// The cancellation token has been raised. Ensure that any DeadlineManager tasks have
// been completed before continuing.
await serverCallContext.DeadlineManager.CancellationProcessedTask;

// There is no point trying to write to the response because it has been finished.
if (serverCallContext.DeadlineManager.CallComplete)
{
return;
}
return;
}

var responseBodyWriter = httpContext.Response.BodyWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ protected override async Task HandleCallAsyncCore(HttpContext httpContext, HttpC
throw new RpcException(new Status(StatusCode.Cancelled, "No message returned from method."));
}

if (serverCallContext.DeadlineManager != null && serverCallContext.DeadlineManager.CancellationToken.IsCancellationRequested)
// Check if deadline exceeded while method was invoked. If it has then skip trying to write
// the response message because it will always fail.
// Note that the call is still going so the deadline could still be exceeded after this point.
if (serverCallContext.DeadlineManager?.IsDeadlineExceededStarted ?? false)
{
// The cancellation token has been raised. Ensure that any DeadlineManager tasks have
// been completed before continuing.
await serverCallContext.DeadlineManager.CancellationProcessedTask;

// There is no point trying to write to the response because it has been finished.
if (serverCallContext.DeadlineManager.CallComplete)
{
return;
}
return;
}

var responseBodyWriter = httpContext.Response.BodyWriter;
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit dd4dca5

Please sign in to comment.