|
17 | 17 | package org.springframework.graphql.server.webmvc;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.time.Duration; |
20 | 21 | import java.util.Map;
|
21 | 22 | import java.util.function.Consumer;
|
22 | 23 |
|
|
31 | 32 | import org.springframework.graphql.execution.SubscriptionPublisherException;
|
32 | 33 | import org.springframework.graphql.server.WebGraphQlHandler;
|
33 | 34 | import org.springframework.graphql.server.WebGraphQlResponse;
|
| 35 | +import org.springframework.lang.Nullable; |
34 | 36 | import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
|
35 | 37 | import org.springframework.web.servlet.function.ServerRequest;
|
36 | 38 | import org.springframework.web.servlet.function.ServerResponse;
|
|
47 | 49 | */
|
48 | 50 | public class GraphQlSseHandler extends AbstractGraphQlHttpHandler {
|
49 | 51 |
|
| 52 | + @Nullable |
| 53 | + private final Duration timeout; |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructor with the handler to delegate to, and no timeout, |
| 58 | + * i.e. relying on underlying Server async request timeout. |
| 59 | + * @param graphQlHandler the handler to delegate to |
| 60 | + */ |
50 | 61 | public GraphQlSseHandler(WebGraphQlHandler graphQlHandler) {
|
| 62 | + this(graphQlHandler, null); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Variant constructor with a timeout to use for SSE subscriptions. |
| 67 | + * @param graphQlHandler the handler to delegate to |
| 68 | + * @param timeout the timeout value to set on |
| 69 | + * {@link org.springframework.web.context.request.async.AsyncWebRequest#setTimeout(Long)} |
| 70 | + * @since 1.3.3 |
| 71 | + */ |
| 72 | + public GraphQlSseHandler(WebGraphQlHandler graphQlHandler, @Nullable Duration timeout) { |
51 | 73 | super(graphQlHandler, null);
|
| 74 | + this.timeout = timeout; |
52 | 75 | }
|
53 | 76 |
|
54 | 77 |
|
@@ -76,7 +99,9 @@ protected ServerResponse prepareResponse(
|
76 | 99 | .toSpecification());
|
77 | 100 | });
|
78 | 101 |
|
79 |
| - return ServerResponse.sse(SseSubscriber.connect(resultFlux)); |
| 102 | + return ((this.timeout != null) ? |
| 103 | + ServerResponse.sse(SseSubscriber.connect(resultFlux), this.timeout) : |
| 104 | + ServerResponse.sse(SseSubscriber.connect(resultFlux))); |
80 | 105 | }
|
81 | 106 |
|
82 | 107 |
|
|
0 commit comments