Skip to content

Commit 87677b0

Browse files
author
Zhen
committed
Added RoutingSession to call get routing table
1 parent fe7554e commit 87677b0

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

neo4j/v1/routing.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,27 @@ def update(self, new_routing_table):
150150
self.ttl = new_routing_table.ttl
151151

152152

153-
class RoutingConnectionPool(ConnectionPool):
154-
""" Connection pool with routing table.
155-
"""
153+
class RoutingSession(BoltSession):
156154

157155
call_get_servers = "CALL dbms.cluster.routing.getServers"
158156
get_routing_table_param = "context"
159157
call_get_routing_table = "CALL dbms.cluster.routing.getRoutingTable({%s})" % get_routing_table_param
160158

159+
def routing_info_procedure(self, routing_context):
160+
if ServerVersion.from_str(self._connection.server.version).at_least_version(3, 2):
161+
return self.call_get_routing_table, {self.get_routing_table_param: routing_context}
162+
else:
163+
return self.call_get_servers, {}
164+
165+
def __run__(self, statement, routing_context):
166+
# the statement is ignored as it will be get routing table procedure call.
167+
return self._run(*self.routing_info_procedure(routing_context))
168+
169+
170+
class RoutingConnectionPool(ConnectionPool):
171+
""" Connection pool with routing table.
172+
"""
173+
161174
def __init__(self, connector, initial_address, routing_context, *routers):
162175
super(RoutingConnectionPool, self).__init__(connector)
163176
self.initial_address = initial_address
@@ -166,12 +179,6 @@ def __init__(self, connector, initial_address, routing_context, *routers):
166179
self.missing_writer = False
167180
self.refresh_lock = Lock()
168181

169-
def routing_info_procedure(self, connection):
170-
if ServerVersion.from_str(connection.server.version).at_least_version(3, 2):
171-
return self.call_get_routing_table, {self.get_routing_table_param: self.routing_context}
172-
else:
173-
return self.call_get_servers, {}
174-
175182
def fetch_routing_info(self, address):
176183
""" Fetch raw routing info from a given router address.
177184
@@ -182,15 +189,8 @@ def fetch_routing_info(self, address):
182189
if routing support is broken
183190
"""
184191
try:
185-
connections = [None]
186-
187-
def connector(_):
188-
connection = self.acquire_direct(address)
189-
connections[0] = connection
190-
return connection
191-
192-
with BoltSession(lambda _: connector) as session:
193-
return list(session.run(*self.routing_info_procedure(connections[0])))
192+
with RoutingSession(lambda _: self.acquire_direct(address)) as session:
193+
return list(session.run("ignored", self.routing_context))
194194
except CypherError as error:
195195
if error.code == "Neo.ClientError.Procedure.ProcedureNotFound":
196196
raise ServiceUnavailable("Server {!r} does not support routing".format(address))

neo4j/v1/session.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BoltSession(Session):
3434
:param bookmark:
3535
"""
3636

37-
def __run__(self, statement, parameters):
37+
def _run(self, statement, parameters):
3838
assert isinstance(statement, unicode)
3939
assert isinstance(parameters, dict)
4040

@@ -52,6 +52,9 @@ def __run__(self, statement, parameters):
5252

5353
return result
5454

55+
def __run__(self, statement, parameters):
56+
return self._run(statement, parameters)
57+
5558
def __begin__(self):
5659
return self.__run__(u"BEGIN", {"bookmark": self._bookmark} if self._bookmark else {})
5760

0 commit comments

Comments
 (0)