Skip to content

Commit

Permalink
Cache socket port number in bolt class (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdedude authored Jun 1, 2022
1 parent 3d7826f commit d3a1f46
Show file tree
Hide file tree
Showing 12 changed files with 8 additions and 54 deletions.
6 changes: 1 addition & 5 deletions neo4j/_async/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ 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]
self.server_info = ServerInfo(Address(sock.getpeername()),
self.PROTOCOL_VERSION)
# so far `connection.recv_timeout_seconds` is the only available
Expand Down Expand Up @@ -379,11 +380,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
Expand Down
7 changes: 0 additions & 7 deletions neo4j/_async/io/_bolt3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions neo4j/_async/io/_bolt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions neo4j/_async/io/_bolt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion neo4j/_async/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AsyncMessageInbox:

def __init__(self, s, on_error):
self.on_error = on_error
self._local_port = s.getsockname()[1]
self._messages = self._yield_messages(s)

async def _yield_messages(self, sock):
Expand All @@ -56,7 +57,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: <NOOP>", sock.getsockname()[1])
log.debug("[#%04X] S: <NOOP>", self._local_port)

await receive_into_buffer(sock, buffer, chunk_size + 2)
chunk_size = buffer.pop_u16()
Expand Down
6 changes: 1 addition & 5 deletions neo4j/_sync/io/_bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ 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]
self.server_info = ServerInfo(Address(sock.getpeername()),
self.PROTOCOL_VERSION)
# so far `connection.recv_timeout_seconds` is the only available
Expand Down Expand Up @@ -379,11 +380,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
Expand Down
7 changes: 0 additions & 7 deletions neo4j/_sync/io/_bolt3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions neo4j/_sync/io/_bolt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions neo4j/_sync/io/_bolt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion neo4j/_sync/io/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class MessageInbox:

def __init__(self, s, on_error):
self.on_error = on_error
self._local_port = s.getsockname()[1]
self._messages = self._yield_messages(s)

def _yield_messages(self, sock):
Expand All @@ -56,7 +57,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: <NOOP>", sock.getsockname()[1])
log.debug("[#%04X] S: <NOOP>", self._local_port)

receive_into_buffer(sock, buffer, chunk_size + 2)
chunk_size = buffer.pop_u16()
Expand Down
1 change: 1 addition & 0 deletions tests/unit/async_/work/_fake_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/unit/sync/work/_fake_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d3a1f46

Please sign in to comment.