Adds connectionRequestTimeoutMillis to zuul.host

fixes gh-2851
parent 0a8f594b
...@@ -18,6 +18,7 @@ package org.springframework.cloud.netflix.zuul.filters; ...@@ -18,6 +18,7 @@ package org.springframework.cloud.netflix.zuul.filters;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy; import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -439,6 +440,11 @@ public class ZuulProperties { ...@@ -439,6 +440,11 @@ public class ZuulProperties {
*/ */
private int connectTimeoutMillis = 2000; private int connectTimeoutMillis = 2000;
/** /**
* The timeout in milliseconds used when requesting a connection
* from the connection manager. Defaults to -1, undefined use the system default.
*/
private int connectionRequestTimeoutMillis = -1;
/**
* The lifetime for the connection pool. * The lifetime for the connection pool.
*/ */
private long timeToLive = -1; private long timeToLive = -1;
...@@ -493,6 +499,14 @@ public class ZuulProperties { ...@@ -493,6 +499,14 @@ public class ZuulProperties {
this.connectTimeoutMillis = connectTimeoutMillis; this.connectTimeoutMillis = connectTimeoutMillis;
} }
public int getConnectionRequestTimeoutMillis() {
return connectionRequestTimeoutMillis;
}
public void setConnectionRequestTimeoutMillis(int connectionRequestTimeoutMillis) {
this.connectionRequestTimeoutMillis = connectionRequestTimeoutMillis;
}
public long getTimeToLive() { public long getTimeToLive() {
return timeToLive; return timeToLive;
} }
...@@ -518,26 +532,28 @@ public class ZuulProperties { ...@@ -518,26 +532,28 @@ public class ZuulProperties {
maxPerRouteConnections == host.maxPerRouteConnections && maxPerRouteConnections == host.maxPerRouteConnections &&
socketTimeoutMillis == host.socketTimeoutMillis && socketTimeoutMillis == host.socketTimeoutMillis &&
connectTimeoutMillis == host.connectTimeoutMillis && connectTimeoutMillis == host.connectTimeoutMillis &&
connectionRequestTimeoutMillis == host.connectionRequestTimeoutMillis &&
timeToLive == host.timeToLive && timeToLive == host.timeToLive &&
timeUnit == host.timeUnit; timeUnit == host.timeUnit;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(maxTotalConnections, maxPerRouteConnections, socketTimeoutMillis, connectTimeoutMillis, timeToLive, timeUnit); return Objects.hash(maxTotalConnections, maxPerRouteConnections, socketTimeoutMillis, connectTimeoutMillis,
connectionRequestTimeoutMillis, timeToLive, timeUnit);
} }
@Override @Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder("Host{"); return new ToStringCreator(this)
sb.append("maxTotalConnections=").append(maxTotalConnections); .append("maxTotalConnections", maxTotalConnections)
sb.append(", maxPerRouteConnections=").append(maxPerRouteConnections); .append("maxPerRouteConnections", maxPerRouteConnections)
sb.append(", socketTimeoutMillis=").append(socketTimeoutMillis); .append("socketTimeoutMillis", socketTimeoutMillis)
sb.append(", connectTimeoutMillis=").append(connectTimeoutMillis); .append("connectTimeoutMillis", connectTimeoutMillis)
sb.append(", timeToLive=").append(timeToLive); .append("connectionRequestTimeoutMillis", connectionRequestTimeoutMillis)
sb.append(", timeUnit=").append(timeUnit); .append("timeToLive", timeToLive)
sb.append('}'); .append("timeUnit", timeUnit)
return sb.toString(); .toString();
} }
} }
......
...@@ -217,6 +217,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter { ...@@ -217,6 +217,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
protected CloseableHttpClient newClient() { protected CloseableHttpClient newClient() {
final RequestConfig requestConfig = RequestConfig.custom() final RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(this.hostProperties.getConnectionRequestTimeoutMillis())
.setSocketTimeout(this.hostProperties.getSocketTimeoutMillis()) .setSocketTimeout(this.hostProperties.getSocketTimeoutMillis())
.setConnectTimeout(this.hostProperties.getConnectTimeoutMillis()) .setConnectTimeout(this.hostProperties.getConnectTimeoutMillis())
.setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
......
...@@ -92,7 +92,7 @@ import static org.springframework.util.StreamUtils.copyToString; ...@@ -92,7 +92,7 @@ import static org.springframework.util.StreamUtils.copyToString;
* @author Gang Li * @author Gang Li
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = SampleApplication.class, @SpringBootTest(classes = SimpleHostRoutingFilterTests.SampleApplication.class,
webEnvironment = RANDOM_PORT, webEnvironment = RANDOM_PORT,
properties = {"server.servlet.contextPath: /app"}) properties = {"server.servlet.contextPath: /app"})
@DirtiesContext @DirtiesContext
...@@ -119,13 +119,14 @@ public class SimpleHostRoutingFilterTests { ...@@ -119,13 +119,14 @@ public class SimpleHostRoutingFilterTests {
@Test @Test
public void timeoutPropertiesAreApplied() { public void timeoutPropertiesAreApplied() {
addEnvironment(this.context, "zuul.host.socket-timeout-millis=11000", addEnvironment(this.context, "zuul.host.socket-timeout-millis=11000",
"zuul.host.connect-timeout-millis=2100"); "zuul.host.connect-timeout-millis=2100", "zuul.host.connection-request-timeout-millis=2500");
setupContext(); setupContext();
CloseableHttpClient httpClient = getFilter().newClient(); CloseableHttpClient httpClient = getFilter().newClient();
Assertions.assertThat(httpClient).isInstanceOf(Configurable.class); Assertions.assertThat(httpClient).isInstanceOf(Configurable.class);
RequestConfig config = ((Configurable) httpClient).getConfig(); RequestConfig config = ((Configurable) httpClient).getConfig();
assertEquals(11000, config.getSocketTimeout()); assertEquals(11000, config.getSocketTimeout());
assertEquals(2100, config.getConnectTimeout()); assertEquals(2100, config.getConnectTimeout());
assertEquals(2500, config.getConnectionRequestTimeout());
} }
@Test @Test
...@@ -180,7 +181,7 @@ public class SimpleHostRoutingFilterTests { ...@@ -180,7 +181,7 @@ public class SimpleHostRoutingFilterTests {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("DELETE", "uri", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("DELETE", "uri", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
assertTrue(httpRequest instanceof HttpEntityEnclosingRequest); assertTrue(httpRequest instanceof HttpEntityEnclosingRequest);
HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest; HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
...@@ -192,7 +193,7 @@ public class SimpleHostRoutingFilterTests { ...@@ -192,7 +193,7 @@ public class SimpleHostRoutingFilterTests {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/compressed/get/1", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/compressed/get/1", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest); CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
...@@ -205,7 +206,7 @@ public class SimpleHostRoutingFilterTests { ...@@ -205,7 +206,7 @@ public class SimpleHostRoutingFilterTests {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/get/1", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/get/1", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest); CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
...@@ -215,11 +216,11 @@ public class SimpleHostRoutingFilterTests { ...@@ -215,11 +216,11 @@ public class SimpleHostRoutingFilterTests {
@Test @Test
public void redirectTest() throws Exception { public void redirectTest() throws IOException {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{}));
HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/redirect", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("GET", "/app/redirect", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest); CloseableHttpResponse response = getFilter().newClient().execute(new HttpHost("localhost", this.port), httpRequest);
assertEquals(302, response.getStatusLine().getStatusCode()); assertEquals(302, response.getStatusLine().getStatusCode());
...@@ -252,11 +253,11 @@ public class SimpleHostRoutingFilterTests { ...@@ -252,11 +253,11 @@ public class SimpleHostRoutingFilterTests {
} }
@Test @Test
public void putRequestBuiltWithBody() throws Exception { public void putRequestBuiltWithBody() {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("PUT", "uri", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("PUT", "uri", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
assertTrue(httpRequest instanceof HttpEntityEnclosingRequest); assertTrue(httpRequest instanceof HttpEntityEnclosingRequest);
HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest; HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
...@@ -264,11 +265,11 @@ public class SimpleHostRoutingFilterTests { ...@@ -264,11 +265,11 @@ public class SimpleHostRoutingFilterTests {
} }
@Test @Test
public void postRequestBuiltWithBody() throws Exception { public void postRequestBuiltWithBody() {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("POST", "uri", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("POST", "uri", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
assertTrue(httpRequest instanceof HttpEntityEnclosingRequest); assertTrue(httpRequest instanceof HttpEntityEnclosingRequest);
HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest; HttpEntityEnclosingRequest httpEntityEnclosingRequest = (HttpEntityEnclosingRequest) httpRequest;
...@@ -276,18 +277,18 @@ public class SimpleHostRoutingFilterTests { ...@@ -276,18 +277,18 @@ public class SimpleHostRoutingFilterTests {
} }
@Test @Test
public void pathRequestBuiltWithBody() throws Exception { public void pathRequestBuiltWithBody() {
setupContext(); setupContext();
InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1})); InputStreamEntity inputStreamEntity = new InputStreamEntity(new ByteArrayInputStream(new byte[]{1}));
HttpRequest httpRequest = getFilter().buildHttpRequest("PATCH", "uri", inputStreamEntity, HttpRequest httpRequest = getFilter().buildHttpRequest("PATCH", "uri", inputStreamEntity,
new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new MockHttpServletRequest()); new LinkedMultiValueMap<>(), new LinkedMultiValueMap<>(), new MockHttpServletRequest());
HttpPatch basicHttpRequest = (HttpPatch) httpRequest; HttpPatch basicHttpRequest = (HttpPatch) httpRequest;
assertTrue(basicHttpRequest.getEntity() != null); assertTrue(basicHttpRequest.getEntity() != null);
} }
@Test @Test
public void shouldFilterFalse() throws Exception { public void shouldFilterFalse() {
setupContext(); setupContext();
assertEquals(false, getFilter().shouldFilter()); assertEquals(false, getFilter().shouldFilter());
} }
...@@ -301,7 +302,7 @@ public class SimpleHostRoutingFilterTests { ...@@ -301,7 +302,7 @@ public class SimpleHostRoutingFilterTests {
} }
@Test @Test
public void filterOrder() throws Exception { public void filterOrder() {
setupContext(); setupContext();
assertEquals(100, getFilter().filterOrder()); assertEquals(100, getFilter().filterOrder());
} }
...@@ -351,16 +352,11 @@ public class SimpleHostRoutingFilterTests { ...@@ -351,16 +352,11 @@ public class SimpleHostRoutingFilterTests {
return new SimpleHostRoutingFilter(new ProxyRequestHelper(), zuulProperties, connectionManagerFactory, clientFactory); return new SimpleHostRoutingFilter(new ProxyRequestHelper(), zuulProperties, connectionManagerFactory, clientFactory);
} }
} }
}
@Configuration
@EnableAutoConfiguration
@RestController
class SampleApplication {
public static void main(String[] args) { @Configuration
SpringApplication.run(SampleApplication.class, args); @EnableAutoConfiguration
} @RestController
static class SampleApplication {
@RequestMapping(value = "/compressed/get/{id}", method = RequestMethod.GET) @RequestMapping(value = "/compressed/get/{id}", method = RequestMethod.GET)
public byte[] getCompressed(@PathVariable String id, HttpServletResponse response) throws IOException { public byte[] getCompressed(@PathVariable String id, HttpServletResponse response) throws IOException {
...@@ -378,9 +374,9 @@ class SampleApplication { ...@@ -378,9 +374,9 @@ class SampleApplication {
response.sendRedirect("/app/get/5"); response.sendRedirect("/app/get/5");
return null; return null;
} }
} }
class GZIPCompression { static class GZIPCompression {
public static byte[] compress(final String str) throws IOException { public static byte[] compress(final String str) throws IOException {
if ((str == null) || (str.length() == 0)) { if ((str == null) || (str.length() == 0)) {
...@@ -392,4 +388,5 @@ class GZIPCompression { ...@@ -392,4 +388,5 @@ class GZIPCompression {
gzip.close(); gzip.close();
return obj.toByteArray(); return obj.toByteArray();
} }
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment