|
19 | 19 | import com.netflix.client.ClientException; |
20 | 20 | import com.netflix.client.ClientRequest; |
21 | 21 | import com.netflix.client.IResponse; |
| 22 | +import com.netflix.client.RequestSpecificRetryHandler; |
22 | 23 | import com.netflix.client.config.CommonClientConfigKey; |
23 | 24 | import com.netflix.client.config.IClientConfig; |
24 | 25 | import com.netflix.loadbalancer.ILoadBalancer; |
25 | | -import com.netflix.util.Pair; |
26 | 26 |
|
27 | 27 | import java.io.IOException; |
28 | 28 | import java.net.URI; |
|
35 | 35 | import feign.Response; |
36 | 36 | import feign.RetryableException; |
37 | 37 |
|
38 | | -import static com.netflix.client.config.CommonClientConfigKey.ConnectTimeout; |
39 | | -import static com.netflix.client.config.CommonClientConfigKey.ReadTimeout; |
40 | | - |
41 | 38 | class LBClient extends AbstractLoadBalancerAwareClient<LBClient.RibbonRequest, LBClient.RibbonResponse> { |
42 | 39 |
|
43 | 40 | private final Client delegate; |
44 | 41 | private final int connectTimeout; |
45 | 42 | private final int readTimeout; |
46 | 43 |
|
47 | 44 | LBClient(Client delegate, ILoadBalancer lb, IClientConfig clientConfig) { |
| 45 | + super(lb, clientConfig); |
48 | 46 | this.delegate = delegate; |
49 | | - this.connectTimeout = Integer.valueOf(clientConfig.getProperty(ConnectTimeout).toString()); |
50 | | - this.readTimeout = Integer.valueOf(clientConfig.getProperty(ReadTimeout).toString()); |
51 | | - setLoadBalancer(lb); |
52 | | - initWithNiwsConfig(clientConfig); |
| 47 | + connectTimeout = clientConfig.get(CommonClientConfigKey.ConnectTimeout); |
| 48 | + readTimeout = clientConfig.get(CommonClientConfigKey.ReadTimeout); |
53 | 49 | } |
54 | 50 |
|
55 | 51 | @Override |
56 | | - public RibbonResponse execute(RibbonRequest request) throws IOException { |
57 | | - int connectTimeout = config(request, ConnectTimeout, this.connectTimeout); |
58 | | - int readTimeout = config(request, ReadTimeout, this.readTimeout); |
59 | | - |
60 | | - Request.Options options = new Request.Options(connectTimeout, readTimeout); |
| 52 | + public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride) throws IOException { |
| 53 | + Request.Options options; |
| 54 | + if (configOverride != null) { |
| 55 | + options = new Request.Options(configOverride.get(CommonClientConfigKey.ConnectTimeout, connectTimeout), |
| 56 | + (configOverride.get(CommonClientConfigKey.ReadTimeout, readTimeout))); |
| 57 | + } else { |
| 58 | + options = new Request.Options(connectTimeout, readTimeout); |
| 59 | + } |
61 | 60 | Response response = delegate.execute(request.toRequest(), options); |
62 | 61 | return new RibbonResponse(request.getUri(), response); |
63 | 62 | } |
64 | 63 |
|
65 | | - @Override protected boolean isCircuitBreakerException(Exception e) { |
66 | | - return e instanceof IOException; |
67 | | - } |
68 | | - |
69 | | - @Override protected boolean isRetriableException(Exception e) { |
70 | | - return e instanceof RetryableException; |
71 | | - } |
72 | | - |
73 | 64 | @Override |
74 | | - protected Pair<String, Integer> deriveSchemeAndPortFromPartialUri(RibbonRequest task) { |
75 | | - return new Pair<String, Integer>(URI.create(task.request.url()).getScheme(), task.getUri().getPort()); |
76 | | - } |
77 | | - |
78 | | - @Override protected int getDefaultPort() { |
79 | | - return 443; |
| 65 | + public RequestSpecificRetryHandler getRequestSpecificRetryHandler( |
| 66 | + RibbonRequest request, IClientConfig requestConfig) { |
| 67 | + |
| 68 | + return new RequestSpecificRetryHandler(true, false) { |
| 69 | + @Override |
| 70 | + public boolean isRetriableException(Throwable e, boolean sameServer) { |
| 71 | + return e instanceof RetryableException; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean isCircuitTrippingException(Throwable e) { |
| 76 | + return e instanceof IOException; |
| 77 | + } |
| 78 | + }; |
80 | 79 | } |
81 | 80 |
|
82 | 81 | static class RibbonRequest extends ClientRequest implements Cloneable { |
@@ -135,11 +134,14 @@ static class RibbonResponse implements IResponse { |
135 | 134 | Response toResponse() { |
136 | 135 | return response; |
137 | 136 | } |
138 | | - } |
139 | 137 |
|
140 | | - static int config(RibbonRequest request, CommonClientConfigKey key, int defaultValue) { |
141 | | - if (request.getOverrideConfig() != null && request.getOverrideConfig().containsProperty(key)) |
142 | | - return Integer.valueOf(request.getOverrideConfig().getProperty(key).toString()); |
143 | | - return defaultValue; |
| 138 | + @Override |
| 139 | + public void close() throws IOException { |
| 140 | + if (response != null && response.body() != null) { |
| 141 | + response.body().close(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
144 | 145 | } |
| 146 | + |
145 | 147 | } |
0 commit comments