Skip to content

Commit acb2465

Browse files
committed
Add keep-alive property for GraphQL SSE handlers
This commit adds a new `spring.graphql.http.sse.keep-alive` configuration property for MVC and WebFlux SSE handlers. Closes gh-44743
1 parent 95d5e35 commit acb2465

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java

+13
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,24 @@ public void setMapping(String mapping) {
315315

316316
public static class Sse {
317317

318+
/**
319+
* How frequently keep-alive messages should be sent.
320+
*/
321+
private Duration keepAlive;
322+
318323
/**
319324
* Time required for concurrent handling to complete.
320325
*/
321326
private Duration timeout;
322327

328+
public Duration getKeepAlive() {
329+
return this.keepAlive;
330+
}
331+
332+
public void setKeepAlive(Duration keepAlive) {
333+
this.keepAlive = keepAlive;
334+
}
335+
323336
public Duration getTimeout() {
324337
return this.timeout;
325338
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public GraphQlHttpHandler graphQlHttpHandler(WebGraphQlHandler webGraphQlHandler
9494
@Bean
9595
@ConditionalOnMissingBean
9696
public GraphQlSseHandler graphQlSseHandler(WebGraphQlHandler webGraphQlHandler, GraphQlProperties properties) {
97-
return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout());
97+
return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout(),
98+
properties.getHttp().getSse().getKeepAlive());
9899
}
99100

100101
@Bean

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public GraphQlHttpHandler graphQlHttpHandler(WebGraphQlHandler webGraphQlHandler
9898
@Bean
9999
@ConditionalOnMissingBean
100100
public GraphQlSseHandler graphQlSseHandler(WebGraphQlHandler webGraphQlHandler, GraphQlProperties properties) {
101-
return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout());
101+
return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout(),
102+
properties.getHttp().getSse().getKeepAlive());
102103
}
103104

104105
@Bean

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfigurationTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.graphql.server.WebGraphQlHandler;
4343
import org.springframework.graphql.server.WebGraphQlInterceptor;
4444
import org.springframework.graphql.server.webflux.GraphQlHttpHandler;
45+
import org.springframework.graphql.server.webflux.GraphQlSseHandler;
4546
import org.springframework.graphql.server.webflux.GraphQlWebSocketHandler;
4647
import org.springframework.http.HttpHeaders;
4748
import org.springframework.http.HttpStatus;
@@ -271,6 +272,24 @@ void shouldConfigureWebSocketProperties() {
271272
});
272273
}
273274

275+
@Test
276+
void shouldConfigureSseTimeout() {
277+
this.contextRunner.withPropertyValues("spring.graphql.http.sse.timeout=10s").run((context) -> {
278+
assertThat(context).hasSingleBean(GraphQlSseHandler.class);
279+
GraphQlSseHandler handler = context.getBean(GraphQlSseHandler.class);
280+
assertThat(handler).hasFieldOrPropertyWithValue("timeout", Duration.ofSeconds(10));
281+
});
282+
}
283+
284+
@Test
285+
void shouldConfigureSseKeepAlive() {
286+
this.contextRunner.withPropertyValues("spring.graphql.http.sse.keep-alive=5s").run((context) -> {
287+
assertThat(context).hasSingleBean(GraphQlSseHandler.class);
288+
GraphQlSseHandler handler = context.getBean(GraphQlSseHandler.class);
289+
assertThat(handler).hasFieldOrPropertyWithValue("keepAliveDuration", Duration.ofSeconds(5));
290+
});
291+
}
292+
274293
@Test
275294
void routerFunctionShouldHaveOrderZero() {
276295
this.contextRunner.withUserConfiguration(CustomRouterFunctions.class).run((context) -> {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ void shouldConfigureSseTimeout() {
108108
});
109109
}
110110

111+
@Test
112+
void shouldConfigureSseKeepAlive() {
113+
this.contextRunner.withPropertyValues("spring.graphql.http.sse.keep-alive=5s").run((context) -> {
114+
assertThat(context).hasSingleBean(GraphQlSseHandler.class);
115+
GraphQlSseHandler handler = context.getBean(GraphQlSseHandler.class);
116+
assertThat(handler).hasFieldOrPropertyWithValue("keepAliveDuration", Duration.ofSeconds(5));
117+
});
118+
}
119+
111120
@Test
112121
void simpleQueryShouldWork() {
113122
withMockMvc((mvc) -> {

0 commit comments

Comments
 (0)