Skip to content

Commit 6551e92

Browse files
authored
Set TCP_NODELAY (#1122)
This yields a small performance improvement by disabling Nagle's algorithm on the TCP sockets. The change only affects the synchronous driver as most async runtime implementations already set this socket option for us.
1 parent 26980e1 commit 6551e92

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/neo4j/_async_compat/network/_bolt_socket.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import annotations
1818

1919
import asyncio
20+
import errno
2021
import logging
2122
import struct
2223
import typing as t
@@ -29,10 +30,12 @@
2930
from socket import (
3031
AF_INET,
3132
AF_INET6,
33+
IPPROTO_TCP,
3234
SHUT_RDWR,
3335
SO_KEEPALIVE,
3436
socket,
3537
SOL_SOCKET,
38+
TCP_NODELAY,
3639
timeout as SocketTimeout, # noqa: N812 (it is a class)
3740
)
3841
# isort: on
@@ -548,6 +551,12 @@ def _connect(cls, resolved_address, timeout, keep_alive):
548551
s = socket(AF_INET6)
549552
else:
550553
raise ValueError(f"Unsupported address {resolved_address!r}")
554+
try:
555+
s.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
556+
except OSError as e:
557+
# option might not be supported on all platforms
558+
if e.errno != errno.ENOPROTOOPT:
559+
raise
551560
t = s.gettimeout()
552561
if timeout:
553562
s.settimeout(timeout)

0 commit comments

Comments
 (0)