Skip to content

Commit c24d013

Browse files
committed
gh-144254: set TCP_NODELAY by default (disable nagle algorithm)
1 parent 8f45925 commit c24d013

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Modules/socketmodule.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,6 +3057,18 @@ sock_accept_impl(PySocketSockObject *s, void *data)
30573057
ctx->result = accept(get_sock_fd(s), addr, paddrlen);
30583058
#endif
30593059

3060+
#ifdef TCP_NODELAY
3061+
/* Enable TCP_NODELAY by default for TCP sockets */
3062+
if (ctx->result >= 0 || (INVALID_SOCKET != (SOCKET_T)-1 && ctx->result != INVALID_SOCKET)) {
3063+
if (s->sock_family == AF_INET || s->sock_family == AF_INET6) {
3064+
if (s->sock_type == SOCK_STREAM) {
3065+
int flag = 1;
3066+
setsockopt(ctx->result, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
3067+
}
3068+
}
3069+
}
3070+
#endif
3071+
30603072
#ifdef MS_WINDOWS
30613073
return (ctx->result != INVALID_SOCKET);
30623074
#else
@@ -5821,6 +5833,17 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
58215833
}
58225834
#endif
58235835
}
5836+
5837+
#ifdef TCP_NODELAY
5838+
/* Enable TCP_NODELAY by default for TCP sockets */
5839+
if (family == AF_INET || family == AF_INET6) {
5840+
if (type == SOCK_STREAM) {
5841+
int flag = 1;
5842+
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
5843+
}
5844+
}
5845+
#endif
5846+
58245847
if (init_sockobject(state, self, fd, family, type, proto) == -1) {
58255848
SOCKETCLOSE(fd);
58265849
return -1;

0 commit comments

Comments
 (0)