Skip to content

Commit 560e141

Browse files
authored
Add retrier example app (#1284)
1 parent 4c410dc commit 560e141

File tree

11 files changed

+377
-0
lines changed

11 files changed

+377
-0
lines changed

examples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,11 @@ Code-first is a good choice if an app is written entirely in .NET. Code contract
260260
* Configure [protobuf-net.Grpc](https://github.com/protobuf-net/protobuf-net.Grpc)
261261
* Create a code-first gRPC service
262262
* Create a code-first gRPC client
263+
264+
## [Retrier](./Retrier)
265+
266+
The retrier example shows how to configure a client to use gRPC retries to retry failed calls. gRPC retries enables resilient, fault tolerant gRPC apps in .NET.
267+
268+
##### Scenarios:
269+
270+
* Configure [gRPC retires](https://docs.microsoft.com/aspnet/core/grpc/retries)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Protobuf Include="..\Proto\retry.proto" GrpcServices="Client" Link="Protos\retry.proto" />
10+
11+
<PackageReference Include="Grpc.Net.Client" Version="$(GrpcDotNetPackageVersion)" />
12+
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
13+
<PackageReference Include="Grpc.Tools" Version="$(GrpcPackageVersion)" PrivateAssets="All" />
14+
</ItemGroup>
15+
16+
</Project>

examples/Retrier/Client/Program.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Threading.Tasks;
22+
using Grpc.Core;
23+
using Grpc.Net.Client;
24+
using Grpc.Net.Client.Configuration;
25+
using Retry;
26+
27+
namespace Client
28+
{
29+
public class Program
30+
{
31+
static async Task Main(string[] args)
32+
{
33+
using var channel = CreateChannel();
34+
var client = new Retrier.RetrierClient(channel);
35+
36+
await UnaryRetry(client);
37+
38+
Console.WriteLine("Shutting down");
39+
Console.WriteLine("Press any key to exit...");
40+
Console.ReadKey();
41+
}
42+
43+
private static async Task UnaryRetry(Retrier.RetrierClient client)
44+
{
45+
Console.WriteLine("Delivering packages...");
46+
foreach (var product in Products)
47+
{
48+
try
49+
{
50+
var package = new Package { Name = product };
51+
var call = client.DeliverPackageAsync(package);
52+
var response = await call;
53+
54+
#region Print success
55+
Console.ForegroundColor = ConsoleColor.Green;
56+
Console.Write(response.Message);
57+
Console.ResetColor();
58+
Console.Write(" " + await GetRetryCount(call.ResponseHeadersAsync));
59+
Console.WriteLine();
60+
#endregion
61+
}
62+
catch (RpcException ex)
63+
{
64+
#region Print failure
65+
Console.ForegroundColor = ConsoleColor.Red;
66+
Console.WriteLine(ex.Status.Detail);
67+
Console.ResetColor();
68+
#endregion
69+
}
70+
71+
await Task.Delay(TimeSpan.FromSeconds(0.2));
72+
}
73+
}
74+
75+
private static GrpcChannel CreateChannel()
76+
{
77+
var methodConfig = new MethodConfig
78+
{
79+
Names = { MethodName.Default },
80+
RetryPolicy = new RetryPolicy
81+
{
82+
MaxAttempts = 10,
83+
InitialBackoff = TimeSpan.FromSeconds(0.5),
84+
MaxBackoff = TimeSpan.FromSeconds(0.5),
85+
BackoffMultiplier = 1,
86+
RetryableStatusCodes = { StatusCode.Unavailable }
87+
}
88+
};
89+
90+
return GrpcChannel.ForAddress("http://localhost:5000", new GrpcChannelOptions
91+
{
92+
ServiceConfig = new ServiceConfig { MethodConfigs = { methodConfig } }
93+
});
94+
}
95+
96+
private static async Task<string> GetRetryCount(Task<Metadata> responseHeadersTask)
97+
{
98+
var headers = await responseHeadersTask;
99+
var previousAttemptCount = headers.GetValue("grpc-previous-rpc-attempts");
100+
return previousAttemptCount != null ? $"(retry count: {previousAttemptCount})" : string.Empty;
101+
}
102+
103+
private static readonly IList<string> Products = new List<string>
104+
{
105+
"Secrets of Silicon Valley",
106+
"The Busy Executive's Database Guide",
107+
"Emotional Security: A New Algorithm",
108+
"Prolonged Data Deprivation: Four Case Studies",
109+
"Cooking with Computers: Surreptitious Balance Sheets",
110+
"Silicon Valley Gastronomic Treats",
111+
"Sushi, Anyone?",
112+
"Fifty Years in Buckingham Palace Kitchens",
113+
"But Is It User Friendly?",
114+
"You Can Combat Computer Stress!",
115+
"Is Anger the Enemy?",
116+
"Life Without Fear",
117+
"The Gourmet Microwave",
118+
"Onions, Leeks, and Garlic: Cooking Secrets of the Mediterranean",
119+
"The Psychology of Computer Cooking",
120+
"Straight Talk About Computers",
121+
"Computer Phobic AND Non-Phobic Individuals: Behavior Variations",
122+
"Net Etiquette"
123+
};
124+
}
125+
}

examples/Retrier/Proto/retry.proto

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2019 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
import "google/protobuf/wrappers.proto";
18+
19+
package retry;
20+
21+
service Retrier {
22+
rpc DeliverPackage (Package) returns (Response);
23+
}
24+
25+
message Package {
26+
string name = 1;
27+
}
28+
29+
message Response {
30+
string message = 1;
31+
}

examples/Retrier/Retrier.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29230.61
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{534AC5F8-2DF2-40BD-87A5-B3D8310118C4}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{48A1D3BC-A14B-436A-8822-6DE2BEF8B747}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {D22B3129-3BFB-41FA-9FCE-E45EBEF8C2DD}
30+
EndGlobalSection
31+
EndGlobal

examples/Retrier/Server/Program.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Microsoft.AspNetCore.Hosting;
20+
using Microsoft.Extensions.Hosting;
21+
22+
namespace Server
23+
{
24+
public class Program
25+
{
26+
public static void Main(string[] args)
27+
{
28+
CreateHostBuilder(args).Build().Run();
29+
}
30+
31+
public static IHostBuilder CreateHostBuilder(string[] args) =>
32+
Host.CreateDefaultBuilder(args)
33+
.ConfigureWebHostDefaults(webBuilder =>
34+
{
35+
webBuilder.UseStartup<Startup>();
36+
});
37+
}
38+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Protobuf Include="..\Proto\retry.proto" GrpcServices="Server" Link="Protos\retry.proto" />
9+
10+
<PackageReference Include="Grpc.AspNetCore" Version="$(GrpcDotNetPackageVersion)" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System;
20+
using System.Threading.Tasks;
21+
using Grpc.Core;
22+
using Retry;
23+
24+
namespace Server
25+
{
26+
public class RetrierService : Retrier.RetrierBase
27+
{
28+
private readonly Random _random = new Random();
29+
30+
public override Task<Response> DeliverPackage(Package request, ServerCallContext context)
31+
{
32+
const double deliveryChance = 0.5;
33+
if (_random.NextDouble() > deliveryChance)
34+
{
35+
throw new RpcException(new Status(StatusCode.Unavailable, $"- {request.Name}"));
36+
}
37+
38+
return Task.FromResult(new Response
39+
{
40+
Message = $"+ {request.Name}"
41+
});
42+
}
43+
}
44+
}

examples/Retrier/Server/Startup.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using Microsoft.AspNetCore.Builder;
20+
using Microsoft.AspNetCore.Hosting;
21+
using Microsoft.Extensions.DependencyInjection;
22+
using Microsoft.Extensions.Hosting;
23+
24+
namespace Server
25+
{
26+
public class Startup
27+
{
28+
public void ConfigureServices(IServiceCollection services)
29+
{
30+
services.AddGrpc();
31+
}
32+
33+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
34+
{
35+
if (env.IsDevelopment())
36+
{
37+
app.UseDeveloperExceptionPage();
38+
}
39+
40+
app.UseRouting();
41+
42+
app.UseEndpoints(endpoints =>
43+
{
44+
endpoints.MapGrpcService<RetrierService>();
45+
});
46+
}
47+
}
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Debug",
5+
"System": "Information",
6+
"Grpc": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)