From a1e64464eeeb38803ab89acb39016751aa0ea239 Mon Sep 17 00:00:00 2001 From: Andrei Ignat Date: Sun, 8 Oct 2023 23:38:30 +0300 Subject: [PATCH] added timeout --- .../code/PollyDemo/ConsoleTest/Emp.Weather.cs | 47 +++++++++++++++++++ .../code/PollyDemo/ConsoleTest/Program.cs | 6 ++- .../Controllers/EmployeeController.cs | 8 ++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 2023/Polly/code/PollyDemo/ConsoleTest/Emp.Weather.cs diff --git a/2023/Polly/code/PollyDemo/ConsoleTest/Emp.Weather.cs b/2023/Polly/code/PollyDemo/ConsoleTest/Emp.Weather.cs new file mode 100644 index 00000000..975bb254 --- /dev/null +++ b/2023/Polly/code/PollyDemo/ConsoleTest/Emp.Weather.cs @@ -0,0 +1,47 @@ + +using Polly.Timeout; +using System.Collections; + +namespace ConsoleTest; + +internal class EmpWeather +{ + static string url = $"http://localhost:5223/api"; + static HttpClient httpClient = new HttpClient(); + static string lastWeather = "this is cold from cache"; + public static async Task GetWeather(params int[] empId) + { + foreach (var it in empId) + { + + var urlLimit = $"{url}/Employee/GetWeatherForEmp/{it}"; + var timeoutPolicy = + Policy.TimeoutAsync( + TimeSpan.FromSeconds(value: 10), + TimeoutStrategy.Pessimistic, + async (context, timespan, task) => + { + + WriteLine("timeout "); + + + }); + + var dataPolicy = await timeoutPolicy + .ExecuteAndCaptureAsync(async () => + { + return await httpClient.GetStringAsync(urlLimit); + }); + + if (dataPolicy.Outcome == OutcomeType.Successful) + { + WriteLine($"real weather for {it} is " + dataPolicy.Result); + } + else + { + WriteLine($"cache weather for {it} is " + lastWeather); + } + + } + } +} diff --git a/2023/Polly/code/PollyDemo/ConsoleTest/Program.cs b/2023/Polly/code/PollyDemo/ConsoleTest/Program.cs index e828ae6f..9229788c 100644 --- a/2023/Polly/code/PollyDemo/ConsoleTest/Program.cs +++ b/2023/Polly/code/PollyDemo/ConsoleTest/Program.cs @@ -7,6 +7,10 @@ #endregion #region RateLimiting //await EmployeeRateLimiting.CalculateHistoryOld(4, 234, 579, 3423, 8756, 45, 2); -await EmployeeRateLimiting.CalculateHistoryPolly(4, 234, 579, 3423, 8756, 45, 2); +//await EmployeeRateLimiting.CalculateHistoryPolly(4, 234, 579, 3423, 8756, 45, 2); + +#endregion +#region Timeout +await EmpWeather.GetWeather(4, 234, 579, 3423, 8756, 45, 2); #endregion diff --git a/2023/Polly/code/PollyDemo/HardWorkingServer/Controllers/EmployeeController.cs b/2023/Polly/code/PollyDemo/HardWorkingServer/Controllers/EmployeeController.cs index 2c3bfdd9..70a46238 100644 --- a/2023/Polly/code/PollyDemo/HardWorkingServer/Controllers/EmployeeController.cs +++ b/2023/Polly/code/PollyDemo/HardWorkingServer/Controllers/EmployeeController.cs @@ -42,6 +42,14 @@ public async Task GetHistoryNOW_LimitedConcurrency(int empId) return "this is the history for " + empId; } + [HttpGet("{empId}")] + public async Task GetWeatherForEmp(int empId) + { + Console.WriteLine("trying to get weather for " + empId); + await Task.Delay(Random.Shared.Next(empId) * 100); + Console.WriteLine("found weather for " + empId); + return "I found that is very cold "; + } } }