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
This commit is contained in:
@@ -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 {
|
||||
this.connectionInitTimeout = connectionInitTimeout;
|
||||
}
|
||||
|
||||
public Duration getKeepAlive() {
|
||||
return this.keepAlive;
|
||||
}
|
||||
|
||||
public void setKeepAlive(Duration keepAlive) {
|
||||
this.keepAlive = keepAlive;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Rsocket {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 @@
|
||||
|
||||
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 {
|
||||
.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) -> {
|
||||
|
||||
@@ -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 {
|
||||
});
|
||||
}
|
||||
|
||||
@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) -> {
|
||||
|
||||
Reference in New Issue
Block a user