[ 
https://issues.apache.org/jira/browse/TINKERPOP-1978?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16522475#comment-16522475
 ] 

ASF GitHub Bot commented on TINKERPOP-1978:
-------------------------------------------

Github user FlorianHockmann commented on a diff in the pull request:

    https://github.com/apache/tinkerpop/pull/885#discussion_r197855449
  
    --- Diff: gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionPool.cs ---
    @@ -45,19 +44,28 @@ public ConnectionPool(ConnectionFactory 
connectionFactory)
     
             public async Task<IConnection> GetAvailableConnectionAsync()
             {
    -            Connection connection = null;
    -            lock (_connectionsLock)
    -            {
    -                if (!_connections.IsEmpty)
    -                    _connections.TryTake(out connection);
    -            }
    -
    -            if (connection == null)
    +            if (!TryGetConnectionFromPool(out var connection))
                     connection = await 
CreateNewConnectionAsync().ConfigureAwait(false);
     
                 return new ProxyConnection(connection, AddConnectionIfOpen);
             }
     
    +        private bool TryGetConnectionFromPool(out Connection connection)
    +        {
    +            while (true)
    +            {
    +                connection = null;
    +                lock (_connectionsLock)
    +                {
    +                    if (_connections.IsEmpty) return false;
    +                    _connections.TryTake(out connection);
    +                }
    +
    +                if (connection.IsOpen) return true;
    +                connection.Dispose();
    --- End diff --
    
    `_connections.TryTake(out connection)` removes the connection from the 
collection. So, here it is already no longer part of `_connections`. That may 
be a bit confusing as `_connections` doesn't contain all connections in the 
pool but only those that are currently unused. That made this class a lot 
easier to implement as no checks are needed of whether a connection is already 
in use or not.
    For reference: The 
[`ConcurrentBag<T>.TryTake`](https://msdn.microsoft.com/en-us/library/dd381774(v=vs.110).aspx)
 docs.


> Check for Websocket connection state when retrieved from Connection Pool 
> missing
> --------------------------------------------------------------------------------
>
>                 Key: TINKERPOP-1978
>                 URL: https://issues.apache.org/jira/browse/TINKERPOP-1978
>             Project: TinkerPop
>          Issue Type: Bug
>          Components: dotnet
>    Affects Versions: 3.3.3
>            Reporter: Jamilu Abubakar
>            Assignee: Florian Hockmann
>            Priority: Major
>
> When retrieving a connection from the connection pool there is no check to 
> see what the current connection state of the websocket is before it is used.  
> This means that if the websocket were to be closed because the server 
> recycled or the connection was aborted by the server since it was last used 
> and its Send method were to be called the following exception would be thrown:
> {quote}Error during execution ExecuteJObjectQueryAsync method. Exception: 
> System.Net.Http.WinHttpException (0x80072EFF): The connection with the server 
> was terminated abnormally
> at System.Net.WebSockets.WinHttpWebSocket.SendAsync(ArraySegment`1 buffer, 
> WebSocketMessageType messageType, Boolean endOfMessage, CancellationToken 
> cancellationToken)
> at System.Net.WebSockets.WebSocketHandle.SendAsync(ArraySegment`1 buffer, 
> WebSocketMessageType messageType, Boolean endOfMessage, CancellationToken 
> cancellationToken)
> at Gremlin.Net.Driver.WebSocketConnection.<SendMessageAsync>d__5.MoveNext()
> {quote}
>  
> Please add check the Websocket state before use, handle exceptions due to 
> closed or aborted connections and ideally have an extensible retry handler.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to