提交 426b6089 编写于 作者: A allenxwang

Merge pull request #72 from allenxwang/cp

Make connection pool configuration dynamic
......@@ -84,6 +84,9 @@ public class NFHttpClient extends DefaultHttpClient {
private Timer tracer;
private DynamicIntProperty maxTotalConnectionProperty;
private DynamicIntProperty maxConnectionPerHostProperty;
protected NFHttpClient(String host, int port){
super(new ThreadSafeClientConnManager());
this.name = "UNNAMED_" + numNonNamedHttpClients.incrementAndGet();
......@@ -136,6 +139,22 @@ public class NFHttpClient extends DefaultHttpClient {
if (registerMonitor) {
Monitors.registerObject(name, this);
}
maxTotalConnectionProperty = new DynamicIntProperty(this.name + "." + config.getNameSpace() + "." + CommonClientConfigKey.MaxTotalHttpConnections.key(),
DefaultClientConfigImpl.DEFAULT_MAX_TOTAL_HTTP_CONNECTIONS);
maxTotalConnectionProperty.addCallback(new Runnable() {
@Override
public void run() {
((ThreadSafeClientConnManager) getConnectionManager()).setMaxTotal(maxTotalConnectionProperty.get());
}
});
maxConnectionPerHostProperty = new DynamicIntProperty(this.name + "." + config.getNameSpace() + "." + CommonClientConfigKey.MaxHttpConnectionsPerHost.key(),
DefaultClientConfigImpl.DEFAULT_MAX_HTTP_CONNECTIONS_PER_HOST);
maxConnectionPerHostProperty.addCallback(new Runnable() {
@Override
public void run() {
((ThreadSafeClientConnManager) getConnectionManager()).setDefaultMaxPerRoute(maxConnectionPerHostProperty.get());
}
});
}
public void initConnectionCleanerTask(){
......
......@@ -33,7 +33,6 @@ import org.apache.http.util.EntityUtils;
import org.junit.Ignore;
import org.junit.Test;
@SuppressWarnings("deprecation")
public class NFHttpClientTest {
@Test
......@@ -136,7 +135,6 @@ public class NFHttpClientTest {
System.out.println(id + " - connection released");
}
}
}
}
......@@ -28,10 +28,13 @@ import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.junit.Test;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.config.ConfigurationManager;
public class NamedConnectionPoolTest {
@Test
public void testConnectionPoolCounters() throws Exception {
LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
// LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
NFHttpClient client = NFHttpClientFactory.getNamedNFHttpClient("google-NamedConnectionPoolTest");
assertTrue(client.getConnectionManager() instanceof MonitoredConnectionManager);
MonitoredConnectionManager connectionPoolManager = (MonitoredConnectionManager) client.getConnectionManager();
......@@ -62,6 +65,10 @@ public class NamedConnectionPoolTest {
assertEquals(0, connectionPool.getDeleteCount());
assertEquals(connectionPool.getReleaseCount(), connectionPool.getRequestsCount());
assertEquals(connectionPool.getRequestsCount(), connectionPool.getCreatedEntryCount() + connectionPool.getFreeEntryCount());
ConfigurationManager.getConfigInstance().setProperty("google-NamedConnectionPoolTest.ribbon." + CommonClientConfigKey.MaxTotalHttpConnections.key(), "50");
ConfigurationManager.getConfigInstance().setProperty("google-NamedConnectionPoolTest.ribbon." + CommonClientConfigKey.MaxHttpConnectionsPerHost.key(), "10");
assertEquals(50, connectionPoolManager.getMaxTotal());
assertEquals(10, connectionPoolManager.getDefaultMaxPerRoute());
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册