-
Notifications
You must be signed in to change notification settings - Fork 165
/
ClientPool.java
138 lines (122 loc) · 4.53 KB
/
ClientPool.java
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
package io.milvus.pool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import java.time.Duration;
public class ClientPool<C, T> {
protected GenericKeyedObjectPool<String, T> clientPool;
protected PoolConfig config;
protected PoolClientFactory<C, T> clientFactory;
protected ClientPool() {
}
protected ClientPool(PoolConfig config, PoolClientFactory clientFactory) {
this.config = config;
this.clientFactory = clientFactory;
GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
poolConfig.setMaxIdlePerKey(config.getMaxIdlePerKey());
poolConfig.setMinIdlePerKey(config.getMinIdlePerKey());
poolConfig.setMaxTotal(config.getMaxTotal());
poolConfig.setMaxTotalPerKey(config.getMaxTotalPerKey());
poolConfig.setBlockWhenExhausted(config.isBlockWhenExhausted());
poolConfig.setMaxWait(config.getMaxBlockWaitDuration());
poolConfig.setTestOnBorrow(config.isTestOnBorrow());
poolConfig.setTestOnReturn(config.isTestOnReturn());
poolConfig.setTestOnCreate(false);
poolConfig.setTestWhileIdle(false);
poolConfig.setTimeBetweenEvictionRuns(config.getEvictionPollingInterval());
poolConfig.setNumTestsPerEvictionRun(5);
poolConfig.setMinEvictableIdleTime(config.getMinEvictableIdleDuration());
this.clientPool = new GenericKeyedObjectPool<String, T>(clientFactory, poolConfig);
}
/**
* Get a client object which is idle from the pool.
* Once the client is hold by the caller, it will be marked as active state and cannot be fetched by other caller.
* If the number of clients hits the MaxTotalPerKey value, this method will be blocked for MaxBlockWaitDuration.
* If no idle client available after MaxBlockWaitDuration, this method will return a null object to caller.
*
* @param key the key of a group where the client belong
* @return MilvusClient or MilvusClientV2
*/
public T getClient(String key) {
try {
return clientPool.borrowObject(key);
} catch (Exception e) {
System.out.println("Failed to get client, exception: " + e.getMessage());
return null;
}
}
/**
* Return a client object. Once a client is returned, it becomes idle state and wait the next caller.
* The caller should ensure the client is returned. Otherwise, the client will keep in active state and cannot be used by the next caller.
* Throw exceptions if the key doesn't exist or the client is not belong to this key group.
*
* @param key the key of a group where the client belong
* @param grpcClient the client object to return
*/
public void returnClient(String key, T grpcClient) {
try {
clientPool.returnObject(key, grpcClient);
} catch (Exception e) {
System.out.println("Failed to return client, exception: " + e.getMessage());
throw e;
}
}
/**
* Release/disconnect all clients of all key groups, close the pool.
*
*/
public void close() {
if (clientPool != null && !clientPool.isClosed()) {
clientPool.close();
clientPool = null;
}
}
/**
* Release/disconnect idle clients of all key groups.
*
*/
public void clear() {
if (clientPool != null && !clientPool.isClosed()) {
clientPool.clear();
}
}
/**
* Release/disconnect idle clients of a key group.
*
* @param key the key of a group
*/
public void clear(String key) {
if (clientPool != null && !clientPool.isClosed()) {
clientPool.clear(key);
}
}
/**
* Return the number of idle clients of a key group
*
* @param key the key of a group
*/
public int getIdleClientNumber(String key) {
return clientPool.getNumIdle(key);
}
/**
* Return the number of active clients of a key group
*
* @param key the key of a group
*/
public int getActiveClientNumber(String key) {
return clientPool.getNumActive(key);
}
/**
* Return the number of idle clients of all key group
*
*/
public int getTotalIdleClientNumber() {
return clientPool.getNumIdle();
}
/**
* Return the number of active clients of all key group
*
*/
public int getTotalActiveClientNumber() {
return clientPool.getNumActive();
}
}