feat: support reusing an existing mongo client to prevent connection churn - #242
Open
Tsurai7 wants to merge 2 commits into
Open
feat: support reusing an existing mongo client to prevent connection churn#242Tsurai7 wants to merge 2 commits into
Tsurai7 wants to merge 2 commits into
Conversation
(cherry picked from commit ca40192)
Replace NewPingCheck with an optional Client field in Config, mirroring the cassandra check's Session pattern, so the check can reuse the application's connection pool instead of establishing and tearing down a new connection on every run.
Tsurai7
force-pushed
the
fix-mongo-conn-churn
branch
from
July 3, 2026 16:33
de579cd to
a87fd89
Compare
Tsurai7
marked this pull request as ready for review
July 3, 2026 16:33
Author
|
@Tsurai7 I'm not the owner of this repo, sorry. |
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
The existing mongo check creates and tears down a new database connection on every periodic run. This causes significant overhead and socket exhaustion (TCP TIME_WAIT leaks), and it does not verify the health of the application's actual connection pool. MongoDB's documentation recommends reusing a single client: https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#:~:text=Reuse%20Your%20Client%20with%20Connection%20Pools
This PR adds an optional
Client *mongo.Clientfield toConfig. When it is supplied, the check reuses the application's client (and its connection pool) and only runs the ping command; the caller stays responsible for the client's lifecycle. When it is not supplied, the previous DSN-based behavior is unchanged: the check still establishes a fresh connection, pings, and disconnects with the existing three timeouts.The API follows the same pattern this library already uses for cassandra, where an existing
Sessioncan be supplied inConfigin place ofHostsandKeyspace(seechecks/cassandra/check.go), so no new constructor is introduced and nothing is deprecated.