Skip to content

Commit 4a6dbce

Browse files
committed
feat: add httpclient to api
1 parent 04fff33 commit 4a6dbce

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace FunctionalLiving.Api.Infrastructure.Modules
2+
{
3+
using System;
4+
using System.Net.Http;
5+
using Autofac;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.Net.Http.Headers;
10+
using Polly;
11+
12+
public class HttpModule : Module
13+
{
14+
public static string HttpClientName = "KnxSender";
15+
16+
public HttpModule(
17+
IConfiguration configuration,
18+
IServiceCollection services,
19+
ILoggerFactory loggerFactory)
20+
{
21+
var logger = loggerFactory.CreateLogger<HttpModule>();
22+
var retryCount = 5;
23+
var lifeTimeInMinutes = 20;
24+
25+
// https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory
26+
services
27+
.AddHttpClient(HttpClientName, client =>
28+
{
29+
client.BaseAddress = configuration.GetValue<Uri>("KnxSender:Endpoint");
30+
client.DefaultRequestHeaders.Add(HeaderNames.UserAgent, "FunctionalLiving.Api");
31+
})
32+
33+
.SetHandlerLifetime(TimeSpan.FromMinutes(lifeTimeInMinutes))
34+
35+
.ConfigurePrimaryHttpMessageHandler(c =>
36+
new HttpClientHandler())
37+
38+
// HttpRequestException, HTTP 5XX, and HTTP 408
39+
.AddTransientHttpErrorPolicy(policyBuilder => policyBuilder
40+
.WaitAndRetryAsync(
41+
retryCount,
42+
retryAttempt =>
43+
{
44+
var value = Math.Pow(2, retryAttempt) / 4;
45+
var randomValue = new Random().Next((int)value * 3, (int)value * 5);
46+
logger?.LogInformation("Retrying after {Seconds} seconds...", randomValue);
47+
return TimeSpan.FromSeconds(randomValue);
48+
}));
49+
}
50+
}
51+
}

src/FunctionalLiving.Api/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"http://localhost:5000"
55
],
66

7+
"KnxSender": {
8+
"Endpoint": "http://localhost:9001"
9+
},
10+
711
"Serilog": {
812
"MinimumLevel": {
913
"Default": "Information",

src/FunctionalLiving.Api/paket.references

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Be.Vlaanderen.Basisregisters.Api
22
Be.Vlaanderen.Basisregisters.EventHandling.Autofac
33

4+
Microsoft.Extensions.Http
5+
Microsoft.Extensions.Http.Polly
6+
47
Serilog.Sinks.Seq
58

69
SourceLink.Embed.AllSourceFiles

src/FunctionalLiving.Knx.Listener/Infrastructure/Modules/HttpModule.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public HttpModule(
1919
ILoggerFactory loggerFactory)
2020
{
2121
var logger = loggerFactory.CreateLogger<HttpModule>();
22+
var retryCount = 5;
23+
var lifeTimeInMinutes = 20;
2224

2325
// https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory
2426
services
@@ -28,13 +30,15 @@ public HttpModule(
2830
client.DefaultRequestHeaders.Add(HeaderNames.UserAgent, "FunctionalLiving.Knx.Listener");
2931
})
3032

33+
.SetHandlerLifetime(TimeSpan.FromMinutes(lifeTimeInMinutes))
34+
3135
.ConfigurePrimaryHttpMessageHandler(c =>
3236
new HttpClientHandler())
3337

3438
// HttpRequestException, HTTP 5XX, and HTTP 408
3539
.AddTransientHttpErrorPolicy(policyBuilder => policyBuilder
3640
.WaitAndRetryAsync(
37-
5,
41+
retryCount,
3842
retryAttempt =>
3943
{
4044
var value = Math.Pow(2, retryAttempt) / 4;

0 commit comments

Comments
 (0)