Browse Source

Add spring.graphql.websocket.keep-alive property

As of spring-projects/spring-graphql#534, Spring for GraphQL supports
the configuration of keep-alive PINGs for WebSocket connections.
This commit auto-configures this value in the `GraphQlWebSocketHandler`
WebFlux and MVC implementations if the
`spring.graphql.websocket.keep-alive` property is configured.

Closes gh-40320
pull/40339/head
Brian Clozel 2 years ago
parent
commit
04faec1d3e
  1. 13
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java
  2. 2
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfiguration.java
  3. 2
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java
  4. 17
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfigurationTests.java
  5. 15
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java

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

@ -217,6 +217,11 @@ public class GraphQlProperties { @@ -217,6 +217,11 @@ public class GraphQlProperties {
*/
private Duration connectionInitTimeout = Duration.ofSeconds(60);
/**
* Maximum idle period before a server keep-alive ping is sent to client.
*/
private Duration keepAlive = null;
public String getPath() {
return this.path;
}
@ -233,6 +238,14 @@ public class GraphQlProperties { @@ -233,6 +238,14 @@ public class GraphQlProperties {
this.connectionInitTimeout = connectionInitTimeout;
}
public Duration getKeepAlive() {
return this.keepAlive;
}
public void setKeepAlive(Duration keepAlive) {
this.keepAlive = keepAlive;
}
}
public static class Rsocket {

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

@ -163,7 +163,7 @@ public class GraphQlWebFluxAutoConfiguration { @@ -163,7 +163,7 @@ public class GraphQlWebFluxAutoConfiguration {
public GraphQlWebSocketHandler graphQlWebSocketHandler(WebGraphQlHandler webGraphQlHandler,
GraphQlProperties properties, ServerCodecConfigurer configurer) {
return new GraphQlWebSocketHandler(webGraphQlHandler, configurer,
properties.getWebsocket().getConnectionInitTimeout());
properties.getWebsocket().getConnectionInitTimeout(), properties.getWebsocket().getKeepAlive());
}
@Bean

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

@ -169,7 +169,7 @@ public class GraphQlWebMvcAutoConfiguration { @@ -169,7 +169,7 @@ public class GraphQlWebMvcAutoConfiguration {
public GraphQlWebSocketHandler graphQlWebSocketHandler(WebGraphQlHandler webGraphQlHandler,
GraphQlProperties properties, HttpMessageConverters converters) {
return new GraphQlWebSocketHandler(webGraphQlHandler, getJsonConverter(converters),
properties.getWebsocket().getConnectionInitTimeout());
properties.getWebsocket().getConnectionInitTimeout(), properties.getWebsocket().getKeepAlive());
}
private GenericHttpMessageConverter<Object> getJsonConverter(HttpMessageConverters converters) {

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.graphql.reactive;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
@ -220,6 +221,20 @@ class GraphQlWebFluxAutoConfigurationTests { @@ -220,6 +221,20 @@ class GraphQlWebFluxAutoConfigurationTests {
.run((context) -> assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class));
}
@Test
void shouldConfigureWebSocketProperties() {
this.contextRunner
.withPropertyValues("spring.graphql.websocket.path=/ws",
"spring.graphql.websocket.connection-init-timeout=120s", "spring.graphql.websocket.keep-alive=30s")
.run((context) -> {
assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class);
GraphQlWebSocketHandler graphQlWebSocketHandler = context.getBean(GraphQlWebSocketHandler.class);
assertThat(graphQlWebSocketHandler).extracting("initTimeoutDuration")
.isEqualTo(Duration.ofSeconds(120));
assertThat(graphQlWebSocketHandler).extracting("keepAliveDuration").isEqualTo(Duration.ofSeconds(30));
});
}
@Test
void routerFunctionShouldHaveOrderZero() {
this.contextRunner.withUserConfiguration(CustomRouterFunctions.class).run((context) -> {

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

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.graphql.servlet;
import java.time.Duration;
import java.util.Map;
import graphql.schema.idl.TypeRuntimeWiring;
@ -184,6 +185,20 @@ class GraphQlWebMvcAutoConfigurationTests { @@ -184,6 +185,20 @@ class GraphQlWebMvcAutoConfigurationTests {
});
}
@Test
void shouldConfigureWebSocketProperties() {
this.contextRunner
.withPropertyValues("spring.graphql.websocket.path=/ws",
"spring.graphql.websocket.connection-init-timeout=120s", "spring.graphql.websocket.keep-alive=30s")
.run((context) -> {
assertThat(context).hasSingleBean(GraphQlWebSocketHandler.class);
GraphQlWebSocketHandler graphQlWebSocketHandler = context.getBean(GraphQlWebSocketHandler.class);
assertThat(graphQlWebSocketHandler).extracting("initTimeoutDuration")
.isEqualTo(Duration.ofSeconds(120));
assertThat(graphQlWebSocketHandler).extracting("keepAliveDuration").isEqualTo(Duration.ofSeconds(30));
});
}
@Test
void routerFunctionShouldHaveOrderZero() {
this.contextRunner.withUserConfiguration(CustomRouterFunctions.class).run((context) -> {

Loading…
Cancel
Save