1616
1717package org .springframework .graphql .execution ;
1818
19+ import java .util .concurrent .atomic .AtomicBoolean ;
20+
1921import graphql .GraphQLContext ;
2022import io .micrometer .context .ContextSnapshot ;
2123import io .micrometer .context .ContextSnapshotFactory ;
@@ -39,7 +41,9 @@ public abstract class ContextPropagationHelper {
3941
4042 private static final String CONTEXT_SNAPSHOT_FACTORY_KEY = ContextPropagationHelper .class .getName () + ".KEY" ;
4143
42- private static final String CANCEL_PUBLISHER_KEY = ContextPropagationHelper .class .getName () + ".cancelled" ;
44+ private static final String CANCELED_KEY = ContextPropagationHelper .class .getName () + ".canceled" ;
45+
46+ private static final String CANCELED_PUBLISHER_KEY = ContextPropagationHelper .class .getName () + ".canceledPublisher" ;
4347
4448
4549 /**
@@ -119,50 +123,37 @@ public static ContextSnapshot captureFrom(GraphQLContext context) {
119123 }
120124
121125 /**
122- * Create a publisher and store it into the given {@link GraphQLContext}.
123- * This publisher can then be used to propagate cancel signals to upstream publishers .
126+ * Create an atomic boolean and store it into the given {@link GraphQLContext}.
127+ * This boolean value can then be checked by upstream publishers to know whether the request is canceled .
124128 * @param context the current GraphQL context
125- * @since 1.3.5
129+ * @since 1.3.6
126130 */
127- public static Sinks .Empty <Void > createCancelPublisher (GraphQLContext context ) {
128- Sinks .Empty <Void > requestCancelled = Sinks .empty ();
129- context .put (CANCEL_PUBLISHER_KEY , requestCancelled .asMono ());
130- return requestCancelled ;
131+ public static Runnable createCancelSignal (GraphQLContext context ) {
132+ AtomicBoolean requestCancelled = new AtomicBoolean ();
133+ Sinks .Empty <Void > cancelSignal = Sinks .empty ();
134+ context .put (CANCELED_KEY , requestCancelled );
135+ context .put (CANCELED_PUBLISHER_KEY , cancelSignal .asMono ());
136+ return () -> {
137+ requestCancelled .set (true );
138+ cancelSignal .tryEmitEmpty ();
139+ };
131140 }
132141
133142 /**
134143 * Return {@code true} if the current request has been cancelled, {@code false} otherwise.
135- * This checks whether a {@link #createCancelPublisher (GraphQLContext) cancellation publisher is present}
144+ * This checks whether a {@link #createCancelSignal (GraphQLContext) cancellation publisher is present}
136145 * in the given context and the cancel signal has fired already.
137146 * @param context the current GraphQL context
138147 * @since 1.4.0
139148 */
140149 public static boolean isCancelled (GraphQLContext context ) {
141- Mono < Void > cancelSignal = context .get (CANCEL_PUBLISHER_KEY );
142- if (cancelSignal != null ) {
143- return cancelSignal . toFuture (). isDone ();
150+ AtomicBoolean requestCancelled = context .get (CANCELED_KEY );
151+ if (requestCancelled != null ) {
152+ return requestCancelled . get ();
144153 }
145154 return false ;
146155 }
147156
148- /**
149- * Bind the source {@link Mono} to the publisher from the given {@link GraphQLContext}.
150- * The returned {@code Mono} will be cancelled when this publisher completes.
151- * Subscribers must use the returned {@code Mono} instance.
152- * @param source the source {@code Mono}
153- * @param context the current GraphQL context
154- * @param <T> the type of published elements
155- * @return the new {@code Mono} that will be cancelled when notified
156- * @since 1.3.5
157- */
158- public static <T > Mono <T > bindCancelFrom (Mono <T > source , GraphQLContext context ) {
159- Mono <Void > cancelSignal = context .get (CANCEL_PUBLISHER_KEY );
160- if (cancelSignal != null ) {
161- return source .takeUntilOther (cancelSignal );
162- }
163- return source ;
164- }
165-
166157 /**
167158 * Bind the source {@link Flux} to the publisher from the given {@link GraphQLContext}.
168159 * The returned {@code Flux} will be cancelled when this publisher completes.
@@ -174,7 +165,7 @@ public static <T> Mono<T> bindCancelFrom(Mono<T> source, GraphQLContext context)
174165 * @since 1.3.5
175166 */
176167 public static <T > Flux <T > bindCancelFrom (Flux <T > source , GraphQLContext context ) {
177- Mono <Void > cancelSignal = context .get (CANCEL_PUBLISHER_KEY );
168+ Mono <Void > cancelSignal = context .get (CANCELED_PUBLISHER_KEY );
178169 if (cancelSignal != null ) {
179170 return source .takeUntilOther (cancelSignal );
180171 }
0 commit comments