Skip to content

Commit

Permalink
Apply max responseBodySize correctly (#6372)
Browse files Browse the repository at this point in the history
* Apply max responseBodySize correctly

* Fix log
  • Loading branch information
benaadams authored Dec 14, 2023
1 parent 36c1171 commit bce3244
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ void SerializeTimeoutException(IJsonRpcService service, IBufferWriter<byte> resu
try
{
JsonRpcContext jsonRpcContext = JsonRpcContext.Http(jsonRpcUrl);
long totalResponseSize = 0;
await foreach (JsonRpcResult result in jsonRpcProcessor.ProcessAsync(request, jsonRpcContext))
{
Stream stream = jsonRpcConfig.BufferResponses ? new MemoryStream() : null;
ICountingBufferWriter resultWriter = stream is not null ? new CountingStreamPipeWriter(stream) : new CountingPipeWriter(ctx.Response.BodyWriter);
try
{
ctx.Response.ContentType = "application/json";
Expand Down Expand Up @@ -198,9 +196,9 @@ void SerializeTimeoutException(IJsonRpcService service, IBufferWriter<byte> resu
_ = jsonRpcLocalStats.ReportCall(entry.Report);
// We reached the limit and don't want to responded to more request in the batch
if (!jsonRpcContext.IsAuthenticated && totalResponseSize > jsonRpcConfig.MaxBatchResponseBodySize)
if (!jsonRpcContext.IsAuthenticated && resultWriter.WrittenCount > jsonRpcConfig.MaxBatchResponseBodySize)
{
if (logger.IsWarn) logger.Warn($"The max batch response body size exceeded. The current response size {totalResponseSize}, and the config setting is JsonRpc.{nameof(jsonRpcConfig.MaxBatchResponseBodySize)} = {jsonRpcConfig.MaxBatchResponseBodySize}");
if (logger.IsWarn) logger.Warn($"The max batch response body size exceeded. The current response size {resultWriter.WrittenCount}, and the config setting is JsonRpc.{nameof(jsonRpcConfig.MaxBatchResponseBodySize)} = {jsonRpcConfig.MaxBatchResponseBodySize}");
enumerator.IsStopped = true;
}
}
Expand Down Expand Up @@ -246,7 +244,6 @@ void SerializeTimeoutException(IJsonRpcService service, IBufferWriter<byte> resu
? new RpcReport("# collection serialization #", handlingTimeMicroseconds, true)
: result.Report.Value, handlingTimeMicroseconds, resultWriter.WrittenCount);
totalResponseSize += resultWriter.WrittenCount;
Interlocked.Add(ref Metrics.JsonRpcBytesSentHttp, resultWriter.WrittenCount);
// There should be only one response because we don't expect multiple JSON tokens in the request
Expand Down

0 comments on commit bce3244

Please sign in to comment.