@@ -57,50 +57,41 @@ ShardsPool& ShardsPool::operator=(ShardsPool &&that) {
5757 return *this ;
5858}
5959
60- ConnectionPoolSPtr ShardsPool::fetch (const StringView &key) {
60+ GuardedConnection ShardsPool::fetch (const StringView &key) {
6161 auto slot = _slot (key);
6262
6363 return _fetch (slot);
6464}
6565
66- ConnectionPoolSPtr ShardsPool::fetch () {
66+ GuardedConnection ShardsPool::fetch () {
6767 auto slot = _slot ();
6868
6969 return _fetch (slot);
7070}
7171
72- ConnectionPoolSPtr ShardsPool::fetch (const Node &node) {
72+ GuardedConnection ShardsPool::fetch (const Node &node) {
7373 std::lock_guard<std::mutex> lock (_mutex);
7474
75- auto iter = _pool .find (node);
76- if (iter == _pool .end ()) {
75+ auto iter = _pools .find (node);
76+ if (iter == _pools .end ()) {
7777 // Node doesn't exist, and it should be a newly created node.
7878 // So add a new connection pool.
7979 iter = _add_node (node);
8080 }
8181
82- assert (iter != _pool .end ());
82+ assert (iter != _pools .end ());
8383
84- return iter->second ;
84+ return GuardedConnection ( iter->second ) ;
8585}
8686
8787void ShardsPool::update () {
8888 // My might send command to a removed node.
8989 // Try at most 3 times.
9090 for (auto idx = 0 ; idx < 3 ; ++idx) {
9191 try {
92- // Randomly pick a connection pool.
93- auto pool = fetch ();
94-
95- assert (bool (pool));
96-
97- auto connection = pool->fetch ();
98-
99- assert (!connection.broken ());
100-
101- ConnectionPoolGuard guard (*pool, connection);
102-
103- auto shards = _cluster_slots (connection);
92+ // Randomly pick a connection.
93+ auto guarded_connection = fetch ();
94+ auto shards = _cluster_slots (guarded_connection.connection ());
10495
10596 std::unordered_set<Node, NodeHash> nodes;
10697 for (const auto &shard : shards) {
@@ -114,10 +105,10 @@ void ShardsPool::update() {
114105 _shards = std::move (shards);
115106
116107 // Remove non-existent nodes.
117- for (auto iter = _pool .begin (); iter != _pool .end (); ) {
108+ for (auto iter = _pools .begin (); iter != _pools .end (); ) {
118109 if (nodes.find (iter->first ) == nodes.end ()) {
119110 // Node has been removed.
120- _pool .erase (iter++);
111+ _pools .erase (iter++);
121112 } else {
122113 ++iter;
123114 }
@@ -126,7 +117,7 @@ void ShardsPool::update() {
126117 // Add connection pool for new nodes.
127118 // In fact, connections will be created lazily.
128119 for (const auto &node : nodes) {
129- if (_pool .find (node) == _pool .end ()) {
120+ if (_pools .find (node) == _pools .end ()) {
130121 _add_node (node);
131122 }
132123 }
@@ -145,7 +136,7 @@ void ShardsPool::_move(ShardsPool &&that) {
145136 _pool_opts = that._pool_opts ;
146137 _connection_opts = that._connection_opts ;
147138 _shards = std::move (that._shards );
148- _pool = std::move (that._pool );
139+ _pools = std::move (that._pools );
149140}
150141
151142void ShardsPool::_init_pool (const Shards &shards) {
@@ -268,7 +259,7 @@ Slot ShardsPool::_slot() const {
268259 return uniform_dist (engine);
269260}
270261
271- ConnectionPoolSPtr ShardsPool::_fetch (Slot slot) {
262+ GuardedConnection ShardsPool::_fetch (Slot slot) {
272263 std::lock_guard<std::mutex> lock (_mutex);
273264
274265 auto shards_iter = _shards.lower_bound (SlotRange{slot, slot});
@@ -278,20 +269,20 @@ ConnectionPoolSPtr ShardsPool::_fetch(Slot slot) {
278269
279270 const auto &node = shards_iter->second ;
280271
281- auto node_iter = _pool .find (node);
282- if (node_iter == _pool .end ()) {
272+ auto node_iter = _pools .find (node);
273+ if (node_iter == _pools .end ()) {
283274 throw Error (" Slot is NOT covered: " + std::to_string (slot));
284275 }
285276
286- return node_iter->second ;
277+ return GuardedConnection ( node_iter->second ) ;
287278}
288279
289280auto ShardsPool::_add_node (const Node &node) -> NodeMap::iterator {
290281 auto opts = _connection_opts;
291282 opts.host = node.host ;
292283 opts.port = node.port ;
293284
294- return _pool .emplace (node, std::make_shared<ConnectionPool>(_pool_opts, opts)).first ;
285+ return _pools .emplace (node, std::make_shared<ConnectionPool>(_pool_opts, opts)).first ;
295286}
296287
297288}
0 commit comments