ConnectionPoolCleaner.java 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
*
* 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.
*
*/
18 19
package com.netflix.http4;

A
Allen Wang 已提交
20 21
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import java.util.concurrent.TimeUnit;

import org.apache.http.conn.ClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.config.DynamicIntProperty;
import com.netflix.config.DynamicPropertyFactory;

/**
 * Class that is responsible to cleanup connections based on a policy
 * For e.g. evict all connections from the pool that have been idle for more than x msecs
 * @author stonse
 *
 */
public class ConnectionPoolCleaner {   

    private static final Logger logger = LoggerFactory.getLogger(ConnectionPoolCleaner.class);
    
    String name = "default";    
    ClientConnectionManager connMgr;
A
Allen Wang 已提交
43
    ScheduledExecutorService scheduler;
44 45 46 47 48
    
    private DynamicIntProperty connIdleEvictTimeMilliSeconds 
        = DynamicPropertyFactory.getInstance().getIntProperty("default.nfhttpclient.connIdleEvictTimeMilliSeconds", 
                NFHttpClientConstants.DEFAULT_CONNECTIONIDLE_TIME_IN_MSECS);
    
A
Allen Wang 已提交
49
    volatile boolean enableConnectionPoolCleanerTask = false;
50 51
    long connectionCleanerTimerDelay = 10;
    long connectionCleanerRepeatInterval = NFHttpClientConstants.DEFAULT_CONNECTION_IDLE_TIMERTASK_REPEAT_IN_MSECS;
A
Allen Wang 已提交
52
    private volatile ScheduledFuture<?> scheduledFuture;
53
    
A
Allen Wang 已提交
54
    public ConnectionPoolCleaner(String name, ClientConnectionManager connMgr, ScheduledExecutorService scheduler){
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        this.name = name;
        this.connMgr = connMgr;        
    }
    
    public DynamicIntProperty getConnIdleEvictTimeMilliSeconds() {
        return connIdleEvictTimeMilliSeconds;
    }

    public void setConnIdleEvictTimeMilliSeconds(
            DynamicIntProperty connIdleEvictTimeMilliSeconds) {
        this.connIdleEvictTimeMilliSeconds = connIdleEvictTimeMilliSeconds;
    }

   
    public boolean isEnableConnectionPoolCleanerTask() {
        return enableConnectionPoolCleanerTask;
    }

    public void setEnableConnectionPoolCleanerTask(
            boolean enableConnectionPoolCleanerTask) {
        this.enableConnectionPoolCleanerTask = enableConnectionPoolCleanerTask;
    }

    public long getConnectionCleanerTimerDelay() {
        return connectionCleanerTimerDelay;
    }

    public void setConnectionCleanerTimerDelay(long connectionCleanerTimerDelay) {
        this.connectionCleanerTimerDelay = connectionCleanerTimerDelay;
    }

    public long getConnectionCleanerRepeatInterval() {
        return connectionCleanerRepeatInterval;
    }

    public void setConnectionCleanerRepeatInterval(
            long connectionCleanerRepeatInterval) {
        this.connectionCleanerRepeatInterval = connectionCleanerRepeatInterval;
    }

    public void initTask(){
A
Allen Wang 已提交
96 97 98 99 100 101
        if (enableConnectionPoolCleanerTask) {
            scheduledFuture = scheduler.scheduleWithFixedDelay(new Runnable() {
                public void run() {
                    try {
                        if (enableConnectionPoolCleanerTask) {
                            logger.debug("Connection pool clean up started for client {}", name);
102
                            cleanupConnections();
A
Allen Wang 已提交
103 104
                        } else if (scheduledFuture != null) {
                            scheduledFuture.cancel(true);
105
                        }
A
Allen Wang 已提交
106 107
                    } catch (Throwable e) {
                        logger.error("Exception in ConnectionPoolCleanerThread",e);
108 109
                    }
                }
A
Allen Wang 已提交
110 111
            }, connectionCleanerTimerDelay, connectionCleanerRepeatInterval, TimeUnit.MILLISECONDS);
            logger.info("Initializing ConnectionPoolCleaner for NFHttpClient:" + name);
112 113 114 115 116 117 118 119
        }
    }
    
    void cleanupConnections(){
        connMgr.closeExpiredConnections();
        connMgr.closeIdleConnections(connIdleEvictTimeMilliSeconds.get(), TimeUnit.MILLISECONDS);       
    }
    
A
Allen Wang 已提交
120 121 122 123 124 125 126
    public void shutdown() {
        enableConnectionPoolCleanerTask = false;
        if (scheduledFuture != null) {
            scheduledFuture.cancel(true);
        }
    }
    
127 128 129 130 131 132 133 134 135 136 137 138
    public String toString(){
        StringBuilder sb = new StringBuilder();
        
        sb.append("ConnectionPoolCleaner:" + name);
        sb.append(", connIdleEvictTimeMilliSeconds:" + connIdleEvictTimeMilliSeconds.get());
        sb.append(", connectionCleanerTimerDelay:" + connectionCleanerTimerDelay);
        sb.append(", connectionCleanerRepeatInterval:" + connectionCleanerRepeatInterval);
        
        return sb.toString();
    }
    
    
A
Allen Wang 已提交
139
    
140
}