|
| 1 | +package com.qcloud.cos.http; |
| 2 | + |
| 3 | +import org.apache.http.conn.HttpClientConnectionManager; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +public class IdleConnectionMonitor extends Thread { |
| 14 | + private static final Logger log = LoggerFactory.getLogger(IdleConnectionMonitor.class); |
| 15 | + private static final Map<HttpClientConnectionManager, Long> connectionManagers = new ConcurrentHashMap<HttpClientConnectionManager, Long>(); |
| 16 | + private static final int PERIOD_MILLISECONDS = 1000 * 60; |
| 17 | + private static volatile IdleConnectionMonitor instance; |
| 18 | + |
| 19 | + private volatile boolean shuttingDown; |
| 20 | + |
| 21 | + private IdleConnectionMonitor() { |
| 22 | + super("cos-java-sdk-http-connection-monitor"); |
| 23 | + setDaemon(true); |
| 24 | + } |
| 25 | + |
| 26 | + private void markShuttingDown() { |
| 27 | + shuttingDown = true; |
| 28 | + } |
| 29 | + |
| 30 | + public static boolean registerConnectionManager(HttpClientConnectionManager connectionManager, long maxIdleInMs) { |
| 31 | + if (instance == null) { |
| 32 | + synchronized (IdleConnectionMonitor.class) { |
| 33 | + if (instance == null) { |
| 34 | + instance = new IdleConnectionMonitor(); |
| 35 | + instance.start(); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return connectionManagers.put(connectionManager, maxIdleInMs) == null; |
| 40 | + } |
| 41 | + |
| 42 | + public static boolean removeConnectionManager(HttpClientConnectionManager connectionManager) { |
| 43 | + boolean wasRemoved = connectionManagers.remove(connectionManager) != null; |
| 44 | + if (connectionManagers.isEmpty()) { |
| 45 | + shutdown(); |
| 46 | + } |
| 47 | + return wasRemoved; |
| 48 | + } |
| 49 | + |
| 50 | + public static List<HttpClientConnectionManager> getRegisteredConnectionManagers() { |
| 51 | + return new ArrayList<HttpClientConnectionManager>(connectionManagers.keySet()); |
| 52 | + } |
| 53 | + |
| 54 | + public static synchronized boolean shutdown() { |
| 55 | + if (instance != null) { |
| 56 | + instance.markShuttingDown(); |
| 57 | + instance.interrupt(); |
| 58 | + connectionManagers.clear(); |
| 59 | + instance = null; |
| 60 | + return true; |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void run() { |
| 67 | + while (!shuttingDown) { |
| 68 | + try { |
| 69 | + for (Map.Entry<HttpClientConnectionManager, Long> entry : connectionManagers.entrySet()) { |
| 70 | + // When we release connections, the connection manager leaves them |
| 71 | + // open so they can be reused. We want to close out any idle |
| 72 | + // connections so that they don't sit around in CLOSE_WAIT. |
| 73 | + try { |
| 74 | + entry.getKey().closeExpiredConnections(); |
| 75 | + entry.getKey().closeIdleConnections(entry.getValue(), TimeUnit.MILLISECONDS); |
| 76 | + } catch (Exception e) { |
| 77 | + log.warn("Unable to closeExpiredConnections and closeIdleConnections, ", e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Thread.sleep(PERIOD_MILLISECONDS); |
| 82 | + } catch (Throwable t) { |
| 83 | + log.error("error occurred when closeExpiredConnections and closeIdleConnections, err:", t); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + log.debug("Shutting down reaper thread."); |
| 88 | + } |
| 89 | +} |
0 commit comments