Skip to content

Commit

Permalink
Clean up code using the Builder pattern
Browse files Browse the repository at this point in the history
Support for exponential back off in response to HTTP throttle responses 502, 503, 504
Configuration property BackoffInterval to use as the interval for the default exponential back off strategy.  Default is 1000 msec.
Pluggable API to specify custom backoff strategy
Pluggable API to specify custom conversion of HTTP responses to exceptions
  • Loading branch information
elandau committed Oct 24, 2014
1 parent e3fb0bd commit 88174d0
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public abstract class CommonClientConfigKey<T> implements IClientConfigKey<T> {

public static final IClientConfigKey<Integer> ConnectTimeout = new CommonClientConfigKey<Integer>("ConnectTimeout"){};

public static final IClientConfigKey<Integer> BackoffInterval = new CommonClientConfigKey<Integer>("BackoffTimeout"){};

public static final IClientConfigKey<Integer> ReadTimeout = new CommonClientConfigKey<Integer>("ReadTimeout"){};

public static final IClientConfigKey<Integer> SendBufferSize = new CommonClientConfigKey<Integer>("SendBufferSize"){};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.netflix.config.DynamicProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;

import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
Expand Down Expand Up @@ -121,6 +122,8 @@ public class DefaultClientConfigImpl implements IClientConfig {

public static final int DEFAULT_MAX_AUTO_RETRIES = 0;

public static final int DEFAULT_BACKOFF_INTERVAL = 1000;

public static final int DEFAULT_READ_TIMEOUT = 5000;

public static final int DEFAULT_CONNECTION_MANAGER_TIMEOUT = 2000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,45 @@ public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient() {
}

public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(ILoadBalancer loadBalancer, IClientConfig config) {
return new LoadBalancingHttpClient<ByteBuf, ByteBuf>(loadBalancer, config, getDefaultHttpRetryHandlerWithConfig(config), DEFAULT_HTTP_PIPELINE_CONFIGURATOR, poolCleanerScheduler);
return LoadBalancingHttpClient.<ByteBuf, ByteBuf>builder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(DEFAULT_HTTP_PIPELINE_CONFIGURATOR)
.withPoolCleanerScheduler(poolCleanerScheduler)
.build();
}

public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(ILoadBalancer loadBalancer, IClientConfig config, RetryHandler retryHandler) {
return new LoadBalancingHttpClient<ByteBuf, ByteBuf>(loadBalancer, config, retryHandler, DEFAULT_HTTP_PIPELINE_CONFIGURATOR, poolCleanerScheduler);
return LoadBalancingHttpClient.<ByteBuf, ByteBuf>builder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(retryHandler)
.withPipelineConfigurator(DEFAULT_HTTP_PIPELINE_CONFIGURATOR)
.withPoolCleanerScheduler(poolCleanerScheduler)
.build();
}

public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(ILoadBalancer loadBalancer, IClientConfig config, RetryHandler retryHandler,
List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners) {
return new LoadBalancingHttpClient<ByteBuf, ByteBuf>(loadBalancer, config, retryHandler, DEFAULT_HTTP_PIPELINE_CONFIGURATOR, poolCleanerScheduler, listeners);
return LoadBalancingHttpClient.<ByteBuf, ByteBuf>builder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(retryHandler)
.withPipelineConfigurator(DEFAULT_HTTP_PIPELINE_CONFIGURATOR)
.withPoolCleanerScheduler(poolCleanerScheduler)
.withExecutorListeners(listeners)
.build();
}


public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(IClientConfig config) {
return new LoadBalancingHttpClient<ByteBuf, ByteBuf>(config, getDefaultHttpRetryHandlerWithConfig(config), DEFAULT_HTTP_PIPELINE_CONFIGURATOR, poolCleanerScheduler);
return LoadBalancingHttpClient.<ByteBuf, ByteBuf>builder()
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(DEFAULT_HTTP_PIPELINE_CONFIGURATOR)
.withPoolCleanerScheduler(poolCleanerScheduler)
.build();
}

public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(ILoadBalancer loadBalancer) {
Expand All @@ -140,41 +164,82 @@ public static LoadBalancingHttpClient<ByteBuf, ByteBuf> newHttpClient(ILoadBalan

public static <I, O> LoadBalancingHttpClient<I, O> newHttpClient(PipelineConfigurator<HttpClientResponse<O>, HttpClientRequest<I>> pipelineConfigurator,
ILoadBalancer loadBalancer, IClientConfig config) {
return new LoadBalancingHttpClient<I, O>(loadBalancer, config, getDefaultHttpRetryHandlerWithConfig(config), pipelineConfigurator, poolCleanerScheduler);
return LoadBalancingHttpClient.<I, O>builder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(pipelineConfigurator)
.withPoolCleanerScheduler(poolCleanerScheduler)
.build();
}

public static <I, O> LoadBalancingHttpClient<I, O> newHttpClient(PipelineConfigurator<HttpClientResponse<O>, HttpClientRequest<I>> pipelineConfigurator,
IClientConfig config) {
return new LoadBalancingHttpClient<I, O>(config, getDefaultHttpRetryHandlerWithConfig(config), pipelineConfigurator, poolCleanerScheduler);
return LoadBalancingHttpClient.<I, O>builder()
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(pipelineConfigurator)
.withPoolCleanerScheduler(poolCleanerScheduler)
.build();
}

public static <I, O> LoadBalancingHttpClient<I, O> newHttpClient(PipelineConfigurator<HttpClientResponse<O>, HttpClientRequest<I>> pipelineConfigurator,
IClientConfig config, RetryHandler retryHandler) {
return new LoadBalancingHttpClient<I, O>(config, retryHandler, pipelineConfigurator, poolCleanerScheduler);
return LoadBalancingHttpClient.<I, O>builder()
.withClientConfig(config)
.withRetryHandler(retryHandler)
.withPipelineConfigurator(pipelineConfigurator)
.withPoolCleanerScheduler(poolCleanerScheduler)
.build();
}

public static <I, O> LoadBalancingHttpClient<I, O> newHttpClient(PipelineConfigurator<HttpClientResponse<O>, HttpClientRequest<I>> pipelineConfigurator,
ILoadBalancer loadBalancer, IClientConfig config, RetryHandler retryHandler,
List<ExecutionListener<HttpClientRequest<I>, HttpClientResponse<O>>> listeners) {
return new LoadBalancingHttpClient<I, O>(loadBalancer, config, retryHandler, pipelineConfigurator, poolCleanerScheduler, listeners);
return LoadBalancingHttpClient.<I, O>builder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(retryHandler)
.withPipelineConfigurator(pipelineConfigurator)
.withPoolCleanerScheduler(poolCleanerScheduler)
.withExecutorListeners(listeners)
.build();
}

public static LoadBalancingHttpClient<ByteBuf, ServerSentEvent> newSSEClient(ILoadBalancer loadBalancer, IClientConfig config) {
return new SSEClient<ByteBuf>(loadBalancer, config, getDefaultHttpRetryHandlerWithConfig(config), DEFAULT_SSE_PIPELINE_CONFIGURATOR);
return SSEClient.<ByteBuf>sseClientBuilder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(DEFAULT_SSE_PIPELINE_CONFIGURATOR)
.build();
}

public static LoadBalancingHttpClient<ByteBuf, ServerSentEvent> newSSEClient(IClientConfig config) {
return new SSEClient<ByteBuf>(config, getDefaultHttpRetryHandlerWithConfig(config), DEFAULT_SSE_PIPELINE_CONFIGURATOR);
return SSEClient.<ByteBuf>sseClientBuilder()
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(DEFAULT_SSE_PIPELINE_CONFIGURATOR)
.build();
}

public static <I> LoadBalancingHttpClient<I, ServerSentEvent> newSSEClient(PipelineConfigurator<HttpClientResponse<ServerSentEvent>, HttpClientRequest<I>> pipelineConfigurator,
ILoadBalancer loadBalancer, IClientConfig config) {
return new SSEClient<I>(loadBalancer, config, getDefaultHttpRetryHandlerWithConfig(config), pipelineConfigurator);
return SSEClient.<I>sseClientBuilder()
.withLoadBalancer(loadBalancer)
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(pipelineConfigurator)
.build();
}

public static <I> LoadBalancingHttpClient<I, ServerSentEvent> newSSEClient(PipelineConfigurator<HttpClientResponse<ServerSentEvent>, HttpClientRequest<I>> pipelineConfigurator,
IClientConfig config) {
return new SSEClient<I>(config, getDefaultHttpRetryHandlerWithConfig(config), pipelineConfigurator);
return SSEClient.<I>sseClientBuilder()
.withClientConfig(config)
.withRetryHandler(getDefaultHttpRetryHandlerWithConfig(config))
.withPipelineConfigurator(pipelineConfigurator)
.build();
}

public static LoadBalancingHttpClient<ByteBuf, ServerSentEvent> newSSEClient() {
Expand Down
Loading

0 comments on commit 88174d0

Please sign in to comment.