feat: enable TCP keepalive and nodelay on all connections (DRIVERS-383)#276
Merged
Conversation
Add {keepalive, true} and {nodelay, true} socket options to both
gen_tcp:connect and ssl:connect calls in mc_worker_logic:do_connect/5.
This brings the Erlang driver into compliance with MongoDB specification
DRIVERS-383, which requires all drivers to enable TCP keepalive by
default. Keepalive prevents firewalls and load balancers from silently
closing idle connections, reducing connection churn in production.
For SSL connections, user-provided ssl_opts are appended after the
defaults so they can override keepalive/nodelay if needed.
Includes property-based tests (PropEr) and unit tests verifying:
- keepalive and nodelay enabled on all plain TCP connections
- existing socket options (binary, active, packet) preserved
- SSL user option override precedence
- ping command works on keepalive-enabled connections
Contributor
Author
|
I've been noticing a lot of silently closed connections on long-running operations after upgrading my production nodes from MongoDB 5 to MongoDB 6. My hope is that by making the driver compliant with TCP keep alive and Mongo's own specifications that this will resolve those errors. |
Contributor
Author
|
@comtihon Let me know if you see any issues with this PR. |
Contributor
Author
|
@comtihon pinging you again to hopefully have this reviewed and merged. |
Owner
|
Somehow missed this. Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
{keepalive, true}and{nodelay, true}socket options to bothgen_tcp:connectandssl:connectcalls inmc_worker_logic:do_connect/5.This brings the Erlang driver into compliance with MongoDB specification DRIVERS-383, which requires all drivers to enable TCP keepalive by default. All other official MongoDB drivers (Node.js, Python, Java, Go, C) already comply.
Problem
Without TCP keepalive, idle connections are silently closed by firewalls and load balancers, causing high connection churn in production environments.
Changes
src/connection/mc_worker_logic.erl: Added{keepalive, true}, {nodelay, true}to both the plain TCP and SSL clauses ofdo_connect/5test/tcp_keepalive_SUITE.erl: New test suite with property-based tests (PropEr, 100 iterations each) and unit testsSocket option precedence (SSL)
For SSL connections, user-provided
ssl_optsare appended after the defaults (++ Opts), so users can override keepalive/nodelay if needed (last occurrence wins).Tests
prop_keepalive_nodelay_enabledprop_existing_options_preservedprop_ssl_user_option_overrideunit_keepalive_concreteunit_keepalive_pingScope
do_connect/5)