From 5e517dbfd6970846ec43d335cb91317cf44f09d8 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Tue, 17 May 2022 09:49:47 +0200 Subject: [PATCH 1/3] Cache socket port number in bolt class --- neo4j/_async/io/_bolt.py | 7 ++----- neo4j/_async/io/_bolt3.py | 7 ------- neo4j/_async/io/_bolt4.py | 7 ------- neo4j/_async/io/_bolt5.py | 7 ------- neo4j/_async/io/_common.py | 4 +++- neo4j/_sync/io/_bolt.py | 7 ++----- neo4j/_sync/io/_bolt3.py | 7 ------- neo4j/_sync/io/_bolt4.py | 7 ------- neo4j/_sync/io/_bolt5.py | 7 ------- neo4j/_sync/io/_common.py | 4 +++- 10 files changed, 10 insertions(+), 54 deletions(-) diff --git a/neo4j/_async/io/_bolt.py b/neo4j/_async/io/_bolt.py index 42e5c810..bbe72d7f 100644 --- a/neo4j/_async/io/_bolt.py +++ b/neo4j/_async/io/_bolt.py @@ -100,6 +100,8 @@ def __init__(self, unresolved_address, sock, max_connection_lifetime, *, auth=None, user_agent=None, routing_context=None): self.unresolved_address = unresolved_address self.socket = sock + self.local_port = self.socket.getsockname()[1] + assert self.local_port # TODO: remove, just for testing self.server_info = ServerInfo(Address(sock.getpeername()), self.PROTOCOL_VERSION) # so far `connection.recv_timeout_seconds` is the only available @@ -379,11 +381,6 @@ def encrypted(self): def der_encoded_server_certificate(self): pass - @property - @abc.abstractmethod - def local_port(self): - pass - @abc.abstractmethod async def hello(self): """ Appends a HELLO message to the outgoing queue, sends it and consumes diff --git a/neo4j/_async/io/_bolt3.py b/neo4j/_async/io/_bolt3.py index 0a8dcd01..b361e1f5 100644 --- a/neo4j/_async/io/_bolt3.py +++ b/neo4j/_async/io/_bolt3.py @@ -137,13 +137,6 @@ def encrypted(self): def der_encoded_server_certificate(self): return self.socket.getpeercert(binary_form=True) - @property - def local_port(self): - try: - return self.socket.getsockname()[1] - except OSError: - return 0 - def get_base_headers(self): return { "user_agent": self.user_agent, diff --git a/neo4j/_async/io/_bolt4.py b/neo4j/_async/io/_bolt4.py index 26c5add3..d1fbf035 100644 --- a/neo4j/_async/io/_bolt4.py +++ b/neo4j/_async/io/_bolt4.py @@ -90,13 +90,6 @@ def encrypted(self): def der_encoded_server_certificate(self): return self.socket.getpeercert(binary_form=True) - @property - def local_port(self): - try: - return self.socket.getsockname()[1] - except OSError: - return 0 - def get_base_headers(self): return { "user_agent": self.user_agent, diff --git a/neo4j/_async/io/_bolt5.py b/neo4j/_async/io/_bolt5.py index a00b57d8..99901ff3 100644 --- a/neo4j/_async/io/_bolt5.py +++ b/neo4j/_async/io/_bolt5.py @@ -89,13 +89,6 @@ def encrypted(self): def der_encoded_server_certificate(self): return self.socket.getpeercert(binary_form=True) - @property - def local_port(self): - try: - return self.socket.getsockname()[1] - except OSError: - return 0 - def get_base_headers(self): headers = {"user_agent": self.user_agent} if self.routing_context is not None: diff --git a/neo4j/_async/io/_common.py b/neo4j/_async/io/_common.py index 4c1e6e7b..bae9192d 100644 --- a/neo4j/_async/io/_common.py +++ b/neo4j/_async/io/_common.py @@ -42,6 +42,8 @@ class AsyncMessageInbox: def __init__(self, s, on_error): self.on_error = on_error + self._local_port = s.getsockname()[1] + assert self._local_port # TODO: remove, just for testing self._messages = self._yield_messages(s) async def _yield_messages(self, sock): @@ -56,7 +58,7 @@ async def _yield_messages(self, sock): await receive_into_buffer(sock, buffer, 2) chunk_size = buffer.pop_u16() if chunk_size == 0: - log.debug("[#%04X] S: ", sock.getsockname()[1]) + log.debug("[#%04X] S: ", self._local_port) await receive_into_buffer(sock, buffer, chunk_size + 2) chunk_size = buffer.pop_u16() diff --git a/neo4j/_sync/io/_bolt.py b/neo4j/_sync/io/_bolt.py index a7bda8c8..acd8bdc6 100644 --- a/neo4j/_sync/io/_bolt.py +++ b/neo4j/_sync/io/_bolt.py @@ -100,6 +100,8 @@ def __init__(self, unresolved_address, sock, max_connection_lifetime, *, auth=None, user_agent=None, routing_context=None): self.unresolved_address = unresolved_address self.socket = sock + self.local_port = self.socket.getsockname()[1] + assert self.local_port # TODO: remove, just for testing self.server_info = ServerInfo(Address(sock.getpeername()), self.PROTOCOL_VERSION) # so far `connection.recv_timeout_seconds` is the only available @@ -379,11 +381,6 @@ def encrypted(self): def der_encoded_server_certificate(self): pass - @property - @abc.abstractmethod - def local_port(self): - pass - @abc.abstractmethod def hello(self): """ Appends a HELLO message to the outgoing queue, sends it and consumes diff --git a/neo4j/_sync/io/_bolt3.py b/neo4j/_sync/io/_bolt3.py index 0265a7d9..ac6e61fb 100644 --- a/neo4j/_sync/io/_bolt3.py +++ b/neo4j/_sync/io/_bolt3.py @@ -137,13 +137,6 @@ def encrypted(self): def der_encoded_server_certificate(self): return self.socket.getpeercert(binary_form=True) - @property - def local_port(self): - try: - return self.socket.getsockname()[1] - except OSError: - return 0 - def get_base_headers(self): return { "user_agent": self.user_agent, diff --git a/neo4j/_sync/io/_bolt4.py b/neo4j/_sync/io/_bolt4.py index 8ddefc2a..2c26af8c 100644 --- a/neo4j/_sync/io/_bolt4.py +++ b/neo4j/_sync/io/_bolt4.py @@ -90,13 +90,6 @@ def encrypted(self): def der_encoded_server_certificate(self): return self.socket.getpeercert(binary_form=True) - @property - def local_port(self): - try: - return self.socket.getsockname()[1] - except OSError: - return 0 - def get_base_headers(self): return { "user_agent": self.user_agent, diff --git a/neo4j/_sync/io/_bolt5.py b/neo4j/_sync/io/_bolt5.py index f148395f..74fe2d18 100644 --- a/neo4j/_sync/io/_bolt5.py +++ b/neo4j/_sync/io/_bolt5.py @@ -89,13 +89,6 @@ def encrypted(self): def der_encoded_server_certificate(self): return self.socket.getpeercert(binary_form=True) - @property - def local_port(self): - try: - return self.socket.getsockname()[1] - except OSError: - return 0 - def get_base_headers(self): headers = {"user_agent": self.user_agent} if self.routing_context is not None: diff --git a/neo4j/_sync/io/_common.py b/neo4j/_sync/io/_common.py index 1b7e5a8d..8b103d58 100644 --- a/neo4j/_sync/io/_common.py +++ b/neo4j/_sync/io/_common.py @@ -42,6 +42,8 @@ class MessageInbox: def __init__(self, s, on_error): self.on_error = on_error + self._local_port = s.getsockname()[1] + assert self._local_port # TODO: remove, just for testing self._messages = self._yield_messages(s) def _yield_messages(self, sock): @@ -56,7 +58,7 @@ def _yield_messages(self, sock): receive_into_buffer(sock, buffer, 2) chunk_size = buffer.pop_u16() if chunk_size == 0: - log.debug("[#%04X] S: ", sock.getsockname()[1]) + log.debug("[#%04X] S: ", self._local_port) receive_into_buffer(sock, buffer, chunk_size + 2) chunk_size = buffer.pop_u16() From d374d0872c9bc64ec61a554ecbcfb5e4cdf53b53 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Tue, 17 May 2022 11:34:12 +0200 Subject: [PATCH 2/3] Adjust mock for unit tests --- tests/unit/async_/work/_fake_connection.py | 1 + tests/unit/sync/work/_fake_connection.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/unit/async_/work/_fake_connection.py b/tests/unit/async_/work/_fake_connection.py index 9cc44404..9aa2071c 100644 --- a/tests/unit/async_/work/_fake_connection.py +++ b/tests/unit/async_/work/_fake_connection.py @@ -31,6 +31,7 @@ def async_fake_connection_generator(session_mocker): class AsyncFakeConnection(mock.NonCallableMagicMock): callbacks = [] server_info = ServerInfo("127.0.0.1", (4, 3)) + local_port = 1234 def __init__(self, *args, **kwargs): kwargs["spec"] = AsyncBolt diff --git a/tests/unit/sync/work/_fake_connection.py b/tests/unit/sync/work/_fake_connection.py index 0d64b815..e6cb750e 100644 --- a/tests/unit/sync/work/_fake_connection.py +++ b/tests/unit/sync/work/_fake_connection.py @@ -31,6 +31,7 @@ def fake_connection_generator(session_mocker): class FakeConnection(mock.NonCallableMagicMock): callbacks = [] server_info = ServerInfo("127.0.0.1", (4, 3)) + local_port = 1234 def __init__(self, *args, **kwargs): kwargs["spec"] = Bolt From 936b403091197bb3f893516c0ecb4e3604d19146 Mon Sep 17 00:00:00 2001 From: Rouven Bauer Date: Tue, 24 May 2022 11:27:01 +0200 Subject: [PATCH 3/3] Remove debug asserts --- neo4j/_async/io/_bolt.py | 1 - neo4j/_async/io/_common.py | 1 - neo4j/_sync/io/_bolt.py | 1 - neo4j/_sync/io/_common.py | 1 - 4 files changed, 4 deletions(-) diff --git a/neo4j/_async/io/_bolt.py b/neo4j/_async/io/_bolt.py index bbe72d7f..90b93612 100644 --- a/neo4j/_async/io/_bolt.py +++ b/neo4j/_async/io/_bolt.py @@ -101,7 +101,6 @@ def __init__(self, unresolved_address, sock, max_connection_lifetime, *, self.unresolved_address = unresolved_address self.socket = sock self.local_port = self.socket.getsockname()[1] - assert self.local_port # TODO: remove, just for testing self.server_info = ServerInfo(Address(sock.getpeername()), self.PROTOCOL_VERSION) # so far `connection.recv_timeout_seconds` is the only available diff --git a/neo4j/_async/io/_common.py b/neo4j/_async/io/_common.py index bae9192d..7291cb27 100644 --- a/neo4j/_async/io/_common.py +++ b/neo4j/_async/io/_common.py @@ -43,7 +43,6 @@ class AsyncMessageInbox: def __init__(self, s, on_error): self.on_error = on_error self._local_port = s.getsockname()[1] - assert self._local_port # TODO: remove, just for testing self._messages = self._yield_messages(s) async def _yield_messages(self, sock): diff --git a/neo4j/_sync/io/_bolt.py b/neo4j/_sync/io/_bolt.py index acd8bdc6..a558d5c8 100644 --- a/neo4j/_sync/io/_bolt.py +++ b/neo4j/_sync/io/_bolt.py @@ -101,7 +101,6 @@ def __init__(self, unresolved_address, sock, max_connection_lifetime, *, self.unresolved_address = unresolved_address self.socket = sock self.local_port = self.socket.getsockname()[1] - assert self.local_port # TODO: remove, just for testing self.server_info = ServerInfo(Address(sock.getpeername()), self.PROTOCOL_VERSION) # so far `connection.recv_timeout_seconds` is the only available diff --git a/neo4j/_sync/io/_common.py b/neo4j/_sync/io/_common.py index 8b103d58..4693d8e7 100644 --- a/neo4j/_sync/io/_common.py +++ b/neo4j/_sync/io/_common.py @@ -43,7 +43,6 @@ class MessageInbox: def __init__(self, s, on_error): self.on_error = on_error self._local_port = s.getsockname()[1] - assert self._local_port # TODO: remove, just for testing self._messages = self._yield_messages(s) def _yield_messages(self, sock):