about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-11-11 03:38:47 +0000
committerEric Wong <e@80x24.org>2015-11-11 03:45:12 +0000
commit42a65a32623158c5bdce234b1b431b9f5093da70 (patch)
tree4e80daa63911ae4c8e082fd496ff805212712630
parent0312c1e6220ef4280268a0f48f24db90738037bd (diff)
downloadcmogstored-42a65a32623158c5bdce234b1b431b9f5093da70.tar.gz
systemd users may not set the correct TCP socket options for
us, so be sure to set TCP_NODELAY, SO_KEEPALIVE, and use
a sufficiently large listen backlog to avoid hurting performance
for users who bind sockets outside of cmogstored.
-rw-r--r--bind_listen.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/bind_listen.c b/bind_listen.c
index 5535b92..c705576 100644
--- a/bind_listen.c
+++ b/bind_listen.c
@@ -15,15 +15,17 @@
  *   http://labs.apnic.net/blabs/?p=57
  */
 
-static int set_tcp_opts(int fd)
+static int set_tcp_opts(int fd, bool inherited)
 {
         int val;
         socklen_t len = sizeof(int);
         int rc;
 
-        val = 1;
-        rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, len);
-        if (rc < 0) return rc;
+        if (!inherited) {
+                val = 1;
+                rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, len);
+                if (rc < 0) return rc;
+        }
 
         val = 1;
         rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, len);
@@ -40,8 +42,11 @@ int mog_bind_listen(struct addrinfo *r)
 {
         /* see if we inherited the socket, first */
         int fd = mog_inherit_get(r->ai_addr, r->ai_addrlen);
+        const int backlog = 1024;
 
-        if (fd >= 0)
+        if (fd >= 0 &&
+            set_tcp_opts(fd, true) == 0 &&
+            listen(fd, backlog) == 0)
                 return fd;
 
         for (; r; r = r->ai_next) {
@@ -56,9 +61,9 @@ int mog_bind_listen(struct addrinfo *r)
                  * everywhere yet (in 2012).
                  */
                 if (mog_set_cloexec(fd, true) == 0 &&
-                    set_tcp_opts(fd) == 0 &&
+                    set_tcp_opts(fd, false) == 0 &&
                     bind(fd, r->ai_addr, r->ai_addrlen) == 0 &&
-                    listen(fd, 1024) == 0)
+                    listen(fd, backlog) == 0)
                         break;
 
                 PRESERVE_ERRNO( mog_close(fd) );