feature: add socket connection pool

This commit is contained in:
tanyawen
2019-12-27 21:41:10 +08:00
parent 70c292f0fc
commit 3ad9f94a69
12 changed files with 516 additions and 88 deletions
@@ -34,7 +34,6 @@ public class ClientGlobal {
public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key";
public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port";
public static final String CONF_KEY_TRACKER_SERVER = "tracker_server";
public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds";
public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds";
public static final String PROP_KEY_CHARSET = "fastdfs.charset";
@@ -43,6 +42,13 @@ public class ClientGlobal {
public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port";
public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers";
public static final String PROP_KEY_CONNECTION_POOL_ENABLED = "fastdfs.connection_pool.enabled";
public static final String PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = "fastdfs.connection_pool.max_count_per_entry";
public static final String PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME = "fastdfs.connection_pool.max_idle_time";
public static final String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME = "fastdfs.connection_pool.max_wait_time";
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
public static final String DEFAULT_CHARSET = "UTF-8";
@@ -50,6 +56,11 @@ public class ClientGlobal {
public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
public static final boolean DEFAULT_CONNECTION_POOL_ENABLED = true;
public static final int DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = 50;
public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 60 ;//second
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME = 3 ;//second
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
public static String g_charset = DEFAULT_CHARSET;
@@ -57,6 +68,11 @@ public class ClientGlobal {
public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key
public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;
public static boolean g_connection_pool_enabled = DEFAULT_CONNECTION_POOL_ENABLED;
public static int g_connection_pool_max_count_per_entry = DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY;
public static int g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME * 1000; //millisecond
public static int g_connection_pool_max_wait_time = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME * 1000; //millisecond
public static TrackerGroup g_tracker_group;
private ClientGlobal() {
@@ -112,6 +128,18 @@ public class ClientGlobal {
if (g_anti_steal_token) {
g_secret_key = iniReader.getStrValue("http.secret_key");
}
g_connection_pool_enabled = iniReader.getBoolValue("connection_pool.enabled", DEFAULT_CONNECTION_POOL_ENABLED);
g_connection_pool_max_count_per_entry = iniReader.getIntValue("connection_pool.max_count_per_entry", DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
g_connection_pool_max_idle_time = iniReader.getIntValue("connection_pool.max_idle_time", DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME);
if (g_connection_pool_max_idle_time < 0) {
g_connection_pool_max_idle_time = DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME;
}
g_connection_pool_max_idle_time *= 1000;
g_connection_pool_max_wait_time = iniReader.getIntValue("connection_pool.max_wait_time", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME);
if (g_connection_pool_max_wait_time < 0) {
g_connection_pool_max_wait_time = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME;
}
g_connection_pool_max_wait_time *= 1000;
}
/**
@@ -149,6 +177,11 @@ public class ClientGlobal {
String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN);
String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY);
String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT);
String poolEnabled = props.getProperty(PROP_KEY_CONNECTION_POOL_ENABLED);
String poolMaxCountPerEntry = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY);
String poolMaxIdleTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME);
String poolMaxWaitTime = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME);
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
}
@@ -167,6 +200,18 @@ public class ClientGlobal {
if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) {
g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf);
}
if (poolEnabled != null && poolEnabled.trim().length() != 0) {
g_connection_pool_enabled = Boolean.parseBoolean(poolEnabled);
}
if (poolMaxCountPerEntry != null && poolMaxCountPerEntry.trim().length() != 0 ) {
g_connection_pool_max_count_per_entry = Integer.parseInt(poolMaxCountPerEntry);
}
if (poolMaxIdleTime != null && poolMaxIdleTime.trim().length() != 0) {
g_connection_pool_max_idle_time = Integer.parseInt(poolMaxIdleTime) * 1000;
}
if (poolMaxWaitTime != null && poolMaxWaitTime.trim().length() != 0) {
g_connection_pool_max_wait_time = Integer.parseInt(poolMaxWaitTime) * 1000;
}
}
/**
@@ -217,6 +262,7 @@ public class ClientGlobal {
*/
public static Socket getSocket(InetSocketAddress addr) throws IOException {
Socket sock = new Socket();
sock.setReuseAddress(true);
sock.setSoTimeout(ClientGlobal.g_network_timeout);
sock.connect(addr, ClientGlobal.g_connect_timeout);
return sock;
@@ -282,6 +328,22 @@ public class ClientGlobal {
ClientGlobal.g_tracker_group = tracker_group;
}
public static boolean isG_connection_pool_enabled() {
return g_connection_pool_enabled;
}
public static int getG_connection_pool_max_count_per_entry() {
return g_connection_pool_max_count_per_entry;
}
public static int getG_connection_pool_max_idle_time() {
return g_connection_pool_max_idle_time;
}
public static int getG_connection_pool_max_wait_time() {
return g_connection_pool_max_wait_time;
}
public static String configInfo() {
String trackerServers = "";
if (g_tracker_group != null) {
@@ -298,6 +360,10 @@ public class ClientGlobal {
+ "\n g_anti_steal_token = " + g_anti_steal_token
+ "\n g_secret_key = " + g_secret_key
+ "\n g_tracker_http_port = " + g_tracker_http_port
+ "\n g_connection_pool_enabled = " + g_connection_pool_enabled
+ "\n g_connection_pool_max_count_per_entry = " + g_connection_pool_max_count_per_entry
+ "\n g_connection_pool_max_idle_time = " + g_connection_pool_max_idle_time
+ "\n g_connection_pool_max_wait_time = " + g_connection_pool_max_wait_time
+ "\n trackerServers = " + trackerServers
+ "\n}";
}