Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ project(':feign-ribbon') {

dependencies {
compile project(':feign-core')
compile 'com.netflix.ribbon:ribbon-core:0.2.4'
compile 'com.netflix.ribbon:ribbon-loadbalancer:2.0-RC5'
testCompile 'org.testng:testng:6.8.5'
testCompile 'com.google.mockwebserver:mockwebserver:20130706'
}
Expand Down
66 changes: 34 additions & 32 deletions ribbon/src/main/java/feign/ribbon/LBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import com.netflix.client.ClientException;
import com.netflix.client.ClientRequest;
import com.netflix.client.IResponse;
import com.netflix.client.RequestSpecificRetryHandler;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.util.Pair;

import java.io.IOException;
import java.net.URI;
Expand All @@ -35,48 +35,47 @@
import feign.Response;
import feign.RetryableException;

import static com.netflix.client.config.CommonClientConfigKey.ConnectTimeout;
import static com.netflix.client.config.CommonClientConfigKey.ReadTimeout;

class LBClient extends AbstractLoadBalancerAwareClient<LBClient.RibbonRequest, LBClient.RibbonResponse> {

private final Client delegate;
private final int connectTimeout;
private final int readTimeout;

LBClient(Client delegate, ILoadBalancer lb, IClientConfig clientConfig) {
super(lb, clientConfig);
this.delegate = delegate;
this.connectTimeout = Integer.valueOf(clientConfig.getProperty(ConnectTimeout).toString());
this.readTimeout = Integer.valueOf(clientConfig.getProperty(ReadTimeout).toString());
setLoadBalancer(lb);
initWithNiwsConfig(clientConfig);
connectTimeout = clientConfig.get(CommonClientConfigKey.ConnectTimeout);
readTimeout = clientConfig.get(CommonClientConfigKey.ReadTimeout);
}

@Override
public RibbonResponse execute(RibbonRequest request) throws IOException {
int connectTimeout = config(request, ConnectTimeout, this.connectTimeout);
int readTimeout = config(request, ReadTimeout, this.readTimeout);

Request.Options options = new Request.Options(connectTimeout, readTimeout);
public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride) throws IOException {
Request.Options options;
if (configOverride != null) {
options = new Request.Options(configOverride.get(CommonClientConfigKey.ConnectTimeout, connectTimeout),
(configOverride.get(CommonClientConfigKey.ReadTimeout, readTimeout)));
} else {
options = new Request.Options(connectTimeout, readTimeout);
}
Response response = delegate.execute(request.toRequest(), options);
return new RibbonResponse(request.getUri(), response);
}

@Override protected boolean isCircuitBreakerException(Exception e) {
return e instanceof IOException;
}

@Override protected boolean isRetriableException(Exception e) {
return e instanceof RetryableException;
}

@Override
protected Pair<String, Integer> deriveSchemeAndPortFromPartialUri(RibbonRequest task) {
return new Pair<String, Integer>(URI.create(task.request.url()).getScheme(), task.getUri().getPort());
}

@Override protected int getDefaultPort() {
return 443;
public RequestSpecificRetryHandler getRequestSpecificRetryHandler(
RibbonRequest request, IClientConfig requestConfig) {

return new RequestSpecificRetryHandler(true, false) {
@Override
public boolean isRetriableException(Throwable e, boolean sameServer) {
return e instanceof RetryableException;
}

@Override
public boolean isCircuitTrippingException(Throwable e) {
return e instanceof IOException;
}
};
}

static class RibbonRequest extends ClientRequest implements Cloneable {
Expand Down Expand Up @@ -135,11 +134,14 @@ static class RibbonResponse implements IResponse {
Response toResponse() {
return response;
}
}

static int config(RibbonRequest request, CommonClientConfigKey key, int defaultValue) {
if (request.getOverrideConfig() != null && request.getOverrideConfig().containsProperty(key))
return Integer.valueOf(request.getOverrideConfig().getProperty(key).toString());
return defaultValue;
@Override
public void close() throws IOException {
if (response != null && response.body() != null) {
response.body().close();
}
}

}

}