RequestSpecificRetryHandler.java 3.0 KB
Newer Older
A
Allen Wang 已提交
1 2 3
package com.netflix.client;

import com.google.common.base.Preconditions;
4
import com.google.common.collect.Lists;
A
Allen Wang 已提交
5 6 7
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;

E
elandau 已提交
8 9 10 11 12
import javax.annotation.Nullable;
import java.net.SocketException;
import java.util.List;
import java.util.Optional;

13 14 15 16
/**
 * Implementation of RetryHandler created for each request which allows for request
 * specific override
 */
17
public class RequestSpecificRetryHandler implements RetryHandler {
A
Allen Wang 已提交
18 19 20 21

    private final RetryHandler fallback;
    private int retrySameServer = -1;
    private int retryNextServer = -1;
22 23
    private final boolean okToRetryOnConnectErrors;
    private final boolean okToRetryOnAllErrors;
A
Allen Wang 已提交
24
    
25 26 27
    protected List<Class<? extends Throwable>> connectionRelated = 
            Lists.<Class<? extends Throwable>>newArrayList(SocketException.class);

28 29 30 31 32 33
    public RequestSpecificRetryHandler(boolean okToRetryOnConnectErrors, boolean okToRetryOnAllErrors) {
        this(okToRetryOnConnectErrors, okToRetryOnAllErrors, RetryHandler.DEFAULT, null);    
    }
    
    public RequestSpecificRetryHandler(boolean okToRetryOnConnectErrors, boolean okToRetryOnAllErrors, RetryHandler baseRetryHandler, @Nullable IClientConfig requestConfig) {
        Preconditions.checkNotNull(baseRetryHandler);
34
        this.okToRetryOnConnectErrors = okToRetryOnConnectErrors;
E
elandau 已提交
35
        this.okToRetryOnAllErrors = okToRetryOnAllErrors;
36
        this.fallback = baseRetryHandler;
A
Allen Wang 已提交
37
        if (requestConfig != null) {
E
elandau 已提交
38
            requestConfig.getIfSet(CommonClientConfigKey.MaxAutoRetries).ifPresent(
E
elandau 已提交
39 40
                    value -> retrySameServer = value
            );
E
elandau 已提交
41
            requestConfig.getIfSet(CommonClientConfigKey.MaxAutoRetriesNextServer).ifPresent(
E
elandau 已提交
42 43
                    value -> retryNextServer = value
            );
A
Allen Wang 已提交
44 45 46
        }
    }
    
47 48 49 50
    public boolean isConnectionException(Throwable e) {
        return Utils.isPresentAsCause(e, connectionRelated);
    }

A
Allen Wang 已提交
51 52
    @Override
    public boolean isRetriableException(Throwable e, boolean sameServer) {
53 54
        if (okToRetryOnAllErrors) {
            return true;
55 56
        } 
        else if (e instanceof ClientException) {
57 58 59 60 61 62
            ClientException ce = (ClientException) e;
            if (ce.getErrorType() == ClientException.ErrorType.SERVER_THROTTLED) {
                return !sameServer;
            } else {
                return false;
            }
63 64
        } 
        else  {
65 66
            return okToRetryOnConnectErrors && isConnectionException(e);
        }
A
Allen Wang 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }

    @Override
    public boolean isCircuitTrippingException(Throwable e) {
        return fallback.isCircuitTrippingException(e);
    }

    @Override
    public int getMaxRetriesOnSameServer() {
        if (retrySameServer >= 0) {
            return retrySameServer;
        }
        return fallback.getMaxRetriesOnSameServer();
    }

    @Override
    public int getMaxRetriesOnNextServer() {
        if (retryNextServer >= 0) {
            return retryNextServer;
        }
        return fallback.getMaxRetriesOnNextServer();
    }    
}