Skip to content

Commit

Permalink
Replace TimeoutHandler in GraphQLHttpService with Vert.x's impl [hype…
Browse files Browse the repository at this point in the history
…rledger#535]

Signed-off-by: Diego López León <[email protected]>
Signed-off-by: Diego López León <[email protected]>
  • Loading branch information
diega committed Apr 25, 2022
1 parent 44979b7 commit dd7ef3a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLResponseType;
import org.hyperledger.besu.ethereum.api.graphql.internal.response.GraphQLSuccessResponse;
import org.hyperledger.besu.ethereum.api.handlers.IsAliveHandler;
import org.hyperledger.besu.ethereum.api.handlers.TimeoutHandler;
import org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
import org.hyperledger.besu.util.NetworkUtility;
Expand All @@ -41,6 +40,7 @@
import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.annotations.VisibleForTesting;
Expand All @@ -66,6 +66,7 @@
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CorsHandler;
import io.vertx.ext.web.handler.TimeoutHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -164,8 +165,9 @@ public CompletableFuture<?> start() {
.method(POST)
.produces(APPLICATION_JSON)
.handler(
TimeoutHandler.handler(
Optional.of(new TimeoutOptions(config.getHttpTimeoutSec())), false))
TimeoutHandler.create(
TimeUnit.SECONDS.toMillis(config.getHttpTimeoutSec()),
TimeoutOptions.DEFAULT_ERROR_CODE))
.handler(this::handleGraphQLRequest);

final CompletableFuture<?> resultFuture = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,19 @@
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

public class HandlerFactory {
private static final Map<HandlerName, Handler<RoutingContext>> HANDLERS =
new ConcurrentHashMap<>();

public static Handler<RoutingContext> timeout(
final TimeoutOptions globalOptions,
final Map<String, JsonRpcMethod> methods,
final boolean decodeJSON) {
final TimeoutOptions globalOptions, final Map<String, JsonRpcMethod> methods) {
assert methods != null && globalOptions != null;
return HANDLERS.computeIfAbsent(
HandlerName.TIMEOUT,
handlerName ->
TimeoutHandler.handler(
Optional.of(globalOptions),
methods.keySet().stream()
.collect(Collectors.toMap(String::new, ignored -> globalOptions)),
decodeJSON));
return TimeoutHandler.handler(
Optional.of(globalOptions),
methods.keySet().stream().collect(Collectors.toMap(String::new, ignored -> globalOptions)));
}

public static Handler<RoutingContext> authentication(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package org.hyperledger.besu.ethereum.api.handlers;

import static java.util.Collections.emptyMap;

import org.hyperledger.besu.ethereum.api.jsonrpc.context.ContextKey;

import java.util.Map;
Expand All @@ -27,30 +25,23 @@

public class TimeoutHandler {

public static Handler<RoutingContext> handler(
final Optional<TimeoutOptions> globalOptions, final boolean decodeJSON) {
return handler(globalOptions, emptyMap(), decodeJSON);
}

public static Handler<RoutingContext> handler(
final Optional<TimeoutOptions> globalOptions,
final Map<String, TimeoutOptions> timeoutOptionsByMethod,
final boolean decodeJSON) {
final Map<String, TimeoutOptions> timeoutOptionsByMethod) {
assert timeoutOptionsByMethod != null;
return ctx -> processHandler(ctx, globalOptions, timeoutOptionsByMethod, decodeJSON);
return ctx -> processHandler(ctx, globalOptions, timeoutOptionsByMethod);
}

private static void processHandler(
final RoutingContext ctx,
final Optional<TimeoutOptions> globalOptions,
final Map<String, TimeoutOptions> timeoutOptionsByMethod,
final boolean decodeJSON) {
final Map<String, TimeoutOptions> timeoutOptionsByMethod) {
try {
final String bodyAsString = ctx.getBodyAsString();
if (bodyAsString != null) {
final String json = ctx.getBodyAsString().trim();
Optional<TimeoutOptions> methodTimeoutOptions = Optional.empty();
if (decodeJSON && !json.isEmpty() && json.charAt(0) == '{') {
if (!json.isEmpty() && json.charAt(0) == '{') {
final JsonObject requestBodyJsonObject = new JsonObject(json);
ctx.put(ContextKey.REQUEST_BODY_AS_JSON_OBJECT.name(), requestBodyJsonObject);
final String method = requestBodyJsonObject.getString("method");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.util.concurrent.TimeUnit;

public class TimeoutOptions {

public static final int DEFAULT_ERROR_CODE = 504;

private static final long DEFAULT_TIMEOUT_SECONDS = Duration.ofMinutes(5).toSeconds();
private static final int DEFAULT_ERROR_CODE = 504;
private final long timeoutSec;

private final int errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,7 @@ private Router buildRouter() {
HandlerFactory.authentication(authenticationService.get(), config.getNoAuthRpcApis()));
}
mainRoute
.handler(
HandlerFactory.timeout(
new TimeoutOptions(config.getHttpTimeoutSec()), rpcMethods, true))
.handler(HandlerFactory.timeout(new TimeoutOptions(config.getHttpTimeoutSec()), rpcMethods))
.handler(this::handleJsonRPCRequest);

if (authenticationService.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hyperledger.besu.ethereum.api.handlers.TimeoutHandler;
import org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.context.ContextKey;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;

import java.util.Arrays;
Expand Down Expand Up @@ -101,13 +100,12 @@ public void test() {
final Map<String, TimeoutOptions> options =
ImmutableMap.of(
method.getMethodName(), new TimeoutOptions(timeoutSec, DEFAULT_OPTS.getErrorCode()));
final Handler<RoutingContext> handler = TimeoutHandler.handler(globalOptions, options, true);
final Handler<RoutingContext> handler = TimeoutHandler.handler(globalOptions, options);
final RoutingContext ctx = Mockito.spy(RoutingContext.class);
final Vertx vertx = Mockito.spy(Vertx.class);
when(ctx.getBodyAsString()).thenReturn(body);
when(ctx.vertx()).thenReturn(vertx);
handler.handle(ctx);
verify(ctx).put(eq(ContextKey.REQUEST_BODY_AS_JSON_OBJECT.name()), any());
verify(vertx, times(timerMustBeSet ? 1 : 0))
.setTimer(eq(TimeUnit.SECONDS.toMillis(timeoutSec)), any());
verify(ctx, times(timerMustBeSet ? 1 : 0)).addBodyEndHandler(any());
Expand Down

0 comments on commit dd7ef3a

Please sign in to comment.