-
Notifications
You must be signed in to change notification settings - Fork 897
Expand file tree
/
Copy pathConnectionManager.java
More file actions
148 lines (129 loc) · 5.25 KB
/
ConnectionManager.java
File metadata and controls
148 lines (129 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.csource.fastdfs.pool;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ConnectionManager {
private InetSocketAddress inetSocketAddress;
/**
* total create connection pool
*/
private AtomicInteger totalCount = new AtomicInteger();
/**
* free connection count
*/
private AtomicInteger freeCount = new AtomicInteger();
/**
* lock
*/
private ReentrantLock lock = new ReentrantLock(true);
private Condition condition = lock.newCondition();
/**
* free connections
*/
private LinkedList<Connection> freeConnections = new LinkedList<Connection>();
private ConnectionManager() {
}
public ConnectionManager(InetSocketAddress socketAddress) {
this.inetSocketAddress = socketAddress;
}
public Connection getConnection() throws IOException {
lock.lock();
try {
Connection connection = null;
while (true) {
if (freeCount.get() > 0) {
freeCount.decrementAndGet();
connection = freeConnections.poll();
if (!connection.isAvaliable() || (System.currentTimeMillis() - connection.getLastAccessTime()) > ClientGlobal.g_connection_pool_max_idle_time) {
closeConnection(connection);
continue;
}
if (connection.isNeedActiveTest()) {
boolean isActive = false;
try {
isActive = connection.activeTest();
} catch (IOException e) {
System.err.println("send to server[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] active test error ,emsg:" + e.getMessage());
isActive = false;
}
if (!isActive) {
closeConnection(connection);
continue;
} else {
connection.setNeedActiveTest(false);
}
}
} else if (ClientGlobal.g_connection_pool_max_count_per_entry == 0 || totalCount.get() < ClientGlobal.g_connection_pool_max_count_per_entry) {
connection = ConnectionFactory.create(this.inetSocketAddress);
totalCount.incrementAndGet();
} else {
try {
if (condition.await(ClientGlobal.g_connection_pool_max_wait_time_in_ms, TimeUnit.MILLISECONDS)) {
//wait single success
continue;
}
throw new IOException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, wait_time > " + ClientGlobal.g_connection_pool_max_wait_time_in_ms + "ms");
} catch (InterruptedException e) {
e.printStackTrace();
throw new IOException("connect to server " + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + " fail, emsg:" + e.getMessage());
}
}
return connection;
}
} finally {
lock.unlock();
}
}
public void releaseConnection(Connection connection) {
if (connection == null) {
return;
}
lock.lock();
try {
connection.setLastAccessTime(System.currentTimeMillis());
freeConnections.add(connection);
freeCount.incrementAndGet();
condition.signal();
} finally {
lock.unlock();
}
}
public void closeConnection(Connection connection) {
try {
if (connection != null) {
totalCount.decrementAndGet();
connection.closeDirectly();
}
} catch (IOException e) {
System.err.println("close socket[" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() + "] error ,emsg:" + e.getMessage());
e.printStackTrace();
}
}
public void setActiveTestFlag() {
if (freeCount.get() > 0) {
lock.lock();
try {
for (Connection freeConnection : freeConnections) {
freeConnection.setNeedActiveTest(true);
}
} finally {
lock.unlock();
}
}
}
@Override
public String toString() {
return "ConnectionManager{" +
"ip:port='" + inetSocketAddress.getAddress().getHostAddress() + ":" + inetSocketAddress.getPort() +
", totalCount=" + totalCount +
", freeCount=" + freeCount +
", freeConnections =" + freeConnections +
'}';
}
}