From 131248c257cadb68bd5192ebd5d4ddc3dbdab9b0 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 21 Nov 2024 12:31:38 +0000 Subject: [PATCH] Switch off Response Compression for loopback --- .../Nethermind.Runner/JsonRpc/Startup.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs b/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs index 351a301f935..5eb8d057040 100644 --- a/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs +++ b/src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO; using System.IO.Pipelines; +using System.Net; using System.Security.Authentication; using System.Threading; using System.Threading.Tasks; @@ -82,7 +83,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpc app.UseRouting(); app.UseCors(); - app.UseResponseCompression(); IConfigProvider? configProvider = app.ApplicationServices.GetService(); IRpcAuthentication? rpcAuthentication = app.ApplicationServices.GetService(); @@ -99,6 +99,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpc IJsonRpcUrlCollection jsonRpcUrlCollection = app.ApplicationServices.GetRequiredService(); IHealthChecksConfig healthChecksConfig = configProvider.GetConfig(); + // If request is local, don't use response compression, + // as it allocates a lot, but doesn't improve much for loopback + app.UseWhen(ctx => + !IsLocalhost(ctx.Connection.RemoteIpAddress), + builder => builder.UseResponseCompression()); + if (initConfig.WebSocketsEnabled) { app.UseWebSockets(new WebSocketOptions()); @@ -285,6 +291,13 @@ async Task PushErrorResponse(int statusCode, int errorCode, string message) }); } + /// + /// Check for IPv4 localhost (127.0.0.1) and IPv6 localhost (::1) + /// + /// Request source + private static bool IsLocalhost(IPAddress remoteIp) + => IPAddress.IsLoopback(remoteIp) || remoteIp.Equals(IPAddress.IPv6Loopback); + private static int GetStatusCode(JsonRpcResult result) { if (result.IsCollection)