Skip to content

Commit 0f30350

Browse files
committed
Avoid stale connections by pinging on acquire
The exporter was returning cached MongoDB connections without validating their health. This meant that if a connection had become unhealthy, subsequent operations would fail after a delay (serverSelectionTimeout etc), causing the entire request to be slower than it should be. This commit adds `Ping` check before returning a cached client.
1 parent f6389c5 commit 0f30350

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

exporter/exporter.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,13 @@ func (e *Exporter) getClient(ctx context.Context) (*mongo.Client, error) {
273273
e.clientMu.Lock()
274274
defer e.clientMu.Unlock()
275275

276-
// If client is already initialized, return it.
276+
// If client is already initialized, and Ping is succesful -- return it.
277277
if e.client != nil {
278+
err := e.client.Ping(ctx, nil)
279+
if err != nil {
280+
return nil, err
281+
}
282+
278283
return e.client, nil
279284
}
280285

0 commit comments

Comments
 (0)