@@ -466,6 +466,7 @@ def __init__(
466
466
read_from_replicas : bool = False ,
467
467
dynamic_startup_nodes : bool = True ,
468
468
url : Optional [str ] = None ,
469
+ host_port_remap : Optional [Callable [[str , int ], Tuple [str , int ]]] = None ,
469
470
** kwargs ,
470
471
):
471
472
"""
@@ -594,6 +595,7 @@ def __init__(
594
595
from_url = from_url ,
595
596
require_full_coverage = require_full_coverage ,
596
597
dynamic_startup_nodes = dynamic_startup_nodes ,
598
+ host_port_remap = host_port_remap ,
597
599
** kwargs ,
598
600
)
599
601
@@ -1269,6 +1271,7 @@ def __init__(
1269
1271
lock = None ,
1270
1272
dynamic_startup_nodes = True ,
1271
1273
connection_pool_class = ConnectionPool ,
1274
+ host_port_remap : Optional [Callable [[str , int ], Tuple [str , int ]]] = None ,
1272
1275
** kwargs ,
1273
1276
):
1274
1277
self .nodes_cache = {}
@@ -1280,6 +1283,7 @@ def __init__(
1280
1283
self ._require_full_coverage = require_full_coverage
1281
1284
self ._dynamic_startup_nodes = dynamic_startup_nodes
1282
1285
self .connection_pool_class = connection_pool_class
1286
+ self .host_port_remap = host_port_remap
1283
1287
self ._moved_exception = None
1284
1288
self .connection_kwargs = kwargs
1285
1289
self .read_load_balancer = LoadBalancer ()
@@ -1502,6 +1506,7 @@ def initialize(self):
1502
1506
if host == "" :
1503
1507
host = startup_node .host
1504
1508
port = int (primary_node [1 ])
1509
+ host , port = self .remap_host_port (host , port )
1505
1510
1506
1511
target_node = self ._get_or_create_cluster_node (
1507
1512
host , port , PRIMARY , tmp_nodes_cache
@@ -1518,6 +1523,7 @@ def initialize(self):
1518
1523
for replica_node in replica_nodes :
1519
1524
host = str_if_bytes (replica_node [0 ])
1520
1525
port = replica_node [1 ]
1526
+ host , port = self .remap_host_port (host , port )
1521
1527
1522
1528
target_replica_node = self ._get_or_create_cluster_node (
1523
1529
host , port , REPLICA , tmp_nodes_cache
@@ -1591,6 +1597,16 @@ def reset(self):
1591
1597
# The read_load_balancer is None, do nothing
1592
1598
pass
1593
1599
1600
+ def remap_host_port (self , host : str , port : int ) -> Tuple [str , int ]:
1601
+ """
1602
+ Remap the host and port returned from the cluster to a different
1603
+ internal value. Useful if the client is not connecting directly
1604
+ to the cluster.
1605
+ """
1606
+ if self .host_port_remap :
1607
+ return self .host_port_remap (host , port )
1608
+ return host , port
1609
+
1594
1610
1595
1611
class ClusterPubSub (PubSub ):
1596
1612
"""
0 commit comments