Commit ed31004a by Ryan Baxter

Uses configuration from IConfigClient reference to always use the most up to…

Uses configuration from IConfigClient reference to always use the most up to date configuration. Fixes #1481.
parent fe50674c
...@@ -75,12 +75,13 @@ public class RibbonLoadBalancingHttpClient ...@@ -75,12 +75,13 @@ public class RibbonLoadBalancingHttpClient
public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request, public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request,
final IClientConfig configOverride) throws Exception { final IClientConfig configOverride) throws Exception {
final RequestConfig.Builder builder = RequestConfig.custom(); final RequestConfig.Builder builder = RequestConfig.custom();
if (configOverride != null) { IClientConfig config = configOverride != null ? configOverride : this.config;
builder.setConnectTimeout(configOverride.get( if (config != null) {
builder.setConnectTimeout(config.get(
CommonClientConfigKey.ConnectTimeout, this.connectTimeout)); CommonClientConfigKey.ConnectTimeout, this.connectTimeout));
builder.setSocketTimeout(configOverride.get( builder.setSocketTimeout(config.get(
CommonClientConfigKey.ReadTimeout, this.readTimeout)); CommonClientConfigKey.ReadTimeout, this.readTimeout));
builder.setRedirectsEnabled(configOverride.get( builder.setRedirectsEnabled(config.get(
CommonClientConfigKey.FollowRedirects, this.followRedirects)); CommonClientConfigKey.FollowRedirects, this.followRedirects));
} }
else { else {
......
...@@ -16,22 +16,20 @@ ...@@ -16,22 +16,20 @@
package org.springframework.cloud.netflix.ribbon.okhttp; package org.springframework.cloud.netflix.ribbon.okhttp;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.net.URI; import java.net.URI;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector; import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.netflix.ribbon.support.AbstractLoadBalancingClient; import org.springframework.cloud.netflix.ribbon.support.AbstractLoadBalancingClient;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import com.netflix.client.config.CommonClientConfigKey; import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToHttpsIfNeeded; import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToHttpsIfNeeded;
/** /**
...@@ -85,12 +83,13 @@ public class OkHttpLoadBalancingClient ...@@ -85,12 +83,13 @@ public class OkHttpLoadBalancingClient
OkHttpClient getOkHttpClient(IClientConfig configOverride, boolean secure) { OkHttpClient getOkHttpClient(IClientConfig configOverride, boolean secure) {
OkHttpClient.Builder builder = this.delegate.newBuilder(); OkHttpClient.Builder builder = this.delegate.newBuilder();
if (configOverride != null) { IClientConfig config = configOverride != null ? configOverride : this.config;
builder.connectTimeout(configOverride.get( if (config != null) {
builder.connectTimeout(config.get(
CommonClientConfigKey.ConnectTimeout, this.connectTimeout), TimeUnit.MILLISECONDS); CommonClientConfigKey.ConnectTimeout, this.connectTimeout), TimeUnit.MILLISECONDS);
builder.readTimeout(configOverride.get( builder.readTimeout(config.get(
CommonClientConfigKey.ReadTimeout, this.readTimeout), TimeUnit.MILLISECONDS); CommonClientConfigKey.ReadTimeout, this.readTimeout), TimeUnit.MILLISECONDS);
builder.followRedirects(configOverride.get( builder.followRedirects(config.get(
CommonClientConfigKey.FollowRedirects, this.followRedirects)); CommonClientConfigKey.FollowRedirects, this.followRedirects));
if (secure) { if (secure) {
builder.followSslRedirects(configOverride.get( builder.followSslRedirects(configOverride.get(
......
...@@ -114,6 +114,21 @@ public class RibbonLoadBalancingHttpClientTests { ...@@ -114,6 +114,21 @@ public class RibbonLoadBalancingHttpClientTests {
assertThat(result.isRedirectsEnabled(), is(false)); assertThat(result.isRedirectsEnabled(), is(false));
} }
@Test
public void testUpdatedTimeouts()
throws Exception {
SpringClientFactory factory = new SpringClientFactory();
RequestConfig result = getBuiltRequestConfig(Timeouts.class, null, factory);
assertThat(result.getConnectTimeout(), is(60000));
assertThat(result.getSocketTimeout(), is (50000));
IClientConfig config = factory.getClientConfig("service");
config.set(CommonClientConfigKey.ConnectTimeout, 60);
config.set(CommonClientConfigKey.ReadTimeout, 50);
result = getBuiltRequestConfig(Timeouts.class, null, factory);
assertThat(result.getConnectTimeout(), is(60));
assertThat(result.getSocketTimeout(), is (50));
}
@Configuration @Configuration
protected static class UseDefaults { protected static class UseDefaults {
...@@ -164,8 +179,13 @@ public class RibbonLoadBalancingHttpClientTests { ...@@ -164,8 +179,13 @@ public class RibbonLoadBalancingHttpClientTests {
private RequestConfig getBuiltRequestConfig(Class<?> defaultConfigurationClass, private RequestConfig getBuiltRequestConfig(Class<?> defaultConfigurationClass,
IClientConfig configOverride) throws Exception { IClientConfig configOverride) throws Exception {
return getBuiltRequestConfig(defaultConfigurationClass, configOverride, new SpringClientFactory());
}
private RequestConfig getBuiltRequestConfig(Class<?> defaultConfigurationClass,
IClientConfig configOverride, SpringClientFactory factory)
throws Exception {
SpringClientFactory factory = new SpringClientFactory();
factory.setApplicationContext(new AnnotationConfigApplicationContext( factory.setApplicationContext(new AnnotationConfigApplicationContext(
RibbonAutoConfiguration.class, defaultConfigurationClass)); RibbonAutoConfiguration.class, defaultConfigurationClass));
HttpClient delegate = mock(HttpClient.class); HttpClient delegate = mock(HttpClient.class);
......
...@@ -16,8 +16,7 @@ ...@@ -16,8 +16,7 @@
package org.springframework.cloud.netflix.ribbon.okhttp; package org.springframework.cloud.netflix.ribbon.okhttp;
import static org.hamcrest.Matchers.is; import okhttp3.OkHttpClient;
import static org.junit.Assert.assertThat;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
...@@ -25,12 +24,12 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; ...@@ -25,12 +24,12 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.netflix.client.config.CommonClientConfigKey; import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl; import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig; import com.netflix.client.config.IClientConfig;
import okhttp3.OkHttpClient; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
...@@ -84,9 +83,45 @@ public class OkHttpLoadBalancingClientTests { ...@@ -84,9 +83,45 @@ public class OkHttpLoadBalancingClientTests {
assertThat(result.followRedirects(), is(false)); assertThat(result.followRedirects(), is(false));
} }
@Test
public void testTimeouts() throws Exception {
OkHttpClient result = getHttpClient(Timeouts.class, null);
assertThat(result.readTimeoutMillis(), is(50000));
assertThat(result.connectTimeoutMillis(), is(60000));
}
@Test
public void testTimeoutsOverride() throws Exception {
DefaultClientConfigImpl override = new DefaultClientConfigImpl();
override.set(CommonClientConfigKey.ConnectTimeout, 60);
override.set(CommonClientConfigKey.ReadTimeout, 50);
OkHttpClient result = getHttpClient(Timeouts.class, override);
assertThat(result.readTimeoutMillis(), is(50));
assertThat(result.connectTimeoutMillis(), is(60));
}
@Test
public void testUpdatedTimeouts() throws Exception {
SpringClientFactory factory = new SpringClientFactory();
OkHttpClient result = getHttpClient(Timeouts.class, null, factory);
assertThat(result.readTimeoutMillis(), is(50000));
assertThat(result.connectTimeoutMillis(), is(60000));
IClientConfig config = factory.getClientConfig("service");
config.set(CommonClientConfigKey.ConnectTimeout, 60);
config.set(CommonClientConfigKey.ReadTimeout, 50);
result = getHttpClient(Timeouts.class, null, factory);
assertThat(result.readTimeoutMillis(), is(50));
assertThat(result.connectTimeoutMillis(), is(60));
}
private OkHttpClient getHttpClient(Class<?> defaultConfigurationClass, private OkHttpClient getHttpClient(Class<?> defaultConfigurationClass,
IClientConfig configOverride) throws Exception { IClientConfig configOverride) throws Exception {
SpringClientFactory factory = new SpringClientFactory(); return getHttpClient(defaultConfigurationClass, configOverride, new SpringClientFactory());
}
private OkHttpClient getHttpClient(Class<?> defaultConfigurationClass,
IClientConfig configOverride,
SpringClientFactory factory) throws Exception {
factory.setApplicationContext(new AnnotationConfigApplicationContext( factory.setApplicationContext(new AnnotationConfigApplicationContext(
RibbonAutoConfiguration.class, defaultConfigurationClass)); RibbonAutoConfiguration.class, defaultConfigurationClass));
...@@ -121,4 +156,15 @@ public class OkHttpLoadBalancingClientTests { ...@@ -121,4 +156,15 @@ public class OkHttpLoadBalancingClientTests {
} }
} }
@Configuration
protected static class Timeouts {
@Bean
public IClientConfig clientConfig() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.set(CommonClientConfigKey.ConnectTimeout, 60000);
config.set(CommonClientConfigKey.ReadTimeout, 50000);
return config;
}
}
} }
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