From 6aa61096b2d6b5f7cb76d24058be595ea7baee75 Mon Sep 17 00:00:00 2001 From: tanyawen Date: Sat, 28 Dec 2019 01:43:42 +0800 Subject: [PATCH] feature: add socket connection pool --- .../java/org/csource/fastdfs/TrackerServer.java | 9 +++++++++ .../org/csource/fastdfs/pool/ConnectionInfo.java | 10 ++++++++++ .../csource/fastdfs/pool/ConnectionManager.java | 7 +++---- .../org/csource/fastdfs/pool/ConnectionPool.java | 14 ++++++++++++-- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/csource/fastdfs/TrackerServer.java b/src/main/java/org/csource/fastdfs/TrackerServer.java index bfce3b4..4d5c31f 100644 --- a/src/main/java/org/csource/fastdfs/TrackerServer.java +++ b/src/main/java/org/csource/fastdfs/TrackerServer.java @@ -147,4 +147,13 @@ public class TrackerServer { public void setLastAccessTime(Long lastAccessTime) { this.lastAccessTime = lastAccessTime; } + + @Override + public String toString() { + return "TrackerServer{" + + "sock=" + sock + + ", inetSockAddr=" + inetSockAddr + + ", lastAccessTime=" + lastAccessTime + + '}'; + } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionInfo.java b/src/main/java/org/csource/fastdfs/pool/ConnectionInfo.java index 45898f2..9da1d76 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionInfo.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionInfo.java @@ -47,4 +47,14 @@ public class ConnectionInfo { this.lastAccessTime = lastAccessTime; this.needActiveCheck = needActiveCheck; } + + @Override + public String toString() { + return "ConnectionInfo{" + + "socket=" + socket + + ", inetSockAddr=" + inetSockAddr + + ", lastAccessTime=" + lastAccessTime + + ", needActiveCheck=" + needActiveCheck + + '}'; + } } diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java index 84c0d40..7c5a927 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionManager.java @@ -109,12 +109,11 @@ public class ConnectionManager { } } - public void freeConnection(TrackerServer trackerServer) throws IOException { - if (trackerServer == null || trackerServer.getSocket() == null) { + public void freeConnection(ConnectionInfo connectionInfo) throws IOException { + if (connectionInfo == null || connectionInfo.getSocket() == 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() - connectionInfo.getLastAccessTime()) < ClientGlobal.getG_connection_pool_max_idle_time()) { try { lock.lock(); freeConnections.add(connectionInfo); diff --git a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java index 5e92ec5..62813c6 100644 --- a/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java +++ b/src/main/java/org/csource/fastdfs/pool/ConnectionPool.java @@ -36,7 +36,11 @@ public class ConnectionPool { String key = getKey(trackerServer.getInetSocketAddress()); if (key != null) { ConnectionManager connectionManager = CP.get(key); - connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true)); + if (connectionManager != null) { + connectionManager.closeConnection(new ConnectionInfo(trackerServer.getSocket(), trackerServer.getInetSocketAddress(),trackerServer.getLastAccessTime(),true)); + } else { + trackerServer.closeDirect(); + } } else { trackerServer.closeDirect(); } @@ -49,7 +53,13 @@ public class ConnectionPool { String key = getKey(trackerServer.getInetSocketAddress()); if (key != null) { ConnectionManager connectionManager = CP.get(key); - connectionManager.freeConnection(trackerServer); + if (connectionManager != null) { + ConnectionInfo connectionInfo = new ConnectionInfo(trackerServer.getSocket(),trackerServer.getInetSocketAddress(),System.currentTimeMillis(),true); + connectionManager.freeConnection(connectionInfo); + } else { + trackerServer.closeDirect(); + } + } else { trackerServer.closeDirect(); }