| 
 | 1 | +package net.schmizz.keepalive;  | 
 | 2 | + | 
 | 3 | +import net.schmizz.sshj.Config;  | 
 | 4 | +import net.schmizz.sshj.connection.ConnectionException;  | 
 | 5 | +import net.schmizz.sshj.connection.ConnectionImpl;  | 
 | 6 | +import net.schmizz.sshj.transport.TransportException;  | 
 | 7 | +import org.slf4j.Logger;  | 
 | 8 | + | 
 | 9 | +import java.util.ArrayList;  | 
 | 10 | +import java.util.Comparator;  | 
 | 11 | +import java.util.List;  | 
 | 12 | +import java.util.concurrent.PriorityBlockingQueue;  | 
 | 13 | +import java.util.concurrent.atomic.AtomicInteger;  | 
 | 14 | +import java.util.concurrent.locks.Condition;  | 
 | 15 | +import java.util.concurrent.locks.ReentrantLock;  | 
 | 16 | + | 
 | 17 | +/**  | 
 | 18 | + * This implementation manages all {@link KeepAlive}s using configured number of threads. It works like a  | 
 | 19 | + * thread pool, thus {@link BoundedKeepAliveProvider#shutdown()} must be called to clean up resources.  | 
 | 20 | + * <br>  | 
 | 21 | + * This provider uses {@link KeepAliveRunner#doKeepAlive()} as delegate, so it supports maxKeepAliveCount  | 
 | 22 | + * parameter. All instances provided by this provider have identical configuration.  | 
 | 23 | + */  | 
 | 24 | +public class BoundedKeepAliveProvider extends KeepAliveProvider {  | 
 | 25 | + | 
 | 26 | +    public int maxKeepAliveCount = 3;  | 
 | 27 | +    public int keepAliveInterval = 5;  | 
 | 28 | + | 
 | 29 | +    protected final KeepAliveMonitor monitor;  | 
 | 30 | + | 
 | 31 | + | 
 | 32 | +    public BoundedKeepAliveProvider(Config config, int numberOfThreads) {  | 
 | 33 | +        this.monitor = new KeepAliveMonitor(config, numberOfThreads);  | 
 | 34 | +    }  | 
 | 35 | + | 
 | 36 | +    public void setKeepAliveInterval(int interval) {  | 
 | 37 | +        keepAliveInterval = interval;  | 
 | 38 | +    }  | 
 | 39 | + | 
 | 40 | +    public void setMaxKeepAliveCount(int count) {  | 
 | 41 | +        maxKeepAliveCount = count;  | 
 | 42 | +    }  | 
 | 43 | + | 
 | 44 | +    @Override  | 
 | 45 | +    public KeepAlive provide(ConnectionImpl connection) {  | 
 | 46 | +        return new Impl(connection, "bounded-keepalive-impl");  | 
 | 47 | +    }  | 
 | 48 | + | 
 | 49 | +    public void shutdown() throws InterruptedException {  | 
 | 50 | +        monitor.shutdown();  | 
 | 51 | +    }  | 
 | 52 | + | 
 | 53 | +    class Impl extends KeepAlive {  | 
 | 54 | + | 
 | 55 | +        private final KeepAliveRunner delegate;  | 
 | 56 | + | 
 | 57 | +        protected Impl(ConnectionImpl conn, String name) {  | 
 | 58 | +            super(conn, name);  | 
 | 59 | +            this.delegate = new KeepAliveRunner(conn);  | 
 | 60 | + | 
 | 61 | +            // take care here, some parameters are set to both delegate and this  | 
 | 62 | +            this.delegate.setMaxAliveCount(BoundedKeepAliveProvider.this.maxKeepAliveCount);  | 
 | 63 | +            super.keepAliveInterval = BoundedKeepAliveProvider.this.keepAliveInterval;  | 
 | 64 | +        }  | 
 | 65 | + | 
 | 66 | +        @Override  | 
 | 67 | +        protected void doKeepAlive() throws TransportException, ConnectionException {  | 
 | 68 | +            delegate.doKeepAlive();  | 
 | 69 | +        }  | 
 | 70 | + | 
 | 71 | +        @Override  | 
 | 72 | +        public void startKeepAlive() {  | 
 | 73 | +            monitor.register(this);  | 
 | 74 | +        }  | 
 | 75 | + | 
 | 76 | +    }  | 
 | 77 | + | 
 | 78 | +    protected static class KeepAliveMonitor {  | 
 | 79 | + | 
 | 80 | +        private final int numberOfThreads;  | 
 | 81 | +        private final PriorityBlockingQueue<Wrapper> Q =  | 
 | 82 | +                new PriorityBlockingQueue<>(32, Comparator.comparingLong(w -> w.nextTimeMillis));  | 
 | 83 | +        private long idleSleepMillis = 100;  | 
 | 84 | +        private static final List<Thread> workerThreads = new ArrayList<>();  | 
 | 85 | +        volatile boolean started = false;  | 
 | 86 | +        private final Logger logger;  | 
 | 87 | + | 
 | 88 | +        private final ReentrantLock lock = new ReentrantLock();  | 
 | 89 | +        private final Condition shutDown = lock.newCondition();  | 
 | 90 | +        private final AtomicInteger shutDownCnt = new AtomicInteger(0);  | 
 | 91 | + | 
 | 92 | +        public KeepAliveMonitor(Config config, int numberOfThreads) {  | 
 | 93 | +            this.numberOfThreads = numberOfThreads;  | 
 | 94 | +            logger = config.getLoggerFactory().getLogger(KeepAliveMonitor.class);  | 
 | 95 | +        }  | 
 | 96 | + | 
 | 97 | +        // made public for test  | 
 | 98 | +        public void register(KeepAlive keepAlive) {  | 
 | 99 | +            if (!started) {  | 
 | 100 | +                start();  | 
 | 101 | +            }  | 
 | 102 | +            Q.add(new Wrapper(keepAlive));  | 
 | 103 | +        }  | 
 | 104 | + | 
 | 105 | +        public void setIdleSleepMillis(long idleSleepMillis) {  | 
 | 106 | +            this.idleSleepMillis = idleSleepMillis;  | 
 | 107 | +        }  | 
 | 108 | + | 
 | 109 | +        void unregister(KeepAlive keepAlive) {  | 
 | 110 | +            Q.removeIf(w -> keepAlive == w.keepAlive);  | 
 | 111 | +        }  | 
 | 112 | + | 
 | 113 | +        private void sleep() {  | 
 | 114 | +            sleep(idleSleepMillis);  | 
 | 115 | +        }  | 
 | 116 | + | 
 | 117 | +        private void sleep(long millis) {  | 
 | 118 | +            try {  | 
 | 119 | +                Thread.sleep(millis);  | 
 | 120 | +            } catch (InterruptedException e) {  | 
 | 121 | +                Thread.currentThread().interrupt();  | 
 | 122 | +            }  | 
 | 123 | +        }  | 
 | 124 | + | 
 | 125 | +        private synchronized void start() {  | 
 | 126 | +            if (started) {  | 
 | 127 | +                return;  | 
 | 128 | +            }  | 
 | 129 | + | 
 | 130 | +            for (int i = 0; i < numberOfThreads; i++) {  | 
 | 131 | +                Thread t = new Thread(this::doStart);  | 
 | 132 | +                workerThreads.add(t);  | 
 | 133 | +            }  | 
 | 134 | +            workerThreads.forEach(Thread::start);  | 
 | 135 | +            started = true;  | 
 | 136 | +        }  | 
 | 137 | + | 
 | 138 | + | 
 | 139 | +        private void doStart() {  | 
 | 140 | +            while (!Thread.currentThread().isInterrupted()) {  | 
 | 141 | +                Wrapper wrapper;  | 
 | 142 | + | 
 | 143 | +                if (Q.isEmpty() || (wrapper = Q.poll()) == null) {  | 
 | 144 | +                    sleep();  | 
 | 145 | +                    continue;  | 
 | 146 | +                }  | 
 | 147 | + | 
 | 148 | +                long currentTimeMillis = System.currentTimeMillis();  | 
 | 149 | +                if (wrapper.nextTimeMillis > currentTimeMillis) {  | 
 | 150 | +                    long sleepMillis = wrapper.nextTimeMillis - currentTimeMillis;  | 
 | 151 | +                    logger.debug("{} millis until next check, sleep", sleepMillis);  | 
 | 152 | +                    sleep(sleepMillis);  | 
 | 153 | +                }  | 
 | 154 | + | 
 | 155 | +                try {  | 
 | 156 | +                    wrapper.keepAlive.doKeepAlive();  | 
 | 157 | +                    Q.add(wrapper.reschedule());  | 
 | 158 | +                } catch (Exception e) {  | 
 | 159 | +                    // If we weren't interrupted, kill the transport, then this exception was unexpected.  | 
 | 160 | +                    // Else we're in shutdown-mode already, so don't forcibly kill the transport.  | 
 | 161 | +                    if (!Thread.currentThread().isInterrupted()) {  | 
 | 162 | +                        wrapper.keepAlive.conn.getTransport().die(e);  | 
 | 163 | +                    }  | 
 | 164 | +                }  | 
 | 165 | +            }  | 
 | 166 | +            lock.lock();  | 
 | 167 | +            try {  | 
 | 168 | +                if (shutDownCnt.incrementAndGet() == numberOfThreads) {  | 
 | 169 | +                    shutDown.signal();  | 
 | 170 | +                }  | 
 | 171 | +            } finally {  | 
 | 172 | +                lock.unlock();  | 
 | 173 | +            }  | 
 | 174 | +        }  | 
 | 175 | + | 
 | 176 | +        private synchronized void shutdown() throws InterruptedException {  | 
 | 177 | +            if (workerThreads.isEmpty()) {  | 
 | 178 | +                return;  | 
 | 179 | +            }  | 
 | 180 | +            for (Thread t : workerThreads) {  | 
 | 181 | +                t.interrupt();  | 
 | 182 | +            }  | 
 | 183 | +            lock.lock();  | 
 | 184 | +            logger.info("waiting for all {} threads to finish", numberOfThreads);  | 
 | 185 | +            shutDown.await();  | 
 | 186 | +        }  | 
 | 187 | + | 
 | 188 | +        private static class Wrapper {  | 
 | 189 | +            private final KeepAlive keepAlive;  | 
 | 190 | +            private final long nextTimeMillis;  | 
 | 191 | + | 
 | 192 | +            private Wrapper(KeepAlive keepAlive) {  | 
 | 193 | +                this.keepAlive = keepAlive;  | 
 | 194 | +                this.nextTimeMillis = System.currentTimeMillis() + keepAlive.keepAliveInterval * 1000L;  | 
 | 195 | +            }  | 
 | 196 | + | 
 | 197 | +            private Wrapper reschedule() {  | 
 | 198 | +                return new Wrapper(keepAlive);  | 
 | 199 | +            }  | 
 | 200 | +        }  | 
 | 201 | +    }  | 
 | 202 | +}  | 
0 commit comments