--- /dev/null
+This is a little helper for setting TCP_NODELAY on all sockets that a program
+creates, thereby reducing round-trip times.
+Especially for loopback sockets, this gets rid of nasty 40ms delays created
+by the nagle algorithm.
--- /dev/null
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <netinet/ip.h>
+#include <netinet/tcp.h>
+#include <dlfcn.h>
+#include <string.h>
+
+int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
+ static int (*real_connect)(int, const struct sockaddr *, socklen_t);
+ if (!real_connect) {
+ real_connect = dlsym(RTLD_NEXT, "connect");
+ }
+ int res = real_connect(sockfd, addr, addrlen);
+ if (res != 0) return res;
+
+ static const int one = 1;
+ setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
+ return 0;
+}
+
+int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
+ static int (*real_accept)(int, struct sockaddr *, socklen_t *);
+ if (!real_accept) {
+ real_accept = dlsym(RTLD_NEXT, "accept");
+ }
+ int res = real_accept(sockfd, addr, addrlen);
+ if (res == -1) return res;
+
+ static const int one = 1;
+ setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
+
+ return res;
+}