This is an automated email from the ASF dual-hosted git repository.
florianhockmann pushed a commit to branch TINKERPOP-2135
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
The following commit(s) were added to refs/heads/TINKERPOP-2135 by this push:
new 0804cdd TINKERPOP-2135/2148 Remove while loop to populate pool
0804cdd is described below
commit 0804cddbb41a47945114656f629e9b0fd19ec04d
Author: Florian Hockmann <[email protected]>
AuthorDate: Wed Mar 13 12:14:24 2019 +0100
TINKERPOP-2135/2148 Remove while loop to populate pool
Instead of implicitly waiting for another thread to populate the pool,
now the pool population is aborted immediately which leads to an
exception. The idea here is to fail fast instead to communicate the
problem to the user.
Also a small refactoring where an out parameter is turned into a return
value.
---
.../src/Gremlin.Net/Driver/ConnectionPool.cs | 36 +++++++++-------------
1 file changed, 15 insertions(+), 21 deletions(-)
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
index 728b01f..09e20a1 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs
@@ -62,19 +62,17 @@ namespace Gremlin.Net.Driver
private async Task EnsurePoolIsPopulatedAsync()
{
// The pool could have been (partially) empty because of
connection problems. So, we need to populate it again.
- while (_poolSize < NrConnections)
+ if (_poolSize >= NrConnections) return;
+ var poolState = Interlocked.CompareExchange(ref _poolState,
PoolPopulationInProgress, PoolIdle);
+ if (poolState == PoolPopulationInProgress) return;
+ try
{
- var poolState = Interlocked.CompareExchange(ref _poolState,
PoolPopulationInProgress, PoolIdle);
- if (poolState == PoolPopulationInProgress) continue;
- try
- {
- await PopulatePoolAsync().ConfigureAwait(false);
- }
- finally
- {
- // We need to remove the PoolPopulationInProgress flag
again even if an exception occurred, so we don't block the pool population for
ever
- Interlocked.CompareExchange(ref _poolState, PoolIdle,
PoolPopulationInProgress);
- }
+ await PopulatePoolAsync().ConfigureAwait(false);
+ }
+ finally
+ {
+ // We need to remove the PoolPopulationInProgress flag again
even if an exception occurred, so we don't block the pool population for ever
+ Interlocked.CompareExchange(ref _poolState, PoolIdle,
PoolPopulationInProgress);
}
}
@@ -114,30 +112,26 @@ namespace Gremlin.Net.Driver
{
var connections = _connections.Snapshot;
if (connections.Length == 0) throw new
ServerUnavailableException();
-
- if (GetAvailableConnection(connections, out var connection))
- return connection;
- throw new ConnectionPoolBusyException(_poolSize,
_maxInProcessPerConnection);
+ return TryGetAvailableConnection(connections);
}
- private bool GetAvailableConnection(Connection[] connections, out
Connection connection)
+ private Connection TryGetAvailableConnection(Connection[] connections)
{
var index = Interlocked.Increment(ref _connectionIndex);
ProtectIndexFromOverflowing(index);
- connection = null;
for (var i = 0; i < connections.Length; i++)
{
- connection = connections[(index + i) % connections.Length];
+ var connection = connections[(index + i) % connections.Length];
if (connection.NrRequestsInFlight >=
_maxInProcessPerConnection) continue;
if (!connection.IsOpen)
{
RemoveConnectionFromPool(connection);
continue;
}
- return true;
+ return connection;
}
- return false;
+ throw new ConnectionPoolBusyException(_poolSize,
_maxInProcessPerConnection);
}
private void ProtectIndexFromOverflowing(int currentIndex)