diff options
author | Rory& <root@rory.gay> | 2024-06-17 00:43:25 +0200 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-06-17 00:43:49 +0200 |
commit | 65e52735bd7b9ae249b97e807d0c83d4e6d6570a (patch) | |
tree | 2b6d909065d6320a1e6e5d0e49265207c2a24909 /LibMatrix/Extensions/MatrixHttpClient.Multi.cs | |
parent | Update dependencies (diff) | |
download | LibMatrix-65e52735bd7b9ae249b97e807d0c83d4e6d6570a.tar.xz |
Single httpclient implementation
Diffstat (limited to '')
-rw-r--r-- | LibMatrix/Extensions/MatrixHttpClient.Multi.cs (renamed from LibMatrix/Extensions/HttpClientExtensions.cs) | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/MatrixHttpClient.Multi.cs index 64b4f6a..e7a2044 100644 --- a/LibMatrix/Extensions/HttpClientExtensions.cs +++ b/LibMatrix/Extensions/MatrixHttpClient.Multi.cs @@ -1,8 +1,10 @@ +#define SINGLE_HTTPCLIENT // Use a single HttpClient instance for all MatrixHttpClient instances +// #define SYNC_HTTPCLIENT // Only allow one request as a time, for debugging using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Globalization; using System.Net.Http.Headers; using System.Reflection; +using System.Security.Cryptography.X509Certificates; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -25,7 +27,16 @@ public static class HttpClientExtensions { } } -public class MatrixHttpClient : HttpClient { +#region Per-instance HTTP client code + +#if !SINGLE_HTTPCLIENT +public class MatrixHttpClient() : HttpClient(handler) { + private static readonly SocketsHttpHandler handler = new() { + PooledConnectionLifetime = TimeSpan.FromMinutes(15), + MaxConnectionsPerServer = 256, + EnableMultipleHttp2Connections = true + }; + public Dictionary<string, string> AdditionalQueryParameters { get; set; } = new(); internal string? AssertedUserId { get; set; } @@ -44,7 +55,7 @@ public class MatrixHttpClient : HttpClient { public async Task<HttpResponseMessage> SendUnhandledAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if(debug) await _rateLimitSemaphore.WaitAsync(cancellationToken); - // Console.WriteLine($"Sending {request.Method} {BaseAddress}{request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)})"); + Console.WriteLine($"Sending {request.Method} {BaseAddress}{request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)})"); if (request.RequestUri is null) throw new NullReferenceException("RequestUri is null"); if (!request.RequestUri.IsAbsoluteUri) request.RequestUri = new Uri(BaseAddress, request.RequestUri); // if (AssertedUserId is not null) request.RequestUri = request.RequestUri.AddQuery("user_id", AssertedUserId); @@ -73,6 +84,8 @@ public class MatrixHttpClient : HttpClient { finally { if(debug) _rateLimitSemaphore.Release(); } + + Console.WriteLine($"Sending {request.Method} {request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)}) -> {(int)responseMessage.StatusCode} {responseMessage.StatusCode} ({Util.BytesToString(responseMessage.Content.Headers.ContentLength ?? 0)})"); return responseMessage; } @@ -191,27 +204,6 @@ public class MatrixHttpClient : HttpClient { await foreach (var resp in result) yield return resp; } } +#endif -public class JsonFloatStringConverter : JsonConverter<float> { - public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - => float.Parse(reader.GetString()!); - - public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); -} - -public class JsonDoubleStringConverter : JsonConverter<double> { - public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - => double.Parse(reader.GetString()!); - - public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); -} - -public class JsonDecimalStringConverter : JsonConverter<decimal> { - public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - => decimal.Parse(reader.GetString()!); - - public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); -} \ No newline at end of file +#endregion \ No newline at end of file |