All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.phantomthief.thrift.client.utils.FailoverCheckingStrategy Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package com.github.phantomthief.thrift.client.utils;

import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.EvictingQueue;

/**
 * 

* FailoverCheckingStrategy class. *

* * @author w.vela * @version $Id: $Id */ public class FailoverCheckingStrategy { private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(getClass()); private static final int DEFAULT_FAIL_COUNT = 10; private static final long DEFAULT_FAIL_DURATION = TimeUnit.MINUTES.toMillis(1); private static final long DEFAULT_RECOVERY_DURATION = TimeUnit.MINUTES.toMillis(3); private final long failDuration; private final Cache failedList; private final LoadingCache> failCountMap; /** *

* Constructor for FailoverCheckingStrategy. *

*/ public FailoverCheckingStrategy() { this(DEFAULT_FAIL_COUNT, DEFAULT_FAIL_DURATION, DEFAULT_RECOVERY_DURATION); } /** *

* Constructor for FailoverCheckingStrategy. *

* * @param failDuration a long. * @param recoveryDuration a long. */ public FailoverCheckingStrategy(int failCount, long failDuration, long recoveryDuration) { this.failDuration = failDuration; this.failedList = CacheBuilder.newBuilder().weakKeys() .expireAfterWrite(recoveryDuration, TimeUnit.MILLISECONDS).build(); this.failCountMap = CacheBuilder.newBuilder().weakKeys() .build(new CacheLoader>() { @Override public EvictingQueue load(T key) throws Exception { return EvictingQueue.create(failCount); } }); } /** *

* getFailed. *

* * @return a {@link java.util.Set} object. */ public Set getFailed() { return failedList.asMap().keySet(); } /** *

* fail. *

* * @param object a T object. */ public void fail(T object) { logger.trace("server {} failed.", object); boolean addToFail = false; try { EvictingQueue evictingQueue = failCountMap.get(object); synchronized (evictingQueue) { evictingQueue.add(System.currentTimeMillis()); if (evictingQueue.remainingCapacity() == 0 && evictingQueue.element() >= System.currentTimeMillis() - failDuration) { addToFail = true; } } } catch (ExecutionException e) { logger.error("Ops.", e); } if (addToFail) { failedList.put(object, Boolean.TRUE); logger.trace("server {} failed. add to fail list.", object); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy