Skip to content

Support async task decorator #350

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
@@ -0,0 +1,7 @@
package graphql.kickstart.servlet;

public interface AsyncTaskDecorator {

Runnable decorate(Runnable runnable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package graphql.kickstart.servlet;

import java.util.concurrent.Executor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
class AsyncTaskExecutor implements Executor {

private final Executor executor;
private final AsyncTaskDecorator taskDecorator;

@Override
public void execute(@NonNull Runnable command) {
if (taskDecorator != null) {
Runnable decorated = taskDecorator.decorate(command);
executor.execute(decorated);
} else {
executor.execute(command);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import lombok.Getter;

Expand Down Expand Up @@ -142,7 +144,10 @@ public static class Builder {
private Supplier<BatchInputPreProcessor> batchInputPreProcessorSupplier =
NoOpBatchInputPreProcessor::new;
private GraphQLResponseCacheManager responseCacheManager;
private Executor asyncExecutor = Executors.newCachedThreadPool();
private int asyncCorePoolSize = 10;
private int asyncMaxPoolSize = 200;
private Executor asyncExecutor;
private AsyncTaskDecorator asyncTaskDecorator;

private Builder(GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder) {
this.invocationInputFactoryBuilder = invocationInputFactoryBuilder;
Expand Down Expand Up @@ -203,6 +208,16 @@ public Builder with(Executor asyncExecutor) {
return this;
}

public Builder asyncCorePoolSize(int asyncCorePoolSize) {
this.asyncCorePoolSize = asyncCorePoolSize;
return this;
}

public Builder asyncMaxPoolSize(int asyncMaxPoolSize) {
this.asyncMaxPoolSize = asyncMaxPoolSize;
return this;
}

public Builder with(ContextSetting contextSetting) {
if (contextSetting != null) {
this.contextSetting = contextSetting;
Expand All @@ -229,6 +244,27 @@ public Builder with(GraphQLResponseCacheManager responseCache) {
return this;
}

public Builder with(AsyncTaskDecorator asyncTaskDecorator) {
this.asyncTaskDecorator = asyncTaskDecorator;
return this;
}

private Executor getAsyncExecutor() {
if (asyncExecutor != null) {
return asyncExecutor;
}
return new ThreadPoolExecutor(
asyncCorePoolSize,
asyncMaxPoolSize,
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(Integer.MAX_VALUE));
}

private Executor getAsyncTaskExecutor() {
return new AsyncTaskExecutor(getAsyncExecutor(), asyncTaskDecorator);
}

public GraphQLConfiguration build() {
return new GraphQLConfiguration(
this.invocationInputFactory != null
Expand All @@ -243,7 +279,7 @@ public GraphQLConfiguration build() {
contextSetting,
batchInputPreProcessorSupplier,
responseCacheManager,
asyncExecutor);
getAsyncTaskExecutor());
}
}
}