Skip to content

Separate async executor #349

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

Merged
merged 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import graphql.schema.GraphQLSchema;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
import lombok.Getter;

Expand All @@ -31,6 +33,7 @@ public class GraphQLConfiguration {
@Getter private final long asyncTimeout;
private final ContextSetting contextSetting;
private final GraphQLResponseCacheManager responseCacheManager;
@Getter private final Executor asyncExecutor;
private HttpRequestHandler requestHandler;

private GraphQLConfiguration(
Expand All @@ -43,8 +46,10 @@ private GraphQLConfiguration(
long asyncTimeout,
ContextSetting contextSetting,
Supplier<BatchInputPreProcessor> batchInputPreProcessor,
GraphQLResponseCacheManager responseCacheManager) {
GraphQLResponseCacheManager responseCacheManager,
Executor asyncExecutor) {
this.invocationInputFactory = invocationInputFactory;
this.asyncExecutor = asyncExecutor;
this.graphQLInvoker = graphQLInvoker != null ? graphQLInvoker : queryInvoker.toGraphQLInvoker();
this.objectMapper = objectMapper;
this.listeners = listeners;
Expand Down Expand Up @@ -137,6 +142,7 @@ public static class Builder {
private Supplier<BatchInputPreProcessor> batchInputPreProcessorSupplier =
NoOpBatchInputPreProcessor::new;
private GraphQLResponseCacheManager responseCacheManager;
private Executor asyncExecutor = Executors.newCachedThreadPool();

private Builder(GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder) {
this.invocationInputFactoryBuilder = invocationInputFactoryBuilder;
Expand Down Expand Up @@ -192,6 +198,11 @@ public Builder asyncTimeout(long asyncTimeout) {
return this;
}

public Builder with(Executor asyncExecutor) {
this.asyncExecutor = asyncExecutor;
return this;
}

public Builder with(ContextSetting contextSetting) {
if (contextSetting != null) {
this.contextSetting = contextSetting;
Expand Down Expand Up @@ -231,7 +242,8 @@ public GraphQLConfiguration build() {
asyncTimeout,
contextSetting,
batchInputPreProcessorSupplier,
responseCacheManager);
responseCacheManager,
asyncExecutor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ private void invokeAndHandleAsync(
}
};
asyncContext.addListener(timeoutListener);
asyncContext.start(
() -> {
FutureExecutionResult futureResult = invoke(invocationInput, request, response);
futureHolder.set(futureResult);
handle(futureResult, request, response, listenerHandler)
.thenAccept(it -> asyncContext.complete());
});
configuration
.getAsyncExecutor()
.execute(
() -> {
FutureExecutionResult futureResult = invoke(invocationInput, request, response);
futureHolder.set(futureResult);
handle(futureResult, request, response, listenerHandler)
.thenAccept(it -> asyncContext.complete());
});
}

private CompletableFuture<Void> handle(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package graphql.kickstart.servlet

import com.google.common.io.ByteStreams
import graphql.Directives
import graphql.Scalars
import graphql.execution.reactive.SingleSubscriberPublisher
import graphql.kickstart.execution.context.ContextSetting
Expand All @@ -15,7 +14,9 @@ import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import graphql.schema.idl.TypeRuntimeWiring
import graphql.schema.idl.errors.SchemaProblem
import org.jetbrains.annotations.NotNull

import java.util.concurrent.Executor
import java.util.concurrent.atomic.AtomicReference

class TestUtils {
Expand Down Expand Up @@ -59,6 +60,7 @@ class TestUtils {
.with(schema)
.with(contextSetting)
.with(contextBuilder)
.with(executor())
.build())
servlet.init(null)
return servlet
Expand Down Expand Up @@ -96,9 +98,19 @@ class TestUtils {
if (listeners != null) {
configBuilder.with(Arrays.asList(listeners))
}
configBuilder.with(executor());
configBuilder.build()
}

private static Executor executor() {
new Executor() {
@Override
void execute(@NotNull Runnable command) {
command.run()
}
}
}

static def createBatchExecutionHandler() {
new TestBatchInputPreProcessor()
}
Expand Down