Closed NikaKingLuffy closed 1 month ago
Java sdk doesn't have connection pool. Each MilvusClient object maintains a long connection. You can use a Map<String, MilvusClient> to build your simple connection pool.
You can use LinkedBlockingQueue to build connection pool,if you have more milvus IP,you can use ConcurrentHashMap<String,LinkedBlockingQueue>. Also,you can use Apache-commonPool,etc. to achieve it. Follow is a simple example: public class MilvusConnectionPool {
private final BlockingQueue<MilvusServiceClient> pool = new LinkedBlockingQueue<>(); ;
private String poolName;
public MilvusConnectionPool(String poolName,MilvusConfig milvusConfig,int poolSize){
this.poolName = poolName;
init(milvusConfig,poolSize);
}
private void init(MilvusConfig milvusConfig,int poolSize){
int maxPoolSize = 1;
if(poolSize>0){
maxPoolSize = poolSize;
}
for (int i = 0; i < maxPoolSize; i++) {
try {
ConnectParam connectParam = ConnectParam.newBuilder()
.withHost(milvusConfig.getHost())
.withPort(milvusConfig.getPort())
.withAuthorization(milvusConfig.getUser(), milvusConfig.getPassword())
.build();
MilvusServiceClient client = new MilvusServiceClient(connectParam);
client.withRetry(3);
pool.offer(client);
} catch (Exception e) {
e.printStackTrace();
System.out.println("[ERROR]init milvus connection pool throw exception!");
}
}
System.out.println(String.format("[INFO]%s-milvus连接池【%s:%s】初始化完成,共有%s个连接",this.poolName,milvusConfig.getHost(),milvusConfig.getPort(),pool.size()));
}
public MilvusServiceClient getConnection() throws InterruptedException {
return pool.take();
}
public void releaseConnection(MilvusServiceClient client) {
if (client == null) return;
pool.offer(client);
}
public void closeAll() {
for (MilvusServiceClient client : pool) {
client.close();
}
pool.clear();
System.out.println(String.format("[INFO]%s-milvus连接池释放所有连接完成!",this.poolName));
}
}
I believe connection pool is a great feature and it would be great to have it.
The latest version v2.4.3 and v2.3.9 have been supported connection pool, close this issue.
How can milvus-java-sdk's milvusClient support connection pooling? I noticed that the Python version of milvus-sdk can simply configure multiple connections by specifying an alias, but I couldn't find a similar feature in the java-sdk. If the java-sdk does not currently support connection pooling, could you please provide some ideas on how to implement this feature?
Thank you for your help!感谢回复!