feature:add connection pool v2
This commit is contained in:
@@ -46,7 +46,7 @@ public class ClientGlobal {
|
||||
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 String PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = "fastdfs.connection_pool.max_wait_time_in_ms";
|
||||
|
||||
|
||||
public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second
|
||||
@@ -56,10 +56,10 @@ 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 boolean DEFAULT_CONNECTION_POOL_ENABLED = false;
|
||||
public static final int DEFAULT_CONNECTION_POOL_MAX_COUNT_PER_ENTRY = 100;
|
||||
public static final int DEFAULT_CONNECTION_POOL_MAX_IDLE_TIME = 60 ;//second
|
||||
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME = 5 ;//second
|
||||
public static final int DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS = 5000 ;//millisecond
|
||||
|
||||
public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond
|
||||
public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond
|
||||
@@ -71,7 +71,7 @@ public class ClientGlobal {
|
||||
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 int g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS; //millisecond
|
||||
|
||||
public static TrackerGroup g_tracker_group;
|
||||
|
||||
@@ -135,11 +135,10 @@ public class ClientGlobal {
|
||||
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_in_ms = iniReader.getIntValue("connection_pool.max_wait_time_in_ms", DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
|
||||
if (g_connection_pool_max_wait_time_in_ms < 0) {
|
||||
g_connection_pool_max_wait_time_in_ms = DEFAULT_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS;
|
||||
}
|
||||
g_connection_pool_max_wait_time *= 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,7 +179,7 @@ public class ClientGlobal {
|
||||
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);
|
||||
String poolMaxWaitTimeInMS = props.getProperty(PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS);
|
||||
|
||||
if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
|
||||
g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
|
||||
@@ -209,8 +208,8 @@ public class ClientGlobal {
|
||||
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;
|
||||
if (poolMaxWaitTimeInMS != null && poolMaxWaitTimeInMS.trim().length() != 0) {
|
||||
g_connection_pool_max_wait_time_in_ms = Integer.parseInt(poolMaxWaitTimeInMS) * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -340,8 +339,8 @@ public class ClientGlobal {
|
||||
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 int getG_connection_pool_max_wait_time_in_ms() {
|
||||
return g_connection_pool_max_wait_time_in_ms;
|
||||
}
|
||||
|
||||
public static String configInfo() {
|
||||
@@ -363,7 +362,7 @@ public class ClientGlobal {
|
||||
+ "\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 g_connection_pool_max_wait_time_in_ms = " + g_connection_pool_max_wait_time_in_ms
|
||||
+ "\n trackerServers = " + trackerServers
|
||||
+ "\n}";
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -10,7 +10,6 @@ package org.csource.fastdfs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Tracker server group
|
||||
@@ -39,7 +38,7 @@ public class TrackerGroup {
|
||||
*
|
||||
* @return connected tracker server, null for fail
|
||||
*/
|
||||
public TrackerServer getConnection(int serverIndex) throws IOException {
|
||||
public TrackerServer getTrackerServer(int serverIndex) throws IOException {
|
||||
return new TrackerServer(this.tracker_servers[serverIndex]);
|
||||
}
|
||||
|
||||
@@ -48,7 +47,7 @@ public class TrackerGroup {
|
||||
*
|
||||
* @return connected tracker server, null for fail
|
||||
*/
|
||||
public TrackerServer getConnection() throws IOException {
|
||||
public TrackerServer getTrackerServer() throws IOException {
|
||||
int current_index;
|
||||
|
||||
synchronized (this.lock) {
|
||||
@@ -61,7 +60,7 @@ public class TrackerGroup {
|
||||
}
|
||||
|
||||
try {
|
||||
return this.getConnection(current_index);
|
||||
return this.getTrackerServer(current_index);
|
||||
} catch (IOException ex) {
|
||||
System.err.println("connect to server " + this.tracker_servers[current_index].getAddress().getHostAddress() + ":" + this.tracker_servers[current_index].getPort() + " fail");
|
||||
ex.printStackTrace(System.err);
|
||||
@@ -73,7 +72,7 @@ public class TrackerGroup {
|
||||
}
|
||||
|
||||
try {
|
||||
TrackerServer trackerServer = this.getConnection(i);
|
||||
TrackerServer trackerServer = this.getTrackerServer(i);
|
||||
|
||||
synchronized (this.lock) {
|
||||
if (this.tracker_server_index == current_index) {
|
||||
|
@@ -8,14 +8,13 @@
|
||||
|
||||
package org.csource.fastdfs;
|
||||
|
||||
import org.csource.fastdfs.pool.ConnectionInfo;
|
||||
import org.csource.common.MyException;
|
||||
import org.csource.fastdfs.pool.Connection;
|
||||
import org.csource.fastdfs.pool.ConnectionPool;
|
||||
import org.csource.fastdfs.pool.ConnectionFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Tracker Server Info
|
||||
@@ -24,46 +23,22 @@ import java.net.Socket;
|
||||
* @version Version 1.11
|
||||
*/
|
||||
public class TrackerServer {
|
||||
protected Socket sock;
|
||||
protected InetSocketAddress inetSockAddr;
|
||||
|
||||
private Long lastAccessTime = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param sock Socket of server
|
||||
* @param inetSockAddr the server info
|
||||
*/
|
||||
public TrackerServer(Socket sock, InetSocketAddress inetSockAddr) {
|
||||
this.sock = sock;
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
}
|
||||
|
||||
public TrackerServer(InetSocketAddress inetSockAddr) throws IOException {
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
this.sock = getSocket();
|
||||
}
|
||||
|
||||
/**
|
||||
* get the connected socket
|
||||
*
|
||||
* @return the socket
|
||||
*/
|
||||
public Socket getSocket() throws IOException {
|
||||
if (this.sock == null) {
|
||||
if (ClientGlobal.g_connection_pool_enabled) {
|
||||
ConnectionInfo connection = ConnectionPool.getConnection(this.inetSockAddr);
|
||||
this.sock = connection.getSocket();
|
||||
this.lastAccessTime = connection.getLastAccessTime();
|
||||
} else {
|
||||
this.sock = ClientGlobal.getSocket(this.inetSockAddr);
|
||||
}
|
||||
public Connection getConnection() throws MyException, IOException {
|
||||
Connection connection;
|
||||
if (ClientGlobal.g_connection_pool_enabled) {
|
||||
connection = ConnectionPool.getConnection(this.inetSockAddr);
|
||||
} else {
|
||||
connection = ConnectionFactory.create(this.inetSockAddr);
|
||||
}
|
||||
|
||||
return this.sock;
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the server info
|
||||
*
|
||||
@@ -72,88 +47,28 @@ public class TrackerServer {
|
||||
public InetSocketAddress getInetSocketAddress() {
|
||||
return this.inetSockAddr;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return this.sock.getOutputStream();
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return this.sock.getInputStream();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
public void close(Connection connection) throws IOException {
|
||||
//if connection enabled get from connection pool
|
||||
if (ClientGlobal.g_connection_pool_enabled) {
|
||||
ConnectionPool.freeConnection(this);
|
||||
ConnectionPool.closeConnection(connection);
|
||||
} else {
|
||||
this.closeDirect();
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* close direct if not create from pool or not open pool
|
||||
* releaseConnection connection
|
||||
* @param connection
|
||||
* @throws IOException
|
||||
*/
|
||||
public void closeDirect() throws IOException {
|
||||
if (this.sock != null) {
|
||||
try {
|
||||
ProtoCommon.closeSocket(this.sock);
|
||||
} finally {
|
||||
this.sock = null;
|
||||
}
|
||||
public void releaseConnection(Connection connection) throws IOException {
|
||||
if (ClientGlobal.g_connection_pool_enabled) {
|
||||
ConnectionPool.releaseConnection(connection);
|
||||
} else {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
protected void finalize() throws Throwable {
|
||||
this.close();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
boolean isConnected = false;
|
||||
if (sock != null) {
|
||||
if (sock.isConnected()) {
|
||||
isConnected = true;
|
||||
}
|
||||
}
|
||||
return isConnected;
|
||||
}
|
||||
|
||||
public boolean isAvaliable() {
|
||||
if (isConnected()) {
|
||||
if (sock.getPort() == 0) {
|
||||
return false;
|
||||
}
|
||||
if (sock.getInetAddress() == null) {
|
||||
return false;
|
||||
}
|
||||
if (sock.getRemoteSocketAddress() == null) {
|
||||
return false;
|
||||
}
|
||||
if (sock.isInputShutdown()) {
|
||||
return false;
|
||||
}
|
||||
if (sock.isOutputShutdown()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Long getLastAccessTime() {
|
||||
return lastAccessTime;
|
||||
}
|
||||
|
||||
public void setLastAccessTime(Long lastAccessTime) {
|
||||
this.lastAccessTime = lastAccessTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TrackerServer{" +
|
||||
"sock=" + sock +
|
||||
", inetSockAddr=" + inetSockAddr +
|
||||
", lastAccessTime=" + lastAccessTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
107
src/main/java/org/csource/fastdfs/pool/Connection.java
Normal file
107
src/main/java/org/csource/fastdfs/pool/Connection.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package org.csource.fastdfs.pool;
|
||||
|
||||
import org.csource.fastdfs.ProtoCommon;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Connection {
|
||||
|
||||
private Socket sock;
|
||||
private InetSocketAddress inetSockAddr;
|
||||
private Long lastAccessTime = System.currentTimeMillis();
|
||||
|
||||
public Connection(Socket sock, InetSocketAddress inetSockAddr) {
|
||||
this.sock = sock;
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the server info
|
||||
*
|
||||
* @return the server info
|
||||
*/
|
||||
public InetSocketAddress getInetSocketAddress() {
|
||||
return this.inetSockAddr;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
return this.sock.getOutputStream();
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return this.sock.getInputStream();
|
||||
}
|
||||
|
||||
public Long getLastAccessTime() {
|
||||
return lastAccessTime;
|
||||
}
|
||||
|
||||
public void setLastAccessTime(Long lastAccessTime) {
|
||||
this.lastAccessTime = lastAccessTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* close direct if not create from pool or not open pool
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if (this.sock != null) {
|
||||
try {
|
||||
ProtoCommon.closeSocket(this.sock);
|
||||
} finally {
|
||||
this.sock = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean activeTest() throws IOException {
|
||||
if (this.sock == null) {
|
||||
return false;
|
||||
}
|
||||
return ProtoCommon.activeTest(this.sock);
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
boolean isConnected = false;
|
||||
if (sock != null) {
|
||||
if (sock.isConnected()) {
|
||||
isConnected = true;
|
||||
}
|
||||
}
|
||||
return isConnected;
|
||||
}
|
||||
public boolean isAvaliable() {
|
||||
if (isConnected()) {
|
||||
if (sock.getPort() == 0) {
|
||||
return false;
|
||||
}
|
||||
if (sock.getInetAddress() == null) {
|
||||
return false;
|
||||
}
|
||||
if (sock.getRemoteSocketAddress() == null) {
|
||||
return false;
|
||||
}
|
||||
if (sock.isInputShutdown()) {
|
||||
return false;
|
||||
}
|
||||
if (sock.isOutputShutdown()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TrackerServer{" +
|
||||
"sock=" + sock +
|
||||
", inetSockAddr=" + inetSockAddr +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -5,9 +5,8 @@ import org.csource.fastdfs.ClientGlobal;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class PoolConnectionFactory {
|
||||
public static ConnectionInfo create(String key) throws IOException {
|
||||
public class ConnectionFactory {
|
||||
public static Connection create(String key) throws IOException {
|
||||
if (key == null) {
|
||||
System.err.printf("ip:port entry conn't be null");
|
||||
return null;
|
||||
@@ -24,7 +23,21 @@ public class PoolConnectionFactory {
|
||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||
InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
|
||||
sock.connect(inetSocketAddress, ClientGlobal.g_connect_timeout);
|
||||
return new ConnectionInfo(sock, inetSocketAddress, System.currentTimeMillis(), false);
|
||||
return new Connection(sock, inetSocketAddress);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* create from InetSocketAddress
|
||||
* @param socketAddress
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Connection create(InetSocketAddress socketAddress) throws IOException {
|
||||
Socket sock = new Socket();
|
||||
sock.setReuseAddress(true);
|
||||
sock.setSoTimeout(ClientGlobal.g_network_timeout);
|
||||
sock.connect(socketAddress, ClientGlobal.g_connect_timeout);
|
||||
return new Connection(sock, socketAddress);
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
package org.csource.fastdfs.pool;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ConnectionInfo {
|
||||
private Socket socket;
|
||||
protected InetSocketAddress inetSockAddr;
|
||||
private Long lastAccessTime;
|
||||
private boolean needActiveCheck = false;
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public void setSocket(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public InetSocketAddress getInetSockAddr() {
|
||||
return inetSockAddr;
|
||||
}
|
||||
|
||||
public void setInetSockAddr(InetSocketAddress inetSockAddr) {
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
}
|
||||
|
||||
public Long getLastAccessTime() {
|
||||
return lastAccessTime;
|
||||
}
|
||||
|
||||
public void setLastAccessTime(Long lastAccessTime) {
|
||||
this.lastAccessTime = lastAccessTime;
|
||||
}
|
||||
|
||||
public boolean isNeedActiveCheck() {
|
||||
return needActiveCheck;
|
||||
}
|
||||
|
||||
public void setNeedActiveCheck(boolean needActiveCheck) {
|
||||
this.needActiveCheck = needActiveCheck;
|
||||
}
|
||||
|
||||
public ConnectionInfo(Socket socket, InetSocketAddress inetSockAddr, Long lastAccessTime, boolean needActiveCheck) {
|
||||
this.socket = socket;
|
||||
this.inetSockAddr = inetSockAddr;
|
||||
this.lastAccessTime = lastAccessTime;
|
||||
this.needActiveCheck = needActiveCheck;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConnectionInfo{" +
|
||||
"socket=" + socket +
|
||||
", inetSockAddr=" + inetSockAddr +
|
||||
", lastAccessTime=" + lastAccessTime +
|
||||
", needActiveCheck=" + needActiveCheck +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -1,9 +1,7 @@
|
||||
package org.csource.fastdfs.pool;
|
||||
|
||||
import org.csource.common.MyException;
|
||||
import org.csource.fastdfs.ClientGlobal;
|
||||
import org.csource.fastdfs.ProtoCommon;
|
||||
import org.csource.fastdfs.TrackerServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -37,7 +35,7 @@ public class ConnectionManager {
|
||||
/**
|
||||
* free connections
|
||||
*/
|
||||
private volatile ConcurrentLinkedQueue<ConnectionInfo> freeConnections = new ConcurrentLinkedQueue<ConnectionInfo>();
|
||||
private volatile ConcurrentLinkedQueue<Connection> freeConnections = new ConcurrentLinkedQueue<Connection>();
|
||||
|
||||
private ConnectionManager() {
|
||||
|
||||
@@ -47,57 +45,46 @@ public class ConnectionManager {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
private synchronized ConnectionInfo newConnection() throws IOException {
|
||||
private Connection newConnection() throws IOException {
|
||||
try {
|
||||
ConnectionInfo connectionInfo = PoolConnectionFactory.create(this.key);
|
||||
return connectionInfo;
|
||||
|
||||
Connection connection = ConnectionFactory.create(this.key);
|
||||
return connection;
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public synchronized ConnectionInfo getConnection() throws IOException {
|
||||
public Connection getConnection() throws MyException {
|
||||
lock.lock();
|
||||
try {
|
||||
ConnectionInfo connectionInfo = null;
|
||||
Connection connection = null;
|
||||
while (true) {
|
||||
if (freeCount.get() > 0) {
|
||||
connectionInfo = freeConnections.poll();
|
||||
if ((System.currentTimeMillis() - connectionInfo.getLastAccessTime()) > ClientGlobal.getG_connection_pool_max_idle_time()) {
|
||||
closeConnection(connectionInfo);
|
||||
freeCount.decrementAndGet();
|
||||
connection = freeConnections.poll();
|
||||
if (!connection.isAvaliable() || (System.currentTimeMillis() - connection.getLastAccessTime()) > ClientGlobal.getG_connection_pool_max_idle_time()) {
|
||||
closeConnection(connection);
|
||||
continue;
|
||||
} else {
|
||||
freeCount.decrementAndGet();
|
||||
}
|
||||
} else if (ClientGlobal.getG_connection_pool_max_count_per_entry() == 0 || totalCount.get() < ClientGlobal.getG_connection_pool_max_count_per_entry()) {
|
||||
connectionInfo = newConnection();
|
||||
if (connectionInfo != null) {
|
||||
connection = newConnection();
|
||||
if (connection != null) {
|
||||
totalCount.incrementAndGet();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
if (condition.await(ClientGlobal.getG_connection_pool_max_wait_time(), TimeUnit.MILLISECONDS)) {
|
||||
if (condition.await(ClientGlobal.getG_connection_pool_max_wait_time_in_ms(), TimeUnit.MILLISECONDS)) {
|
||||
//wait single success
|
||||
continue;
|
||||
}
|
||||
throw new MyException("get connection fail, wait_time greater than " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
throw new MyException("get connection fail, emsg > " + e.getMessage());
|
||||
}
|
||||
}
|
||||
//if need check active
|
||||
if (connectionInfo.isNeedActiveCheck()) {
|
||||
boolean activeYes = ProtoCommon.activeTest(connectionInfo.getSocket());
|
||||
if (activeYes) {
|
||||
connectionInfo.setLastAccessTime(System.currentTimeMillis());
|
||||
} else {
|
||||
//close if check fail
|
||||
closeConnection(connectionInfo);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return connectionInfo;
|
||||
return connection;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
@@ -106,33 +93,34 @@ public class ConnectionManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void freeConnection(TrackerServer trackerServer) throws IOException {
|
||||
if (trackerServer == null || !trackerServer.isConnected()) {
|
||||
public void releaseConnection(Connection connection) throws IOException {
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true);
|
||||
if ((System.currentTimeMillis() - trackerServer.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) {
|
||||
if ((System.currentTimeMillis() - connection.getLastAccessTime()) < ClientGlobal.g_connection_pool_max_idle_time) {
|
||||
try {
|
||||
lock.lock();
|
||||
freeConnections.add(connectionInfo);
|
||||
freeConnections.add(connection);
|
||||
freeCount.incrementAndGet();
|
||||
condition.signal();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
} else {
|
||||
closeConnection(connectionInfo);
|
||||
closeConnection(connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void closeConnection(ConnectionInfo connectionInfo) throws IOException {
|
||||
if (connectionInfo.getSocket() != null) {
|
||||
totalCount.decrementAndGet();
|
||||
try {
|
||||
ProtoCommon.closeSocket(connectionInfo.getSocket());
|
||||
} finally {
|
||||
connectionInfo.setSocket(null);
|
||||
public void closeConnection(Connection connection) throws IOException {
|
||||
try {
|
||||
if (connection != null) {
|
||||
totalCount.decrementAndGet();
|
||||
connection.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("close socket error , msg:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package org.csource.fastdfs.pool;
|
||||
|
||||
import org.csource.fastdfs.TrackerServer;
|
||||
import org.csource.common.MyException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -13,54 +13,51 @@ public class ConnectionPool {
|
||||
*/
|
||||
private final static ConcurrentHashMap<String, ConnectionManager> CP = new ConcurrentHashMap<String, ConnectionManager>();
|
||||
|
||||
public static synchronized ConnectionInfo getConnection(InetSocketAddress socketAddress) throws IOException {
|
||||
public static Connection getConnection(InetSocketAddress socketAddress) throws MyException {
|
||||
if (socketAddress == null) {
|
||||
return null;
|
||||
}
|
||||
String key = getKey(socketAddress);
|
||||
ConnectionManager connectionManager = CP.get(key);
|
||||
if (connectionManager == null) {
|
||||
connectionManager = new ConnectionManager(key);
|
||||
CP.put(key, connectionManager);
|
||||
ConnectionManager connectionManager;
|
||||
synchronized (ConnectionPool.class) {
|
||||
connectionManager = CP.get(key);
|
||||
if (connectionManager == null) {
|
||||
connectionManager = new ConnectionManager(key);
|
||||
CP.put(key, connectionManager);
|
||||
}
|
||||
}
|
||||
return connectionManager.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* release connection
|
||||
*/
|
||||
public static void closeConnection(TrackerServer trackerServer) throws IOException {
|
||||
if (trackerServer == null || trackerServer.getInetSocketAddress() == null) {
|
||||
public static void releaseConnection(Connection connection) throws IOException {
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
String key = getKey(trackerServer.getInetSocketAddress());
|
||||
if (key != null) {
|
||||
ConnectionManager connectionManager = CP.get(key);
|
||||
if (connectionManager != null) {
|
||||
connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true));
|
||||
} else {
|
||||
trackerServer.closeDirect();
|
||||
}
|
||||
String key = getKey(connection.getInetSocketAddress());
|
||||
ConnectionManager connectionManager = CP.get(key);
|
||||
if (connectionManager != null) {
|
||||
connectionManager.releaseConnection(connection);
|
||||
} else {
|
||||
trackerServer.closeDirect();
|
||||
try {
|
||||
connection.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println("close socket error, msg:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void freeConnection(TrackerServer trackerServer) throws IOException {
|
||||
if (trackerServer == null || trackerServer.getInetSocketAddress() == null) {
|
||||
public static void closeConnection(Connection connection) throws IOException {
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
String key = getKey(trackerServer.getInetSocketAddress());
|
||||
if (key != null) {
|
||||
ConnectionManager connectionManager = CP.get(key);
|
||||
if (connectionManager != null) {
|
||||
connectionManager.freeConnection(trackerServer);
|
||||
} else {
|
||||
trackerServer.closeDirect();
|
||||
}
|
||||
|
||||
String key = getKey(connection.getInetSocketAddress());
|
||||
ConnectionManager connectionManager = CP.get(key);
|
||||
if (connectionManager != null) {
|
||||
connectionManager.closeConnection(connection);
|
||||
} else {
|
||||
trackerServer.closeDirect();
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +67,7 @@ public class ConnectionPool {
|
||||
}
|
||||
return String.format("%s:%s", socketAddress.getHostName(), socketAddress.getPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (!CP.isEmpty()) {
|
||||
|
@@ -14,4 +14,4 @@ fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
|
||||
fastdfs.connection_pool.enabled = false
|
||||
fastdfs.connection_pool.max_count_per_entry = 50
|
||||
fastdfs.connection_pool.max_idle_time = 60
|
||||
fastdfs.connection_pool.max_wait_time = 5
|
||||
fastdfs.connection_pool.max_wait_time_in_ms = 5000
|
@@ -18,4 +18,4 @@ fastdfs.connection_pool.enabled = false
|
||||
fastdfs.connection_pool.max_count_per_entry = 100
|
||||
|
||||
fastdfs.connection_pool.max_idle_time = 60
|
||||
fastdfs.connection_pool.max_wait_time = 5
|
||||
fastdfs.connection_pool.max_wait_time_in_ms = 5000
|
@@ -11,4 +11,4 @@ tracker_server = 10.0.11.244:22122
|
||||
connection_pool.enabled = false
|
||||
connection_pool.max_count_per_entry = 100
|
||||
connection_pool.max_idle_time = 60
|
||||
connection_pool.max_wait_time = 5
|
||||
connection_pool.max_wait_time_in_ms = 5000
|
Reference in New Issue
Block a user