Merge pull request #27371 from saraswathy-krish

* gh-27371:
  Polish "Add idle timeout property for Reactor Netty"
  Add idle timeout property for Reactor Netty

Closes gh-27371
pull/27409/head
Andy Wilkinson 3 years ago
commit 894daa9ea6

@ -1300,6 +1300,12 @@ public class ServerProperties {
*/ */
private boolean validateHeaders = true; private boolean validateHeaders = true;
/**
* Idle timeout of the Netty channel. When not specified, an infinite timeout is
* used.
*/
private Duration idleTimeout;
public Duration getConnectionTimeout() { public Duration getConnectionTimeout() {
return this.connectionTimeout; return this.connectionTimeout;
} }
@ -1348,6 +1354,14 @@ public class ServerProperties {
this.validateHeaders = validateHeaders; this.validateHeaders = validateHeaders;
} }
public Duration getIdleTimeout() {
return this.idleTimeout;
}
public void setIdleTimeout(Duration idleTimeout) {
this.idleTimeout = idleTimeout;
}
} }
/** /**

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,6 +60,8 @@ public class NettyWebServerFactoryCustomizer
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty(); ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull() propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout)); .to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
propertyMapper.from(nettyProperties::getIdleTimeout).whenNonNull()
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
customizeRequestDecoder(factory, propertyMapper); customizeRequestDecoder(factory, propertyMapper);
} }
@ -98,4 +100,8 @@ public class NettyWebServerFactoryCustomizer
})); }));
} }
private void customizeIdleTimeout(NettyReactiveWebServerFactory factory, Duration idleTimeout) {
factory.addServerCustomizers((httpServer) -> httpServer.idleTimeout(idleTimeout));
}
} }

@ -333,6 +333,12 @@ class ServerPropertiesTests {
assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path", "/b/path"); assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path", "/b/path");
} }
@Test
void testCustomizeNettyIdleTimeout() {
bind("server.netty.idle-timeout", "10s");
assertThat(this.properties.getNetty().getIdleTimeout()).isEqualTo(Duration.ofSeconds(10));
}
@Test @Test
void tomcatAcceptCountMatchesProtocolDefault() throws Exception { void tomcatAcceptCountMatchesProtocolDefault() throws Exception {
assertThat(this.properties.getTomcat().getAcceptCount()).isEqualTo(getDefaultProtocol().getAcceptCount()); assertThat(this.properties.getTomcat().getAcceptCount()).isEqualTo(getDefaultProtocol().getAcceptCount());

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,7 +30,6 @@ import reactor.netty.http.server.HttpRequestDecoderSpec;
import reactor.netty.http.server.HttpServer; import reactor.netty.http.server.HttpServer;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties.ForwardHeadersStrategy;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer; import org.springframework.boot.web.embedded.netty.NettyServerCustomizer;
@ -104,12 +103,20 @@ class NettyWebServerFactoryCustomizerTests {
@Test @Test
void setConnectionTimeout() { void setConnectionTimeout() {
setupConnectionTimeout(Duration.ofSeconds(1)); this.serverProperties.getNetty().setConnectionTimeout(Duration.ofSeconds(1));
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class); NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory); this.customizer.customize(factory);
verifyConnectionTimeout(factory, 1000); verifyConnectionTimeout(factory, 1000);
} }
@Test
void setIdleTimeout() {
this.serverProperties.getNetty().setIdleTimeout(Duration.ofSeconds(1));
NettyReactiveWebServerFactory factory = mock(NettyReactiveWebServerFactory.class);
this.customizer.customize(factory);
verifyIdleTimeout(factory, Duration.ofSeconds(1));
}
@Test @Test
void configureHttpRequestDecoder() { void configureHttpRequestDecoder() {
ServerProperties.Netty nettyProperties = this.serverProperties.getNetty(); ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
@ -143,10 +150,16 @@ class NettyWebServerFactoryCustomizerTests {
assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected); assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected);
} }
private void setupConnectionTimeout(Duration connectionTimeout) { private void verifyIdleTimeout(NettyReactiveWebServerFactory factory, Duration expected) {
this.serverProperties.setForwardHeadersStrategy(ForwardHeadersStrategy.NONE); if (expected == null) {
this.serverProperties.setMaxHttpHeaderSize(null); verify(factory, never()).addServerCustomizers(any(NettyServerCustomizer.class));
this.serverProperties.getNetty().setConnectionTimeout(connectionTimeout); 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);
} }
} }

Loading…
Cancel
Save