Add idle timeout property for Reactor Netty

See gh-27371
pull/27409/head
Saraswathy Hariharakrishnan 3 years ago committed by Andy Wilkinson
parent 9d461a08c1
commit cbcd7b939b

@ -1300,6 +1300,12 @@ public class ServerProperties {
*/
private boolean validateHeaders = true;
/**
* IdleTimeout for netty server .If an idletimeout is not specified, this
* indicates no timeout(infinite).
*/
private Duration idleTimeout;
public Duration getConnectionTimeout() {
return this.connectionTimeout;
}
@ -1348,6 +1354,14 @@ public class ServerProperties {
this.validateHeaders = validateHeaders;
}
public Duration getIdleTimeout() {
return this.idleTimeout;
}
public void setIdleTimeout(Duration idleTimeout) {
this.idleTimeout = idleTimeout;
}
}
/**

@ -60,6 +60,8 @@ public class NettyWebServerFactoryCustomizer
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
propertyMapper.from(nettyProperties::getIdleTimeout).whenNonNull()
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
customizeRequestDecoder(factory, propertyMapper);
}
@ -98,4 +100,8 @@ public class NettyWebServerFactoryCustomizer
}));
}
private void customizeIdleTimeout(NettyReactiveWebServerFactory factory, Duration idleTimeout) {
factory.addServerCustomizers((httpServer) -> httpServer.idleTimeout(idleTimeout));
}
}

@ -535,6 +535,12 @@ class ServerPropertiesTests {
.isEqualTo(HttpDecoderSpec.DEFAULT_INITIAL_BUFFER_SIZE);
}
@Test
void testCustomizeNettyIdleTimeout() {
bind("server.netty.idle-timeout", "10s");
assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10));
}
private Connector getDefaultConnector() {
return new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
}

@ -104,12 +104,22 @@ class NettyWebServerFactoryCustomizerTests {
@Test
void setConnectionTimeout() {
setupConnectionTimeout(Duration.ofSeconds(1));
setServerProperties();
this.serverProperties.getNetty().setConnectionTimeout(Duration.ofSeconds(1));
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
verifyConnectionTimeout(factory, 1000);
}
@Test
void setIdleTimeout() {
setServerProperties();
this.serverProperties.getNetty().setIdleTimeout(Duration.ofSeconds(1));
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
verifyIdleTimeout(factory, Duration.ofSeconds(1));
}
@Test
void configureHttpRequestDecoder() {
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
@ -143,10 +153,21 @@ class NettyWebServerFactoryCustomizerTests {
assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected);
}
private void setupConnectionTimeout(Duration connectionTimeout) {
private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration expected) {
if (expected == null) {
verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));
return;
}
verify(factory, times(2)).addServerCustomizers(this.customizerCaptor.capture());
NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
Duration idleTimeout = httpServer.configuration().idleTimeout();
assertThat(idleTimeout).isEqualTo(expected);
}
private void setServerProperties() {
this.serverProperties.setForwardHeadersStrategy(ForwardHeadersStrategy.NONE);
this.serverProperties.setMaxHttpHeaderSize(null);
this.serverProperties.getNetty().setConnectionTimeout(connectionTimeout);
}
}

Loading…
Cancel
Save