// Path: src/main/java/com/github/phantomthief/failover/util/SharedCheckExecutorHolder.java
// public class SharedCheckExecutorHolder {
// 
//     private static final int THREAD_COUNT = 10;
// 
//     public static ScheduledExecutorService getInstance() {
//         return LazyHolder.INSTANCE;
//     }
// 
//     private static class LazyHolder {
// 
//         private static final ScheduledExecutorService INSTANCE = new ScheduledThreadPoolExecutor(
//                 THREAD_COUNT,
//                 new ThreadFactory() {
//                     private AtomicLong count = new AtomicLong();
//                     private static final String NAME_PATTERN = "scheduled-failover-recovery-check-%d";
//                     @Override
//                     public Thread newThread(Runnable r) {
//                         String name = format(NAME_PATTERN, count.getAndIncrement());
//                         Thread thread = new Thread(r, name);
//                         thread.setDaemon(true);
//                         thread.setPriority(MIN_PRIORITY);
//                         if (Thread.getDefaultUncaughtExceptionHandler() == null) {
//                             thread.setUncaughtExceptionHandler((t, e) -> {
//                                 e.printStackTrace();
//                             });
//                         }
//                         return thread;
//                     }
//                 }) {
// 
//             public void shutdown() {
//                 throw new UnsupportedOperationException();
//             }
// 
//             public List<Runnable> shutdownNow() {
//                 throw new UnsupportedOperationException();
//             }
//         };
//     }
// }


import static com.github.phantomthief.util.MoreSuppliers.lazy;
import static com.google.common.primitives.Ints.constrainToRange;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.phantomthief.failover.util.SharedCheckExecutorHolder;
import com.github.phantomthief.util.MoreSuppliers.CloseableSupplier;
package com.github.phantomthief.failover.impl;





/**
 *
 * @author w.vela
 * @author huangli
 */

class WeightFailoverCheckTask<T> {

    private static final Logger logger = LoggerFactory.getLogger(WeightFailoverCheckTask.class);

    static final int CLEAN_INIT_DELAY_SECONDS = 5;
    static final int CLEAN_DELAY_SECONDS = 10;

    static {
// Path: src/main/java/com/github/phantomthief/failover/util/SharedCheckExecutorHolder.java
// public class SharedCheckExecutorHolder {
// 
//     private static final int THREAD_COUNT = 10;
// 
//     public static ScheduledExecutorService getInstance() {
//         return LazyHolder.INSTANCE;
//     }
// 
//     private static class LazyHolder {
// 
//         private static final ScheduledExecutorService INSTANCE = new ScheduledThreadPoolExecutor(
//                 THREAD_COUNT,
//                 new ThreadFactory() {
//                     private AtomicLong count = new AtomicLong();
//                     private static final String NAME_PATTERN = "scheduled-failover-recovery-check-%d";
//                     @Override
//                     public Thread newThread(Runnable r) {
//                         String name = format(NAME_PATTERN, count.getAndIncrement());
//                         Thread thread = new Thread(r, name);
//                         thread.setDaemon(true);
//                         thread.setPriority(MIN_PRIORITY);
//                         if (Thread.getDefaultUncaughtExceptionHandler() == null) {
//                             thread.setUncaughtExceptionHandler((t, e) -> {
//                                 e.printStackTrace();
//                             });
//                         }
//                         return thread;
//                     }
//                 }) {
// 
//             public void shutdown() {
//                 throw new UnsupportedOperationException();
//             }
// 
//             public List<Runnable> shutdownNow() {
//                 throw new UnsupportedOperationException();
//             }
//         };
//     }
// }


// Path: src/main/java/com/github/phantomthief/failover/impl/WeightFailoverCheckTask.java
import static com.github.phantomthief.util.MoreSuppliers.lazy;
import static com.google.common.primitives.Ints.constrainToRange;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.phantomthief.failover.util.SharedCheckExecutorHolder;
import com.github.phantomthief.util.MoreSuppliers.CloseableSupplier;

package com.github.phantomthief.failover.impl;





/**
 *
 * @author w.vela
 * @author huangli
 */

class WeightFailoverCheckTask<T> {

    private static final Logger logger = LoggerFactory.getLogger(WeightFailoverCheckTask.class);

    static final int CLEAN_INIT_DELAY_SECONDS = 5;
    static final int CLEAN_DELAY_SECONDS = 10;

    static {
        SharedCheckExecutorHolder.getInstance().scheduleWithFixedDelay(WeightFailoverCheckTask::doClean,