@@ -152,7 +152,6 @@ def getnext(self):
152
152
self .pos = (self .pos + 1 ) % len (self .addrs )
153
153
return self .addrs [self .pos ]
154
154
155
-
156
155
class MeshConnection (Connection ):
157
156
'''
158
157
Represents a connection to a cluster of Tarantool servers.
@@ -254,7 +253,8 @@ def __init__(self, host=None, port=None,
254
253
self .last_nodes_refresh = 0
255
254
256
255
self .pool = {}
257
- self .ro = {}
256
+ self .pool_ro = {}
257
+ self .pool_rw = {}
258
258
259
259
super (MeshConnection , self ).__init__ (
260
260
host = host ,
@@ -305,7 +305,37 @@ def connect(self):
305
305
continue
306
306
307
307
# box.cfg was called at least once since we can connect.
308
- self .ro [key ] = conn .eval ("return box.info.ro" )
308
+ if conn .eval ("return box.info.ro" ):
309
+ self .pool_ro [key ] = conn
310
+ else :
311
+ self .pool_rw [key ] = conn
312
+
313
+ def _get_connection (self , mode ):
314
+ if mode == Mode .RW :
315
+ if len (self .pool_rw ) > 0 :
316
+ return next (iter (self .pool_rw ))[1 ]
317
+ else :
318
+ warn ("can't find rw instance in pool, route request to default connection" , ClusterDiscoveryWarning )
319
+ return self
320
+ elif mode == Mode .PREFER_RO :
321
+ if len (self .pool_ro ) > 0 :
322
+ return next (iter (self .pool_ro ))[1 ]
323
+ elif len (self .pool_rw ) > 0 :
324
+ return next (iter (self .pool_rw ))[1 ]
325
+ else :
326
+ warn ("can't find rw/ro instance in pool, route request to default connection" , ClusterDiscoveryWarning )
327
+ return self
328
+ elif mode == Mode .PREFER_RW :
329
+ if len (self .pool_rw ) > 0 :
330
+ return next (iter (self .pool_rw ))[1 ]
331
+ elif len (self .pool_ro ) > 0 :
332
+ return next (iter (self .pool_ro ))[1 ]
333
+ else :
334
+ warn ("can't find rw/ro instance in pool, route request to default connection" , ClusterDiscoveryWarning )
335
+ return self
336
+ else :
337
+ # warn("mode is not set", ClusterDiscoveryWarning)
338
+ return self
309
339
310
340
def _opt_reconnect (self ):
311
341
'''
@@ -403,8 +433,9 @@ def _send_request(self, request):
403
433
404
434
:rtype: `Response` instance
405
435
'''
406
- self ._opt_refresh_instances ()
407
- return super (MeshConnection , self )._send_request (request )
436
+ conn = self ._get_connection (request .mode )
437
+ conn ._opt_refresh_instances ()
438
+ return super (MeshConnection , conn )._send_request (request )
408
439
409
440
def _build_insert_request (self , space_name , values ):
410
441
request = super (MeshConnection , self )._build_insert_request (space_name , values )
0 commit comments