about summary refs log tree commit diff
path: root/LibMatrix/Extensions/HttpClientExtensions.cs
diff options
context:
space:
mode:
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