NettyHttpLoadBalancerErrorHandler.java 3.5 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 20 21 22 23 24 25 26
package com.netflix.client.netty.http;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.List;


import com.google.common.collect.Lists;
27
import com.netflix.client.ClientException;
28
import com.netflix.client.DefaultLoadBalancerRetryHandler;
A
Allen Wang 已提交
29
import com.netflix.client.config.IClientConfig;
30
import com.netflix.client.http.UnexpectedHttpResponseException;
31

32
public class NettyHttpLoadBalancerErrorHandler extends DefaultLoadBalancerRetryHandler {
33 34

    @SuppressWarnings("unchecked")
A
Allen Wang 已提交
35
    private List<Class<? extends Throwable>> retriable = 
36
            Lists.<Class<? extends Throwable>>newArrayList(ConnectException.class, SocketTimeoutException.class, 
37 38
                    io.netty.handler.timeout.ReadTimeoutException.class, io.netty.channel.ConnectTimeoutException.class, 
                    io.reactivex.netty.client.PoolExhaustedException.class);
39 40
    
    @SuppressWarnings("unchecked")
A
Allen Wang 已提交
41
    private List<Class<? extends Throwable>> circuitRelated = 
42
            Lists.<Class<? extends Throwable>>newArrayList(SocketException.class, SocketTimeoutException.class, 
43 44
                    io.netty.handler.timeout.ReadTimeoutException.class, io.netty.channel.ConnectTimeoutException.class,
                    io.reactivex.netty.client.PoolExhaustedException.class);
45
    
A
Allen Wang 已提交
46 47
    public NettyHttpLoadBalancerErrorHandler() {
        super();
48 49
    }

A
Allen Wang 已提交
50 51 52 53 54 55 56 57
    public NettyHttpLoadBalancerErrorHandler(IClientConfig clientConfig) {
        super(clientConfig);
    }
    
    public NettyHttpLoadBalancerErrorHandler(int retrySameServer, int retryNextServer, boolean retryEnabled) {
        super(retrySameServer, retryNextServer, retryEnabled);
    }
    
58 59 60 61 62 63
    /**
     * @return true if the Throwable has one of the following exception type as a cause: 
     * {@link SocketException}, {@link SocketTimeoutException}
     */
    @Override
    public boolean isCircuitTrippingException(Throwable e) {
64
        if (e instanceof UnexpectedHttpResponseException) {
65
            return ((UnexpectedHttpResponseException) e).getStatusCode() == 503;
66 67
        } else if (e instanceof ClientException) {
            return ((ClientException) e).getErrorType() == ClientException.ErrorType.SERVER_THROTTLED;
68
        }
A
Allen Wang 已提交
69
        return super.isCircuitTrippingException(e);
70
    }
71 72 73 74 75 76 77 78 79 80 81
    
    @Override
    public boolean isRetriableException(Throwable e, boolean sameServer) {
        if (e instanceof ClientException) {
            ClientException ce = (ClientException) e;
            if (ce.getErrorType() == ClientException.ErrorType.SERVER_THROTTLED) {
                return !sameServer && retryEnabled;
            }
        }
        return super.isRetriableException(e, sameServer);
    }
82

A
Allen Wang 已提交
83 84 85 86 87 88 89 90 91
    @Override
    protected List<Class<? extends Throwable>> getRetriableExceptions() {
        return retriable;
    }

    @Override
    protected List<Class<? extends Throwable>> getCircuitRelatedExceptions() {
        return circuitRelated;
    }
92
}