Skip to content

Feature/issue #1077 Add TCP Socket Options for HTTP and WebSocket Connections - #1202

Open
jwolak wants to merge 15 commits into
CrowCpp:masterfrom
jwolak:feature/issue-1077-configurable-tcp-socket-options
Open

Feature/issue #1077 Add TCP Socket Options for HTTP and WebSocket Connections#1202
jwolak wants to merge 15 commits into
CrowCpp:masterfrom
jwolak:feature/issue-1077-configurable-tcp-socket-options

Conversation

@jwolak

@jwolak jwolak commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

This PR implements independent TCP socket option configuration for HTTP and WebSocket servers, resolving the requirement to differentiate between socket options for different server types.

HTTP and WebSocket connections have fundamentally different traffic patterns:

  • HTTP: Request-response pattern → benefits from TCP_NODELAY enabled (low latency)
  • WebSocket: Streaming pattern → benefits from TCP_NODELAY disabled (higher throughput via packet batching)

Previously, both protocols shared the same socket options. This implementation allows independent configuration while maintaining a clean, symmetric API.

Changes

  • include/crow/tcp_socket_options.h
    • struct tcp_socket_options - container for TCP options
    • apply_tcp_socket_options() - applies options to TCP sockets with error handling
    • Generic template specialization for non-TCP sockets (no-op)
    • Supports both Boost.ASIO and standalone ASIO

API Extensions

  • include/crow/app.h
    • tcp_nodelay(bool enabled = true) - configure HTTP TCP_NODELAY
    • tcp_socket_options() - retrieve HTTP TCP settings
    • websocket_tcp_nodelay(bool enabled = true) - configure WebSocket TCP_NODELAY
    • websocket_tcp_socket_options() - retrieve WebSocket TCP settings
    • Private fields: tcp_socket_options_ and websocket_tcp_socket_options_

Integration

  • include/crow/websocket.h

    • Added tcp_options parameter to Connection::create()
    • Options applied immediately after connection creation, before handshake
  • include/crow/routing.h

    • All WebSocket upgrade handlers pass websocket_tcp_socket_options() to connection factory
    • Supports SocketAdaptor, UnixSocketAdaptor, and SSLAdaptor

Test Coverage

  • tests/unittest.cpp
    • 2 low-level infrastructure tests (enable/disable transitions)
    • 6 API tests (HTTP and WebSocket defaults, enable, disable)
    • 2 integration/smoke tests (HTTP server and WebSocket handshake)

Test Cases

  • TCP_NODELAY_socket_option_apply_enable - Low-level: enable transition
  • TCP_NODELAY_socket_option_apply_disable - Low-level: disable transition
  • TCP_NODELAY_http_api_defaults - HTTP defaults to disabled
  • TCP_NODELAY_http_api_enabled - HTTP can be enabled
  • TCP_NODELAY_http_api_disabled - HTTP can be disabled
  • TCP_NODELAY_websocket_api_defaults - WebSocket defaults to disabled
  • TCP_NODELAY_websocket_api_enabled - WebSocket can be enabled
  • TCP_NODELAY_websocket_api_disabled - WebSocket can be disabled
  • TCP_NODELAY_http_smoke_test - HTTP server integration test
  • TCP_NODELAY_websocket_smoke_test - WebSocket handshake integration test

Compatibility / Impact
None. This is a purely additive feature with sensible defaults (TCP_NODELAY disabled for both).

Comment thread tests/unittest.cpp
Comment thread tests/unittest.cpp
Comment thread tests/unittest.cpp
Comment thread tests/unittest.cpp

@gittiver gittiver left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

@gittiver gittiver linked an issue Jul 23, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make TCP socket options configurable (TCP_NODELAY etc)

2 participants