|
| 1 | +/* |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign.ribbon; |
| 17 | + |
| 18 | +import com.google.common.collect.ImmutableListMultimap; |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import com.google.common.collect.Iterables; |
| 21 | +import com.google.common.collect.LinkedListMultimap; |
| 22 | +import com.google.common.collect.ListMultimap; |
| 23 | +import com.netflix.client.AbstractLoadBalancerAwareClient; |
| 24 | +import com.netflix.client.ClientException; |
| 25 | +import com.netflix.client.ClientRequest; |
| 26 | +import com.netflix.client.IResponse; |
| 27 | +import com.netflix.client.config.CommonClientConfigKey; |
| 28 | +import com.netflix.client.config.IClientConfig; |
| 29 | +import com.netflix.loadbalancer.ILoadBalancer; |
| 30 | +import com.netflix.util.Pair; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.URI; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Set; |
| 38 | + |
| 39 | +import javax.ws.rs.core.MultivaluedMap; |
| 40 | + |
| 41 | +import feign.Client; |
| 42 | +import feign.Request; |
| 43 | +import feign.RequestTemplate; |
| 44 | +import feign.Response; |
| 45 | +import feign.RetryableException; |
| 46 | + |
| 47 | +import static com.netflix.client.config.CommonClientConfigKey.ConnectTimeout; |
| 48 | +import static com.netflix.client.config.CommonClientConfigKey.ReadTimeout; |
| 49 | + |
| 50 | +class LBClient extends AbstractLoadBalancerAwareClient<LBClient.RibbonRequest, LBClient.RibbonResponse> { |
| 51 | + |
| 52 | + private final Client delegate; |
| 53 | + private final int connectTimeout; |
| 54 | + private final int readTimeout; |
| 55 | + |
| 56 | + LBClient(Client delegate, ILoadBalancer lb, IClientConfig clientConfig) { |
| 57 | + this.delegate = delegate; |
| 58 | + this.connectTimeout = Integer.valueOf(clientConfig.getProperty(ConnectTimeout).toString()); |
| 59 | + this.readTimeout = Integer.valueOf(clientConfig.getProperty(ReadTimeout).toString()); |
| 60 | + setLoadBalancer(lb); |
| 61 | + initWithNiwsConfig(clientConfig); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public RibbonResponse execute(RibbonRequest request) throws IOException { |
| 66 | + int connectTimeout = config(request, ConnectTimeout, this.connectTimeout); |
| 67 | + int readTimeout = config(request, ReadTimeout, this.readTimeout); |
| 68 | + |
| 69 | + Request.Options options = new Request.Options(connectTimeout, readTimeout); |
| 70 | + Response response = delegate.execute(request.toRequest(), options); |
| 71 | + return new RibbonResponse(request.getUri(), response); |
| 72 | + } |
| 73 | + |
| 74 | + @Override protected boolean isCircuitBreakerException(Exception e) { |
| 75 | + return e instanceof IOException; |
| 76 | + } |
| 77 | + |
| 78 | + @Override protected boolean isRetriableException(Exception e) { |
| 79 | + return e instanceof RetryableException; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected Pair<String, Integer> deriveSchemeAndPortFromPartialUri(RibbonRequest task) { |
| 84 | + return new Pair<String, Integer>(URI.create(task.request.url()).getScheme(), task.getUri().getPort()); |
| 85 | + } |
| 86 | + |
| 87 | + @Override protected int getDefaultPort() { |
| 88 | + return 443; |
| 89 | + } |
| 90 | + |
| 91 | + static class RibbonRequest extends ClientRequest implements Cloneable { |
| 92 | + |
| 93 | + private final Request request; |
| 94 | + |
| 95 | + RibbonRequest(Request request, URI uri) { |
| 96 | + this.request = request; |
| 97 | + setUri(uri); |
| 98 | + } |
| 99 | + |
| 100 | + Request toRequest() { |
| 101 | + return new RequestTemplate() |
| 102 | + .method(request.method()) |
| 103 | + .append(getUri().toASCIIString()) |
| 104 | + .headers(request.headers()) |
| 105 | + .body(request.body().orNull()).request(); |
| 106 | + } |
| 107 | + |
| 108 | + public Object clone() { |
| 109 | + return new RibbonRequest(request, getUri()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + static class RibbonResponse implements IResponse { |
| 114 | + |
| 115 | + private final URI uri; |
| 116 | + private final Response response; |
| 117 | + |
| 118 | + RibbonResponse(URI uri, Response response) { |
| 119 | + this.uri = uri; |
| 120 | + this.response = response; |
| 121 | + } |
| 122 | + |
| 123 | + @Override public Object getPayload() throws ClientException { |
| 124 | + return response.body().orNull(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override public boolean hasPayload() { |
| 128 | + return response.body().isPresent(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override public boolean isSuccess() { |
| 132 | + return response.status() == 200; |
| 133 | + } |
| 134 | + |
| 135 | + @Override public URI getRequestedURI() { |
| 136 | + return uri; |
| 137 | + } |
| 138 | + |
| 139 | + @Override public Map<String, Collection<String>> getHeaders() { |
| 140 | + return response.headers().asMap(); |
| 141 | + } |
| 142 | + |
| 143 | + Response toResponse() { |
| 144 | + return response; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + static int config(RibbonRequest request, CommonClientConfigKey key, int defaultValue) { |
| 149 | + if (request.getOverrideConfig() != null && request.getOverrideConfig().containsProperty(key)) |
| 150 | + return Integer.valueOf(request.getOverrideConfig().getProperty(key).toString()); |
| 151 | + return defaultValue; |
| 152 | + } |
| 153 | +} |
0 commit comments