Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch off Response Compression for loopback #7782

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +83,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpc

app.UseRouting();
app.UseCors();
app.UseResponseCompression();

IConfigProvider? configProvider = app.ApplicationServices.GetService<IConfigProvider>();
IRpcAuthentication? rpcAuthentication = app.ApplicationServices.GetService<IRpcAuthentication>();
Expand All @@ -99,6 +99,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJsonRpc
IJsonRpcUrlCollection jsonRpcUrlCollection = app.ApplicationServices.GetRequiredService<IJsonRpcUrlCollection>();
IHealthChecksConfig healthChecksConfig = configProvider.GetConfig<IHealthChecksConfig>();

// 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());
Expand Down Expand Up @@ -285,6 +291,13 @@ async Task PushErrorResponse(int statusCode, int errorCode, string message)
});
}

/// <summary>
/// Check for IPv4 localhost (127.0.0.1) and IPv6 localhost (::1)
/// </summary>
/// <param name="remoteIp">Request source</param>
private static bool IsLocalhost(IPAddress remoteIp)
=> IPAddress.IsLoopback(remoteIp) || remoteIp.Equals(IPAddress.IPv6Loopback);

private static int GetStatusCode(JsonRpcResult result)
{
if (result.IsCollection)
Expand Down