Skip to content

Commit ca4cad9

Browse files
committed
Better options support
1 parent a09a5a4 commit ca4cad9

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed

src/HandlerKey.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
namespace Soenneker.Utils.HttpClientCache;
22

3-
internal readonly record struct HandlerKey(double LifetimeSeconds, int MaxConnections, bool UseCookies);
3+
internal readonly record struct HandlerKey(
4+
double LifetimeSeconds,
5+
int MaxConnections,
6+
bool UseCookies,
7+
double ConnectTimeoutSeconds,
8+
double? ResponseDrainTimeoutSeconds,
9+
bool? AllowAutoRedirect,
10+
int? AutomaticDecompression,
11+
double? KeepAlivePingDelaySeconds,
12+
double? KeepAlivePingTimeoutSeconds,
13+
int? KeepAlivePingPolicy,
14+
bool? UseProxy,
15+
int? ProxyHashCode,
16+
int? MaxResponseDrainSize,
17+
int? MaxResponseHeadersLength,
18+
int? SslOptionsHashCode);

src/HttpClientCache.cs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,70 @@ await modifyClient(httpClient)
106106

107107
private SocketsHttpHandler GetOrCreateHandler(HttpClientOptions? options)
108108
{
109-
var key = new HandlerKey(options?.PooledConnectionLifetime?.TotalSeconds ?? 600, options?.MaxConnectionsPerServer ?? 40,
110-
options?.UseCookieContainer == true);
109+
double connectTimeoutSeconds = options?.Timeout?.TotalSeconds ?? 100;
110+
111+
var key = new HandlerKey(
112+
LifetimeSeconds: options?.PooledConnectionLifetime?.TotalSeconds ?? 600,
113+
MaxConnections: options?.MaxConnectionsPerServer ?? 40,
114+
UseCookies: options?.UseCookieContainer == true,
115+
ConnectTimeoutSeconds: connectTimeoutSeconds,
116+
ResponseDrainTimeoutSeconds: options?.ResponseDrainTimeout?.TotalSeconds,
117+
AllowAutoRedirect: options?.AllowAutoRedirect,
118+
AutomaticDecompression: (int?)options?.AutomaticDecompression,
119+
KeepAlivePingDelaySeconds: options?.KeepAlivePingDelay?.TotalSeconds,
120+
KeepAlivePingTimeoutSeconds: options?.KeepAlivePingTimeout?.TotalSeconds,
121+
KeepAlivePingPolicy: (int?)options?.KeepAlivePingPolicy,
122+
UseProxy: options?.UseProxy,
123+
ProxyHashCode: options?.Proxy?.GetHashCode(),
124+
MaxResponseDrainSize: options?.MaxResponseDrainSize,
125+
MaxResponseHeadersLength: options?.MaxResponseHeadersLength,
126+
SslOptionsHashCode: options?.SslOptions?.GetHashCode());
111127

112128
return _handlers.GetOrAdd(key, _ =>
113129
{
114130
var handler = new SocketsHttpHandler
115131
{
116132
PooledConnectionLifetime = TimeSpan.FromSeconds(key.LifetimeSeconds),
117-
MaxConnectionsPerServer = key.MaxConnections
133+
MaxConnectionsPerServer = key.MaxConnections,
134+
ConnectTimeout = TimeSpan.FromSeconds(key.ConnectTimeoutSeconds)
118135
};
119136

120137
if (key.UseCookies)
121138
handler.CookieContainer = new CookieContainer();
122139

140+
if (key.ResponseDrainTimeoutSeconds.HasValue)
141+
handler.ResponseDrainTimeout = TimeSpan.FromSeconds(key.ResponseDrainTimeoutSeconds.Value);
142+
143+
if (key.AllowAutoRedirect.HasValue)
144+
handler.AllowAutoRedirect = key.AllowAutoRedirect.Value;
145+
146+
if (key.AutomaticDecompression.HasValue)
147+
handler.AutomaticDecompression = (DecompressionMethods)key.AutomaticDecompression.Value;
148+
149+
if (key.KeepAlivePingDelaySeconds.HasValue)
150+
handler.KeepAlivePingDelay = TimeSpan.FromSeconds(key.KeepAlivePingDelaySeconds.Value);
151+
152+
if (key.KeepAlivePingTimeoutSeconds.HasValue)
153+
handler.KeepAlivePingTimeout = TimeSpan.FromSeconds(key.KeepAlivePingTimeoutSeconds.Value);
154+
155+
if (key.KeepAlivePingPolicy.HasValue)
156+
handler.KeepAlivePingPolicy = (HttpKeepAlivePingPolicy)key.KeepAlivePingPolicy.Value;
157+
158+
if (key.UseProxy.HasValue)
159+
handler.UseProxy = key.UseProxy.Value;
160+
161+
if (options?.Proxy != null)
162+
handler.Proxy = options.Proxy;
163+
164+
if (key.MaxResponseDrainSize.HasValue)
165+
handler.MaxResponseDrainSize = key.MaxResponseDrainSize.Value;
166+
167+
if (key.MaxResponseHeadersLength.HasValue)
168+
handler.MaxResponseHeadersLength = key.MaxResponseHeadersLength.Value;
169+
170+
if (options?.SslOptions != null)
171+
handler.SslOptions = options.SslOptions;
172+
123173
return handler;
124174
});
125175
}

src/Soenneker.Utils.HttpClientCache.csproj

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8"?><Project Sdk="Microsoft.NET.Sdk">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Project Sdk="Microsoft.NET.Sdk">
24

35
<PropertyGroup>
46
<TargetFramework>net10.0</TargetFramework>
@@ -30,15 +32,19 @@
3032
<LangVersion>latest</LangVersion>
3133
<PackageReadmeFile>README.md</PackageReadmeFile>
3234
<PackageIcon>icon.png</PackageIcon>
33-
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild></PropertyGroup>
34-
35+
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
36+
</PropertyGroup>
37+
3538
<ItemGroup>
3639
<None Include="..\README.md" Pack="true" PackagePath="\" />
3740
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
3841
<None Include="..\icon.png" Pack="true" PackagePath="\" />
42+
</ItemGroup>
43+
44+
<ItemGroup>
3945
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.1" />
40-
<PackageReference Include="Soenneker.Dtos.HttpClientOptions" Version="4.0.9" />
46+
<PackageReference Include="Soenneker.Dtos.HttpClientOptions" Version="4.0.10" />
4147
<PackageReference Include="Soenneker.Utils.Runtime" Version="4.0.897" />
4248
<PackageReference Include="Soenneker.Utils.SingletonDictionary" Version="4.0.1135" />
4349
</ItemGroup>
44-
</Project>
50+
</Project>

0 commit comments

Comments
 (0)