Skip to content

Add SO_KEEPALIVE support for connection monitoring#43

Open
claudiodube wants to merge 2 commits into
troglobit:masterfrom
claudiodube:add-keepalive-support
Open

Add SO_KEEPALIVE support for connection monitoring#43
claudiodube wants to merge 2 commits into
troglobit:masterfrom
claudiodube:add-keepalive-support

Conversation

@claudiodube

Copy link
Copy Markdown
  • Enable TCP keepalive on server sockets to detect broken connections
  • Configure keepalive parameters: idle=60s, interval=300s, count=9
  • Add debug logging for keepalive configuration
  • Follow existing setsockopt pattern after SO_LINGER configuration

Implementation details:

  • Added #include <netinet/tcp.h> for TCP socket options
  • Enable SO_KEEPALIVE on all server sockets in server_socket() function
  • Configure TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT parameters
  • Use same error handling pattern as existing socket options

Tested successfully:

  • Compilation: gcc builds without errors
  • Runtime: Debug log shows 'TCP keepalive enabled: idle=60s, interval=300s, count=9'
  • Functionality: curl test confirms connection redirection works properly
  • Compatibility: No breaking changes to existing functionality

Benefits:

  • Automatic detection of broken connections after 46 minutes
  • Prevents resource leaks from stale connections
  • Improves reliability in production environments
  • Better behavior with firewalls and NAT devices

- Enable TCP keepalive on server sockets to detect broken connections
- Configure keepalive parameters: idle=60s, interval=300s, count=9
- Add debug logging for keepalive configuration
- Follow existing setsockopt pattern after SO_LINGER configuration

Implementation details:
- Added #include <netinet/tcp.h> for TCP socket options
- Enable SO_KEEPALIVE on all server sockets in server_socket() function
- Configure TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT parameters
- Use same error handling pattern as existing socket options

Tested successfully:
- Compilation: gcc builds without errors
- Runtime: Debug log shows 'TCP keepalive enabled: idle=60s, interval=300s, count=9'
- Functionality: curl test confirms connection redirection works properly
- Compatibility: No breaking changes to existing functionality

Benefits:
- Automatic detection of broken connections after 46 minutes
- Prevents resource leaks from stale connections
- Improves reliability in production environments
- Better behavior with firewalls and NAT devices
- Remove hardcoded TCP keepalive parameters (idle, interval, count)
- Use system-wide sysctl settings instead of hardcoded values
- Respects administrator's network configuration via:
  * net.ipv4.tcp_keepalive_time (currently 60s)
  * net.ipv4.tcp_keepalive_intvl (currently 300s)
  * net.ipv4.tcp_keepalive_probes (currently 9)
- Follows Unix philosophy: one configuration location
- More flexible and maintainable approach
- Cleaner code with fewer magic numbers

Tested: Debug log confirms 'TCP keepalive enabled using system defaults'
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.

1 participant