Repository: tinkerpop Updated Branches: refs/heads/TINKERPOP-1752 05851764f -> b8a278c6c (forced update)
Add handling for closed connections Closed connections are not returned to the ConnectionPool as the client cannot send any further requests with them. Additionally, the host is considered to be unavailable when a connection was closed and all other connections are closed and removed from the ConnectionPool. This is in line with the handling for closed connection in gremlin-driver. Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/98e4e1b8 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/98e4e1b8 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/98e4e1b8 Branch: refs/heads/TINKERPOP-1752 Commit: 98e4e1b8201f50e1f34458a82c20f2737aa6ae53 Parents: 397158a Author: florianhockmann <florianhockm...@apache.org> Authored: Sat Sep 2 17:54:39 2017 +0200 Committer: florianhockmann <florianhockm...@apache.org> Committed: Sat Sep 2 17:54:39 2017 +0200 ---------------------------------------------------------------------- .../src/Gremlin.Net/Driver/Connection.cs | 2 + .../src/Gremlin.Net/Driver/ConnectionPool.cs | 62 ++++++++++++++------ .../Gremlin.Net/Driver/WebSocketConnection.cs | 2 + 3 files changed, 47 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/98e4e1b8/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs index 126b461..dbbd375 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs @@ -68,6 +68,8 @@ namespace Gremlin.Net.Driver await _webSocketConnection.CloseAsync().ConfigureAwait(false); } + public bool IsOpen => _webSocketConnection.IsOpen; + private async Task SendAsync(RequestMessage message) { var graphsonMsg = _graphSONWriter.WriteObject(message); http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/98e4e1b8/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 945e5e4..779815a 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs @@ -55,7 +55,7 @@ namespace Gremlin.Net.Driver if (connection == null) connection = await CreateNewConnectionAsync().ConfigureAwait(false); - return new ProxyConnection(connection, AddConnection); + return new ProxyConnection(connection, AddConnectionIfOpen); } private async Task<Connection> CreateNewConnectionAsync() @@ -66,6 +66,17 @@ namespace Gremlin.Net.Driver return newConnection; } + private void AddConnectionIfOpen(Connection connection) + { + if (!connection.IsOpen) + { + ConsiderUnavailable(); + connection.Dispose(); + return; + } + AddConnection(connection); + } + private void AddConnection(Connection connection) { lock (_connectionsLock) @@ -74,6 +85,36 @@ namespace Gremlin.Net.Driver } } + private void ConsiderUnavailable() + { + CloseAndRemoveAllConnections(); + } + + private void CloseAndRemoveAllConnections() + { + lock (_connectionsLock) + { + TeardownAsync().WaitUnwrap(); + RemoveAllConnections(); + } + } + + private void RemoveAllConnections() + { + while (!_connections.IsEmpty) + { + _connections.TryTake(out var connection); + connection.Dispose(); + } + } + + private async Task TeardownAsync() + { + var closeTasks = new List<Task>(_connections.Count); + closeTasks.AddRange(_connections.Select(conn => conn.CloseAsync())); + await Task.WhenAll(closeTasks).ConfigureAwait(false); + } + #region IDisposable Support private bool _disposed; @@ -89,27 +130,10 @@ namespace Gremlin.Net.Driver if (!_disposed) { if (disposing) - lock (_connectionsLock) - { - if (_connections != null && !_connections.IsEmpty) - { - TeardownAsync().WaitUnwrap(); - - foreach (var conn in _connections) - conn.Dispose(); - } - } + CloseAndRemoveAllConnections(); _disposed = true; } } - - private async Task TeardownAsync() - { - var closeTasks = new List<Task>(_connections.Count); - closeTasks.AddRange(_connections.Select(conn => conn.CloseAsync())); - await Task.WhenAll(closeTasks).ConfigureAwait(false); - } - #endregion } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/98e4e1b8/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs ---------------------------------------------------------------------- diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs index 5a20759..9672606 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Driver/WebSocketConnection.cs @@ -71,6 +71,8 @@ namespace Gremlin.Net.Driver } } + public bool IsOpen => _client.State == WebSocketState.Open; + #region IDisposable Support private bool _disposed;