-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
70 lines (57 loc) · 2.83 KB
/
Copy pathProgram.cs
File metadata and controls
70 lines (57 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Net;
using System.Reflection;
using System.Text;
using Grpc.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http.Resilience;
using Polly;
// A gRPC client set up through Grpc.Net.ClientFactory with AddStandardResilienceHandler().
// The caller cancels while the only attempt is in flight, and that attempt completes with a
// transient HTTP failure (503). Expected: RpcException with StatusCode.Cancelled.
//
// Run as-is to get the Polly version that Microsoft.Extensions.Http.Resilience pins (8.4.2),
// or with -p:PollyVersion=8.7.0 to see the same code on a newer Polly.
var pollyVersion = typeof(ResiliencePipeline).Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
Console.WriteLine($"Polly.Core: {pollyVersion}");
using var cts = new CancellationTokenSource();
var services = new ServiceCollection();
services
.AddGrpcClient<GreeterClient>(options => options.Address = new Uri("https://dummy"))
.ConfigurePrimaryHttpMessageHandler(() => new CancelThenFailHandler(cts))
.AddStandardResilienceHandler();
using var provider = services.BuildServiceProvider();
var client = provider.GetRequiredService<GreeterClient>();
try
{
await client.SayHelloAsync("dummy", cts.Token);
Console.WriteLine("FAIL: the call completed");
return 1;
}
catch (RpcException e)
{
var expected = e.StatusCode == StatusCode.Cancelled;
Console.WriteLine($"{(expected ? "PASS" : "FAIL")}: RpcException with StatusCode.{e.StatusCode}");
return expected ? 0 : 1;
}
// Stands in for a protoc-generated client, so the repro needs no .proto file or Grpc.Tools.
// Grpc.Net.ClientFactory only requires a constructor that takes a CallInvoker.
internal sealed class GreeterClient(CallInvoker invoker)
{
private static readonly Marshaller<string> Utf8 =
Marshallers.Create(value => Encoding.UTF8.GetBytes(value), bytes => Encoding.UTF8.GetString(bytes));
private static readonly Method<string, string> SayHello =
new(MethodType.Unary, "Greeter", "SayHello", Utf8, Utf8);
public Task<string> SayHelloAsync(string name, CancellationToken cancellationToken)
=> invoker.AsyncUnaryCall(SayHello, host: null, new CallOptions(cancellationToken: cancellationToken), name).ResponseAsync;
}
// Cancels the caller's token while the attempt is in flight, then completes that attempt
// with a transient failure the retry strategy is configured to handle. HTTP/2, as gRPC requires.
internal sealed class CancelThenFailHandler(CancellationTokenSource cts) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
cts.Cancel();
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) { Version = HttpVersion.Version20 });
}
}