Skip to content
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

More GraphQL Context cleanup #27173

Merged
merged 1 commit into from
Aug 8, 2022
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 @@ -19,6 +19,7 @@
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser;
import io.smallrye.graphql.execution.ExecutionService;
import io.smallrye.graphql.execution.context.SmallRyeContextManager;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -56,7 +57,7 @@ private <T> Handler<T> createCurrentManagedContextTerminationHandler() {
return new Handler<>() {
@Override
public void handle(Object e) {
currentManagedContext.terminate();
terminate();
}
};
}
Expand All @@ -73,9 +74,9 @@ public void handle(final RoutingContext ctx) {
}
try {
handleWithIdentity(ctx);
currentManagedContext.deactivate();
deactivate();
} catch (Throwable t) {
currentManagedContext.terminate();
terminate();
throw t;
}
}
Expand Down Expand Up @@ -128,4 +129,14 @@ private Map<String, List<String>> getHeaders(RoutingContext ctx) {
}
return h;
}

private void deactivate() {
SmallRyeContextManager.clearCurrentSmallRyeContext();
currentManagedContext.deactivate();
}

private void terminate() {
SmallRyeContextManager.clearCurrentSmallRyeContext();
currentManagedContext.terminate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;
import io.smallrye.graphql.SmallRyeGraphQLServerMessages;
import io.smallrye.graphql.execution.context.SmallRyeContextManager;
import io.smallrye.graphql.execution.datafetcher.AbstractDataFetcher;
import io.smallrye.graphql.schema.model.Operation;
import io.smallrye.graphql.schema.model.Type;
Expand All @@ -36,6 +37,11 @@ protected <O> O invokeAndTransform(
return (O) uni
.onItemOrFailure()
.transformToUni((result, throwable, emitter) -> {

emitter.onTermination(() -> {
deactivate(requestContext);
});

if (throwable != null) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
if (throwable instanceof GraphQLException) {
Expand All @@ -55,16 +61,26 @@ protected <O> O invokeAndTransform(
te.appendDataFetcherResult(resultBuilder, dfe);
}
}

emitter.complete(resultBuilder.build());
})
.onCancellation().invoke(() -> {
deactivate(requestContext);
})
.onTermination().invoke(() -> {
deactivate(requestContext);
})
.subscribe()
.asCompletionStage();
} finally {
requestContext.deactivate();
deactivate(requestContext);
}
}

private void deactivate(ManagedContext requestContext) {
SmallRyeContextManager.clearCurrentSmallRyeContext();
requestContext.deactivate();
}

protected abstract Uni<?> handleUserMethodCall(DataFetchingEnvironment dfe, final Object[] transformedArguments)
throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;
import io.smallrye.context.SmallRyeThreadContext;
import io.smallrye.graphql.execution.context.SmallRyeContextManager;
import io.smallrye.graphql.execution.datafetcher.DefaultDataFetcher;
import io.smallrye.graphql.schema.model.Operation;
import io.smallrye.graphql.schema.model.Type;
Expand Down Expand Up @@ -40,7 +41,7 @@ public <T> T invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.B
return invokeAndTransformBlocking(dfe, resultBuilder, transformedArguments, vc);
}
} finally {
requestContext.deactivate();
deactivate(requestContext);
}
}

Expand All @@ -57,7 +58,7 @@ public CompletionStage<List<T>> invokeBatch(DataFetchingEnvironment dfe, Object[
return invokeBatchBlocking(dfe, arguments, vc);
}
} finally {
requestContext.deactivate();
deactivate(requestContext);
}
}

Expand Down Expand Up @@ -113,4 +114,9 @@ private CompletionStage<List<T>> invokeBatchBlocking(DataFetchingEnvironment dfe
private boolean runBlocking(DataFetchingEnvironment dfe) {
return dfe.getGraphQlContext().get("runBlocking");
}

private void deactivate(ManagedContext requestContext) {
SmallRyeContextManager.clearCurrentSmallRyeContext();
requestContext.deactivate();
}
}