Skip to content

Commit 155b926

Browse files
author
Robert Stam
committed
Fix CSHARP-454. Ping and VerifyState now open a new connection instead of using one from the connection pool, so they work quickly even if the connection pool is oversubscribed.
1 parent 2fe7af3 commit 155b926

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

Driver/Core/MongoServerInstance.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ public IPEndPoint GetIPEndPoint()
219219
/// </summary>
220220
public void Ping()
221221
{
222-
var connection = _connectionPool.AcquireConnection(null);
222+
// use a new connection instead of one from the connection pool
223+
var connection = new MongoConnection(this);
223224
try
224225
{
225-
var pingCommand = new CommandDocument("ping", 1);
226-
connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, pingCommand, true);
226+
Ping(connection);
227227
}
228228
finally
229229
{
230-
_connectionPool.ReleaseConnection(connection);
230+
connection.Close();
231231
}
232232
}
233233

@@ -238,21 +238,22 @@ public void VerifyState()
238238
{
239239
lock (_serverInstanceLock)
240240
{
241-
// Console.WriteLine("MongoServerInstance[{0}]: VerifyState called.", sequentialId);
242-
// if ping fails assume all connections in the connection pool are doomed
241+
// use a new connection instead of one from the connection pool
242+
var connection = new MongoConnection(this);
243243
try
244244
{
245-
Ping();
246-
}
247-
catch
248-
{
249-
// Console.WriteLine("MongoServerInstance[{0}]: Ping failed: {1}.", sequentialId, ex.Message);
250-
_connectionPool.Clear();
251-
}
245+
// Console.WriteLine("MongoServerInstance[{0}]: VerifyState called.", sequentialId);
246+
// if ping fails assume all connections in the connection pool are doomed
247+
try
248+
{
249+
Ping(connection);
250+
}
251+
catch
252+
{
253+
// Console.WriteLine("MongoServerInstance[{0}]: Ping failed: {1}.", sequentialId, ex.Message);
254+
_connectionPool.Clear();
255+
}
252256

253-
var connection = _connectionPool.AcquireConnection(null);
254-
try
255-
{
256257
var previousState = _state;
257258
try
258259
{
@@ -270,7 +271,7 @@ public void VerifyState()
270271
}
271272
finally
272273
{
273-
_connectionPool.ReleaseConnection(connection);
274+
connection.Close();
274275
}
275276
}
276277
}
@@ -373,6 +374,12 @@ internal void Disconnect()
373374
}
374375
}
375376

377+
internal void Ping(MongoConnection connection)
378+
{
379+
var pingCommand = new CommandDocument("ping", 1);
380+
connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, pingCommand, true);
381+
}
382+
376383
internal void ReleaseConnection(MongoConnection connection)
377384
{
378385
_connectionPool.ReleaseConnection(connection);

Driver/Internal/MongoConnection.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ internal MongoConnection(MongoConnectionPool connectionPool)
7474
_state = MongoConnectionState.Initial;
7575
}
7676

77+
internal MongoConnection(MongoServerInstance serverInstance)
78+
{
79+
_serverInstance = serverInstance;
80+
_createdAt = DateTime.UtcNow;
81+
_state = MongoConnectionState.Initial;
82+
}
83+
7784
// public properties
7885
/// <summary>
7986
/// Gets the connection pool that this connection belongs to.
@@ -540,7 +547,10 @@ private void HandleException(Exception ex)
540547
break;
541548
case HandleExceptionAction.ClearConnectionPool:
542549
Close();
543-
_connectionPool.Clear();
550+
if (_connectionPool != null)
551+
{
552+
_connectionPool.Clear();
553+
}
544554
break;
545555
default:
546556
throw new MongoInternalException("Invalid HandleExceptionAction");

0 commit comments

Comments
 (0)