44import com .tencent .tcvectordb .model .param .database .ConnectParam ;
55import com .tencent .tcvectordb .rpc .Interceptor .AuthorityInterceptor ;
66import io .grpc .ManagedChannel ;
7- import io .grpc .ManagedChannelBuilder ;
87import io .grpc .okhttp .OkHttpChannelBuilder ;
9- import org .apache .commons .pool2 .BasePooledObjectFactory ;
10- import org .apache .commons .pool2 .PooledObject ;
11- import org .apache .commons .pool2 .impl .DefaultPooledObject ;
12- import org .apache .commons .pool2 .impl .GenericObjectPool ;
13- import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
148
159import java .net .MalformedURLException ;
1610import java .net .URL ;
17- import java .time .Duration ;
18- import java .util .concurrent .TimeUnit ;
11+ import java .util .ArrayList ;
12+ import java .util .List ;
13+ import java .util .concurrent .atomic .AtomicLong ;
1914
2015public class ChannelPool {
21- private final GenericObjectPool <ManagedChannel > pool ;
16+ private List <ManagedChannel > pool ;
17+ private AtomicLong channelSelectorCounter = new AtomicLong (0 );
2218
2319 public ChannelPool (ConnectParam param , int maxReceiveMessageSize , String authorization ) {
24- GenericObjectPoolConfig <ManagedChannel > config = new GenericObjectPoolConfig <>();
25- config .setMaxTotal (param .getMaxIdleConnections ()); // 最大连接数
26- config .setMaxIdle (param .getMaxIdleConnections ()); // 最大空闲连接
27- config .setMaxWait (Duration .ofSeconds (param .getConnectTimeout ()));
2820
29- this .pool = new GenericObjectPool <>(new ChannelFactory ( getAddress ( param . getUrl ()), maxReceiveMessageSize , authorization ), config );
21+ this .pool = new ArrayList <>();
3022
31- for (int i = 0 ; i < pool . getMaxIdle ( ); i ++) {
23+ for (int i = 0 ; i < ( param . getConnectionPoolSize () ); i ++) {
3224 try {
33- pool .addObject (); // 添加一个初始对象到池中,直到达到maxIdle设置的数量
25+ pool .add (OkHttpChannelBuilder .forTarget (getAddress (param .getUrl ())).
26+ intercept (new AuthorityInterceptor (authorization )).
27+ flowControlWindow (maxReceiveMessageSize ).
28+ maxInboundMessageSize (maxReceiveMessageSize ).
29+ enableRetry ().
30+ usePlaintext ().build ());
3431 } catch (Exception e ) {
3532 throw new VectorDBException ("create channel pool error" , e );
3633 }
@@ -53,63 +50,26 @@ private String getAddress(String url){
5350
5451 public ManagedChannel getChannel () {
5552 try {
56- // printPoolStats();
57- return pool .borrowObject ();
53+ long count = channelSelectorCounter .incrementAndGet ();
54+ if (count < 0 ) {
55+ count = 0 ;
56+ channelSelectorCounter .set (0 );
57+ }
58+ Long index = count % pool .size ();
59+ return pool .get (index .intValue ());
5860 } catch (Exception e ) {
5961 throw new RuntimeException (e );
6062 }
6163 }
6264
63- public void returnChannel (ManagedChannel channel ) {
64- pool .returnObject (channel );
65- }
66-
6765 /**
6866 * 关闭连接池,会关闭池中所有 ManagedChannel
6967 */
7068 public void close () {
7169 if (pool != null ) {
72- pool .close ();
73- }
74- }
75-
76- public void printPoolStats () {
77- System .out .println ("Active: " + pool .getNumActive ());
78- System .out .println ("Idle: " + pool .getNumIdle ());
79- System .out .println ("Total created: " + pool .getCreatedCount ());
80- System .out .println ("Total borrowed: " + pool .getBorrowedCount ());
81- System .out .println ("Total returned: " + pool .getReturnedCount ());
82- }
83-
84- private static class ChannelFactory extends BasePooledObjectFactory <ManagedChannel > {
85- private final String url ;
86- private final int maxReceiveMessageSize ;
87- private final String authorization ;
88-
89- public ChannelFactory (String url , int maxReceiveMessageSize , String authorization ) {
90- this .url = url ;
91- this .maxReceiveMessageSize = maxReceiveMessageSize ;
92- this .authorization = authorization ;
93- }
94-
95- @ Override
96- public ManagedChannel create () {
97- return OkHttpChannelBuilder .forTarget (url ).
98- intercept (new AuthorityInterceptor (this .authorization )).
99- flowControlWindow (maxReceiveMessageSize ).
100- maxInboundMessageSize (maxReceiveMessageSize ).
101- enableRetry ().
102- usePlaintext ().build ();
103- }
104-
105- @ Override
106- public PooledObject <ManagedChannel > wrap (ManagedChannel channel ) {
107- return new DefaultPooledObject <>(channel );
108- }
109-
110- @ Override
111- public void destroyObject (PooledObject <ManagedChannel > p ) {
112- p .getObject ().shutdown ();
70+ for (ManagedChannel channel : pool ) {
71+ channel .shutdown ();
72+ }
11373 }
11474 }
11575}
0 commit comments