@@ -34,13 +34,13 @@ DECLARE_LOG_OBJECT()
3434namespace pulsar {
3535
3636ConnectionPool::ConnectionPool (const ClientConfiguration& conf, ExecutorServiceProviderPtr executorProvider,
37- const AuthenticationPtr& authentication, bool poolConnections,
38- const std::string& clientVersion)
37+ const AuthenticationPtr& authentication, const std::string& clientVersion)
3938 : clientConfiguration_ (conf),
4039 executorProvider_ (executorProvider),
4140 authentication_ (authentication),
42- poolConnections_ (poolConnections),
43- clientVersion_ (clientVersion) {}
41+ clientVersion_ (clientVersion),
42+ randomDistribution_ (0 , conf.getConnectionsPerBroker () - 1 ),
43+ randomEngine_ (std::chrono::high_resolution_clock::now ().time_since_epoch ().count ()) {}
4444
4545bool ConnectionPool::close () {
4646 bool expectedState = false ;
@@ -49,16 +49,15 @@ bool ConnectionPool::close() {
4949 }
5050
5151 std::unique_lock<std::recursive_mutex> lock (mutex_);
52- if (poolConnections_) {
53- for (auto cnxIt = pool_.begin (); cnxIt != pool_.end (); cnxIt++) {
54- auto & cnx = cnxIt->second ;
55- if (cnx) {
56- // The 2nd argument is false because removing a value during the iteration will cause segfault
57- cnx->close (ResultDisconnected, false );
58- }
52+
53+ for (auto cnxIt = pool_.begin (); cnxIt != pool_.end (); cnxIt++) {
54+ auto & cnx = cnxIt->second ;
55+ if (cnx) {
56+ // The 2nd argument is false because removing a value during the iteration will cause segfault
57+ cnx->close (ResultDisconnected, false );
5958 }
60- pool_.clear ();
6159 }
60+ pool_.clear ();
6261 return true ;
6362}
6463
@@ -72,22 +71,24 @@ Future<Result, ClientConnectionWeakPtr> ConnectionPool::getConnectionAsync(
7271
7372 std::unique_lock<std::recursive_mutex> lock (mutex_);
7473
75- if (poolConnections_) {
76- PoolMap::iterator cnxIt = pool_.find (logicalAddress);
77- if (cnxIt != pool_.end ()) {
78- auto & cnx = cnxIt->second ;
79-
80- if (!cnx->isClosed ()) {
81- // Found a valid or pending connection in the pool
82- LOG_DEBUG (" Got connection from pool for " << logicalAddress << " use_count: " //
83- << (cnx.use_count ()) << " @ " << cnx.get ());
84- return cnx->getConnectFuture ();
85- } else {
86- // The closed connection should have been removed from the pool in ClientConnection::close
87- LOG_WARN (" Deleting stale connection from pool for "
88- << logicalAddress << " use_count: " << (cnx.use_count ()) << " @ " << cnx.get ());
89- pool_.erase (logicalAddress);
90- }
74+ std::stringstream ss;
75+ ss << logicalAddress << ' -' << randomDistribution_ (randomEngine_);
76+ const std::string key = ss.str ();
77+
78+ PoolMap::iterator cnxIt = pool_.find (key);
79+ if (cnxIt != pool_.end ()) {
80+ auto & cnx = cnxIt->second ;
81+
82+ if (!cnx->isClosed ()) {
83+ // Found a valid or pending connection in the pool
84+ LOG_DEBUG (" Got connection from pool for " << key << " use_count: " //
85+ << (cnx.use_count ()) << " @ " << cnx.get ());
86+ return cnx->getConnectFuture ();
87+ } else {
88+ // The closed connection should have been removed from the pool in ClientConnection::close
89+ LOG_WARN (" Deleting stale connection from pool for " << key << " use_count: " << (cnx.use_count ())
90+ << " @ " << cnx.get ());
91+ pool_.erase (key);
9192 }
9293 }
9394
@@ -107,7 +108,7 @@ Future<Result, ClientConnectionWeakPtr> ConnectionPool::getConnectionAsync(
107108 LOG_INFO (" Created connection for " << logicalAddress);
108109
109110 Future<Result, ClientConnectionWeakPtr> future = cnx->getConnectFuture ();
110- pool_.insert (std::make_pair (logicalAddress , cnx));
111+ pool_.insert (std::make_pair (key , cnx));
111112
112113 lock.unlock ();
113114
0 commit comments