|
| 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 | +} |
0 commit comments