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