Skip to content

Make TCP socket options configurable (TCP_NODELAY etc) #1077

Description

@t-cadet

Hello, is it possible to add a way to configure TCP options on sockets?

My use case is to enable TCP_NODELAY and potentially other options like TCP_QUICKACK etc for performance reasons.

For now I patched the code in http_server.h:

diff --git a/include/crow/http_server.h b/include/crow/http_server.h
index d171632..4b3e06c 100644
--- a/include/crow/http_server.h
+++ b/include/crow/http_server.h
@@ -27,6 +27,19 @@
 #include "crow/logging.h"
 #include "crow/task_timer.h"

+#if defined(__linux__)
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+
+namespace crow
+{
+    inline void set_tcp_option(asio::ip::tcp::socket& sock, int optname) {
+        int flag = 1;
+        ::setsockopt(sock.native_handle(), IPPROTO_TCP, optname, &flag, sizeof(flag));
+    }
+}
+#endif
+

 namespace crow // NOTE: Already documented in "crow/app.h"
 {
@@ -245,6 +258,10 @@ namespace crow // NOTE: Already documented in "crow/app.h"
                   [this, p, &is, service_idx](error_code ec) {
                       if (!ec)
                       {
+                          #if defined(__linux__)
+                            set_tcp_option(p->socket(), TCP_NODELAY);
+                            // set_tcp_option(p->socket(), TCP_QUICKACK);
+                          #endif
                           is.post(
                             [p] {
                                 p->start();

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionThe viability / implementation of the issue is up for debate

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions