Skip to content

feat: support reusing an existing mongo client to prevent connection churn - #242

Open
Tsurai7 wants to merge 2 commits into
hellofresh:masterfrom
Tsurai7:fix-mongo-conn-churn
Open

feat: support reusing an existing mongo client to prevent connection churn#242
Tsurai7 wants to merge 2 commits into
hellofresh:masterfrom
Tsurai7:fix-mongo-conn-churn

Conversation

@Tsurai7

@Tsurai7 Tsurai7 commented May 4, 2026

Copy link
Copy Markdown

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.Client field to Config. 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 Session can be supplied in Config in place of Hosts and Keyspace (see checks/cassandra/check.go), so no new constructor is introduced and nothing is deprecated.

(cherry picked from commit ca40192)
@Tsurai7 Tsurai7 changed the title feat: add NewPingCheck to reuse client and prevent connection churn feat: support reusing an existing mongo client to prevent connection churn Jul 3, 2026
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
Tsurai7 force-pushed the fix-mongo-conn-churn branch from de579cd to a87fd89 Compare July 3, 2026 16:33
@Tsurai7
Tsurai7 marked this pull request as ready for review July 3, 2026 16:33
@Tsurai7
Tsurai7 requested a review from a team as a code owner July 3, 2026 16:33
@Tsurai7

Tsurai7 commented Jul 27, 2026

Copy link
Copy Markdown
Author

@Nelapa @Aj-vrod guys could you take a look pls? Thanks in advance

@Nelapa

Nelapa commented Jul 28, 2026

Copy link
Copy Markdown

@Tsurai7 I'm not the owner of this repo, sorry.

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.

2 participants