Skip to content

Commit

Permalink
fix: Avoid catastrophic failure for too large transactions (#13029)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Heinrichs <netopyr@users.noreply.github.com>
  • Loading branch information
netopyr committed Apr 29, 2024
1 parent 11e55ed commit 8ef29c4
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 8ef29c4

Please sign in to comment.