@@ -32,11 +32,14 @@ import (
3232// ConnectionPool is a interface of connection pool.
3333type ConnectionPool interface {
3434 // GetConnection get a connection from ConnectionPool.
35- GetConnection (logicalAddr * url.URL , physicalAddr * url.URL ) (Connection , error )
35+ GetConnection (logicalAddr * url.URL , physicalAddr * url.URL , keySuffix int32 ) (Connection , error )
3636
3737 // GetConnections get all connections in the pool.
3838 GetConnections () map [string ]Connection
3939
40+ // GenerateRoundRobinIndex generates a round-robin index.
41+ GenerateRoundRobinIndex () int32
42+
4043 // Close all the connections in the pool
4144 Close ()
4245}
@@ -47,8 +50,8 @@ type connectionPool struct {
4750 connectionTimeout time.Duration
4851 tlsOptions * TLSOptions
4952 auth auth.Provider
50- maxConnectionsPerHost int32
51- roundRobinCnt int32
53+ maxConnectionsPerHost uint32
54+ roundRobinCnt uint32
5255 keepAliveInterval time.Duration
5356 closeCh chan struct {}
5457
@@ -73,7 +76,7 @@ func NewConnectionPool(
7376 tlsOptions : tlsOptions ,
7477 auth : auth ,
7578 connectionTimeout : connectionTimeout ,
76- maxConnectionsPerHost : int32 (maxConnectionsPerHost ),
79+ maxConnectionsPerHost : uint32 (maxConnectionsPerHost ),
7780 keepAliveInterval : keepAliveInterval ,
7881 log : logger ,
7982 metrics : metrics ,
@@ -84,9 +87,12 @@ func NewConnectionPool(
8487 return p
8588}
8689
87- func (p * connectionPool ) GetConnection (logicalAddr * url.URL , physicalAddr * url.URL ) (Connection , error ) {
88- p .log .WithField ("logicalAddr" , logicalAddr ).WithField ("physicalAddr" , physicalAddr ).Debug ("Getting pooled connection" )
89- key := p .getMapKey (logicalAddr , physicalAddr )
90+ func (p * connectionPool ) GetConnection (logicalAddr * url.URL , physicalAddr * url.URL ,
91+ keySuffix int32 ) (Connection , error ) {
92+ p .log .WithField ("logicalAddr" , logicalAddr ).
93+ WithField ("physicalAddr" , physicalAddr ).
94+ WithField ("keySuffix" , keySuffix ).Debug ("Getting pooled connection" )
95+ key := fmt .Sprint (logicalAddr .Host , "-" , physicalAddr .Host , "-" , keySuffix )
9096
9197 p .Lock ()
9298 conn , ok := p .connections [key ]
@@ -141,6 +147,10 @@ func (p *connectionPool) GetConnections() map[string]Connection {
141147 return conns
142148}
143149
150+ func (p * connectionPool ) GenerateRoundRobinIndex () int32 {
151+ return int32 (atomic .AddUint32 (& p .roundRobinCnt , 1 ) % p .maxConnectionsPerHost )
152+ }
153+
144154func (p * connectionPool ) Close () {
145155 p .Lock ()
146156 close (p .closeCh )
@@ -151,15 +161,6 @@ func (p *connectionPool) Close() {
151161 p .Unlock ()
152162}
153163
154- func (p * connectionPool ) getMapKey (logicalAddr * url.URL , physicalAddr * url.URL ) string {
155- cnt := atomic .AddInt32 (& p .roundRobinCnt , 1 )
156- if cnt < 0 {
157- cnt = - cnt
158- }
159- idx := cnt % p .maxConnectionsPerHost
160- return fmt .Sprint (logicalAddr .Host , "-" , physicalAddr .Host , "-" , idx )
161- }
162-
163164func (p * connectionPool ) checkAndCleanIdleConnections (maxIdleTime time.Duration ) {
164165 if maxIdleTime < 0 {
165166 return
0 commit comments