forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLBClient.java
More file actions
144 lines (116 loc) · 4.38 KB
/
LBClient.java
File metadata and controls
144 lines (116 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feign.ribbon;
import com.netflix.client.AbstractLoadBalancerAwareClient;
import com.netflix.client.ClientException;
import com.netflix.client.ClientRequest;
import com.netflix.client.IResponse;
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;
import java.util.Collection;
import java.util.Map;
import feign.Client;
import feign.Request;
import feign.RequestTemplate;
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) {
this.delegate = delegate;
this.connectTimeout = Integer.valueOf(clientConfig.getProperty(ConnectTimeout).toString());
this.readTimeout = Integer.valueOf(clientConfig.getProperty(ReadTimeout).toString());
setLoadBalancer(lb);
initWithNiwsConfig(clientConfig);
}
@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);
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;
}
static class RibbonRequest extends ClientRequest implements Cloneable {
private final Request request;
RibbonRequest(Request request, URI uri) {
this.request = request;
setUri(uri);
}
Request toRequest() {
return new RequestTemplate()
.method(request.method())
.append(getUri().toASCIIString())
.headers(request.headers())
.body(request.body()).request();
}
public Object clone() {
return new RibbonRequest(request, getUri());
}
}
static class RibbonResponse implements IResponse {
private final URI uri;
private final Response response;
RibbonResponse(URI uri, Response response) {
this.uri = uri;
this.response = response;
}
@Override public Object getPayload() throws ClientException {
return response.body();
}
@Override public boolean hasPayload() {
return response.body() != null;
}
@Override public boolean isSuccess() {
return response.status() == 200;
}
@Override public URI getRequestedURI() {
return uri;
}
@Override public Map<String, Collection<String>> getHeaders() {
return response.headers();
}
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;
}
}