Skip to content

Commit

Permalink
fix: Cherry-pick: Avoid catastrophic failure for too large transactio…
Browse files Browse the repository at this point in the history
…ns (#13029) (#13079)

Signed-off-by: Stanimir Stoyanov <stanimir.stoyanov@limechain.tech>
  • Loading branch information
netopyr authored and stoyanov-st committed May 15, 2024
1 parent 2fa6805 commit a647886
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,20 @@ public abstract class MethodBase implements ServerCalls.UnaryMethod<BufferedData
@Override
public void invoke(
@NonNull final BufferedData requestBuffer, @NonNull final StreamObserver<BufferedData> responseObserver) {
try {
// Track the number of times this method has been called
callsReceivedCounter.increment();
callsReceivedSpeedometer.cycle();
// Track the number of times this method has been called
callsReceivedCounter.increment();
callsReceivedSpeedometer.cycle();

// Fail-fast if the request is too large (Note that the request buffer is sized to allow exactly
// 1 more byte than MAX_MESSAGE_SIZE, so we can detect this case).
if (requestBuffer.length() > MAX_MESSAGE_SIZE) {
throw new RuntimeException("More than " + MAX_MESSAGE_SIZE + " received");
}
// Fail-fast if the request is too large (Note that the request buffer is sized to allow exactly
// 1 more byte than MAX_MESSAGE_SIZE, so we can detect this case).
if (requestBuffer.length() > MAX_MESSAGE_SIZE) {
callsFailedCounter.increment();
final var exception = new RuntimeException("More than " + MAX_MESSAGE_SIZE + " received");
responseObserver.onError(exception);
return;
}

try {
// Prepare the response buffer
final var responseBuffer = BUFFER_THREAD_LOCAL.get();
responseBuffer.reset();
Expand Down

0 comments on commit a647886

Please sign in to comment.