NettyHttpLoadBalancingClient.java 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *
 * Copyright 2014 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.
 *
 */
18 19
package com.netflix.client.netty.http;

20
import io.netty.buffer.ByteBuf;
21 22 23
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;
import io.reactivex.netty.protocol.http.client.RepeatableContentHttpRequest;
24 25
import rx.Observable;

26
import com.netflix.client.RetryHandler;
27
import com.netflix.client.config.IClientConfig;
28
import com.netflix.loadbalancer.ClientObservableProvider;
29
import com.netflix.loadbalancer.ILoadBalancer;
30
import com.netflix.loadbalancer.LoadBalancerExecutor;
31 32
import com.netflix.loadbalancer.Server;

33
public class NettyHttpLoadBalancingClient extends AbstractLoadBalancingClient {
34 35

    private final NettyHttpClient delegate;
A
Allen Wang 已提交
36 37

    public NettyHttpLoadBalancingClient(ILoadBalancer lb, IClientConfig config) {
38
        delegate = new NettyHttpClient(config);
39 40
        lbExecutor = new LoadBalancerExecutor(lb, config);
        lbExecutor.setErrorHandler(new NettyHttpLoadBalancerErrorHandler(config));
41
    }
42
        
A
Allen Wang 已提交
43 44
    public NettyHttpLoadBalancingClient(ILoadBalancer lb, IClientConfig config, RetryHandler errorHandler) {
        delegate = new NettyHttpClient(config);
45 46
        lbExecutor = new LoadBalancerExecutor(lb, config);
        lbExecutor.setErrorHandler(errorHandler);
47 48
    }
        
49 50
    public <I> Observable<HttpClientResponse<ByteBuf>> submit(final HttpClientRequest<I> request) {
        return submit(request, null, null);
51 52
    }
    
53 54 55 56
    public <I> Observable<HttpClientResponse<ByteBuf>> submit(final HttpClientRequest<I> request, final RetryHandler errorHandler, final IClientConfig requestConfig) {
        final RepeatableContentHttpRequest<I> repeatableRequest = getRepeatableRequest(request);
        final RetryHandler retryHandler = (errorHandler == null) ? getRequestRetryHandler(request, requestConfig) : errorHandler;
        return lbExecutor.executeWithLoadBalancer(new ClientObservableProvider<HttpClientResponse<ByteBuf>>() {
57
            @Override
58
            public Observable<HttpClientResponse<ByteBuf>> getObservableForEndpoint(
A
Allen Wang 已提交
59
                    Server server) {
60
                return delegate.submit(server.getHost(), server.getPort(), repeatableRequest, requestConfig);
61
            }
62
        }, retryHandler);
63 64
    }
}