com.netflix.client.DefaultLoadBalancerRetryHandler Maven / Gradle / Ivy
/*
*
* 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 com.netflix.client;
import com.google.common.collect.Lists;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.List;
/**
* A default {@link RetryHandler}. The implementation is limited to
* known exceptions in java.net. Specific client implementation should provide its own
* {@link RetryHandler}
*
* @author awang
*/
public class DefaultLoadBalancerRetryHandler implements RetryHandler {
@SuppressWarnings("unchecked")
private List> retriable =
Lists.>newArrayList(ConnectException.class, SocketTimeoutException.class);
@SuppressWarnings("unchecked")
private List> circuitRelated =
Lists.>newArrayList(SocketException.class, SocketTimeoutException.class);
protected final int retrySameServer;
protected final int retryNextServer;
protected final boolean retryEnabled;
public DefaultLoadBalancerRetryHandler() {
this.retrySameServer = 0;
this.retryNextServer = 0;
this.retryEnabled = false;
}
public DefaultLoadBalancerRetryHandler(int retrySameServer, int retryNextServer, boolean retryEnabled) {
this.retrySameServer = retrySameServer;
this.retryNextServer = retryNextServer;
this.retryEnabled = retryEnabled;
}
public DefaultLoadBalancerRetryHandler(IClientConfig clientConfig) {
this.retrySameServer = clientConfig.get(CommonClientConfigKey.MaxAutoRetries, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES);
this.retryNextServer = clientConfig.get(CommonClientConfigKey.MaxAutoRetriesNextServer, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER);
this.retryEnabled = clientConfig.get(CommonClientConfigKey.OkToRetryOnAllOperations, false);
}
@Override
public boolean isRetriableException(Throwable e, boolean sameServer) {
if (retryEnabled) {
if (sameServer) {
return Utils.isPresentAsCause(e, getRetriableExceptions());
} else {
return true;
}
}
return false;
}
/**
* @return true if {@link SocketException} or {@link SocketTimeoutException} is a cause in the Throwable.
*/
@Override
public boolean isCircuitTrippingException(Throwable e) {
return Utils.isPresentAsCause(e, getCircuitRelatedExceptions());
}
@Override
public int getMaxRetriesOnSameServer() {
return retrySameServer;
}
@Override
public int getMaxRetriesOnNextServer() {
return retryNextServer;
}
protected List> getRetriableExceptions() {
return retriable;
}
protected List> getCircuitRelatedExceptions() {
return circuitRelated;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy