Skip to content

Commit 8e569cc

Browse files
committed
Better socket usage
1 parent 12bcc93 commit 8e569cc

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

LessAnnoyingHttp/Http.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@
33
namespace LessAnnoyingHttp;
44

55
public static class Http {
6+
private static readonly HttpClient Client = new (new SocketsHttpHandler {
7+
PooledConnectionLifetime = TimeSpan.FromMinutes(2)
8+
});
9+
610
/// <summary>
711
/// The timeout in seconds which requests should wait for before failing (default: 10)<br/>
812
/// Changing this value changes the timeouts globally
913
/// </summary>
10-
public static int Timeout { get; set; } = 10;
14+
public static int Timeout {
15+
get => (int) Client.Timeout.TotalSeconds;
16+
set => Client.Timeout = TimeSpan.FromSeconds(value);
17+
}
18+
19+
static Http() {
20+
Timeout = 10;
21+
AppDomain.CurrentDomain.ProcessExit += (_, _) => Client.Dispose();
22+
}
1123

1224
/// <summary>
1325
/// Sends a GET request
@@ -16,8 +28,6 @@ public static class Http {
1628
/// <param name="headers">Optional headers</param>
1729
/// <returns><see cref="Response"/></returns>
1830
public static async Task<Response> GetAsync(string endpoint, Header[]? headers = null) {
19-
using HttpClient client = new ();
20-
client.Timeout = TimeSpan.FromSeconds(Timeout);
2131
HttpRequestMessage request = new () {
2232
RequestUri = new Uri(endpoint),
2333
Method = HttpMethod.Get
@@ -29,7 +39,7 @@ public static async Task<Response> GetAsync(string endpoint, Header[]? headers =
2939

3040
HttpResponseMessage response;
3141
try {
32-
response = await client.SendAsync(request);
42+
response = await Client.SendAsync(request);
3343
} catch (TaskCanceledException) {
3444
throw new TimeoutException($"Timeout waiting for response for request to {endpoint}");
3545
} catch (Exception e) {
@@ -45,8 +55,6 @@ public static async Task<Response> GetAsync(string endpoint, Header[]? headers =
4555
public static Response Get(string endpoint, Header[]? headers = null) => GetAsync(endpoint, headers).GetAwaiter().GetResult();
4656

4757
private static async Task<Response> BodyRequest(string endpoint, HttpMethod method, string body, Header[]? headers, string contentType) {
48-
using HttpClient client = new ();
49-
client.Timeout = TimeSpan.FromSeconds(Timeout);
5058
HttpRequestMessage request = new () {
5159
RequestUri = new Uri(endpoint),
5260
Method = method,
@@ -59,7 +67,7 @@ private static async Task<Response> BodyRequest(string endpoint, HttpMethod meth
5967

6068
HttpResponseMessage response;
6169
try {
62-
response = await client.SendAsync(request);
70+
response = await Client.SendAsync(request);
6371
} catch (TaskCanceledException) {
6472
throw new TimeoutException($"Timeout waiting for response for request to {endpoint}");
6573
} catch (Exception e) {

LessAnnoyingHttp/LessAnnoyingHttp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net10.0</TargetFramework>
4+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
55
<Version>1.4.0</Version>
66
<Authors>DemonExposer</Authors>
77
<Description>Simpler HTTP requests in .NET</Description>

0 commit comments

Comments
 (0)