Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1978 [created] 4bacf67af
Add check for connection state before returning it TINKERPOP-1978 Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/4bacf67a Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/4bacf67a Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/4bacf67a Branch: refs/heads/TINKERPOP-1978 Commit: 4bacf67af79025520171cbd6ea75c9237981df88 Parents: 32aebb8 Author: Florian Hockmann <[email protected]> Authored: Sat Jun 23 11:56:01 2018 +0200 Committer: Florian Hockmann <[email protected]> Committed: Sat Jun 23 11:56:01 2018 +0200 ---------------------------------------------------------------------- .../src/Gremlin.Net/Driver/ConnectionPool.cs | 26 ++++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4bacf67a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs index 9501686..69985f5 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs @@ -23,7 +23,6 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Gremlin.Net.Process; @@ -45,17 +44,28 @@ namespace Gremlin.Net.Driver public async Task<IConnection> GetAvailableConnectionAsync() { + var connection = GetConnectionFromPoolIfAvailable() ?? + await CreateNewConnectionAsync().ConfigureAwait(false); + + return new ProxyConnection(connection, AddConnectionIfOpen); + } + + private Connection GetConnectionFromPoolIfAvailable() + { Connection connection = null; - lock (_connectionsLock) + while (true) { - if (!_connections.IsEmpty) + lock (_connectionsLock) + { + if (_connections.IsEmpty) break; _connections.TryTake(out connection); - } - - if (connection == null) - connection = await CreateNewConnectionAsync().ConfigureAwait(false); + } - return new ProxyConnection(connection, AddConnectionIfOpen); + if (connection.IsOpen) break; + connection.Dispose(); + connection = null; + } + return connection; } private async Task<Connection> CreateNewConnectionAsync()
