Skip to content

Commit 9e88452

Browse files
authored
[4.4] Cache socket port number in bolt class (#726)
Backport of #724
1 parent 2630638 commit 9e88452

File tree

5 files changed

+4
-20
lines changed

5 files changed

+4
-20
lines changed

neo4j/io/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class Bolt(abc.ABC):
149149
def __init__(self, unresolved_address, sock, max_connection_lifetime, *, auth=None, user_agent=None, routing_context=None):
150150
self.unresolved_address = unresolved_address
151151
self.socket = sock
152+
self.local_port = self.socket.getsockname()[1]
152153
self.server_info = ServerInfo(Address(sock.getpeername()), self.PROTOCOL_VERSION)
153154
# so far `connection.recv_timeout_seconds` is the only available
154155
# configuration hint that exists. Therefore, all hints can be stored at
@@ -373,11 +374,6 @@ def encrypted(self):
373374
def der_encoded_server_certificate(self):
374375
pass
375376

376-
@property
377-
@abc.abstractmethod
378-
def local_port(self):
379-
pass
380-
381377
@abc.abstractmethod
382378
def hello(self):
383379
""" Appends a HELLO message to the outgoing queue, sends it and consumes

neo4j/io/_bolt3.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ def encrypted(self):
140140
def der_encoded_server_certificate(self):
141141
return self.socket.getpeercert(binary_form=True)
142142

143-
@property
144-
def local_port(self):
145-
try:
146-
return self.socket.getsockname()[1]
147-
except OSError:
148-
return 0
149-
150143
def get_base_headers(self):
151144
return {
152145
"user_agent": self.user_agent,

neo4j/io/_bolt4.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ def encrypted(self):
9898
def der_encoded_server_certificate(self):
9999
return self.socket.getpeercert(binary_form=True)
100100

101-
@property
102-
def local_port(self):
103-
try:
104-
return self.socket.getsockname()[1]
105-
except OSError:
106-
return 0
107-
108101
def get_base_headers(self):
109102
return {
110103
"user_agent": self.user_agent,

neo4j/io/_common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class MessageInbox:
4141

4242
def __init__(self, s, on_error):
4343
self.on_error = on_error
44+
self._local_port = s.getsockname()[1]
4445
self._messages = self._yield_messages(s)
4546

4647
def _yield_messages(self, sock):
@@ -55,7 +56,7 @@ def _yield_messages(self, sock):
5556
buffer.receive(sock, 2)
5657
chunk_size = buffer.pop_u16()
5758
if chunk_size == 0:
58-
log.debug("[#%04X] S: <NOOP>", sock.getsockname()[1])
59+
log.debug("[#%04X] S: <NOOP>", self._local_port)
5960

6061
buffer.receive(sock, chunk_size + 2)
6162
chunk_size = buffer.pop_u16()

tests/unit/work/_fake_connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
class FakeConnection(mock.NonCallableMagicMock):
3131
callbacks = []
3232
server_info = ServerInfo("127.0.0.1", (4, 3))
33+
local_port = 1234
3334

3435
def __init__(self, *args, **kwargs):
3536
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)