private void OnConnectionClosing(IConnection c = null) { int currentLength; if (c != null) { var removalInfo = _connections.RemoveAndCount(c); currentLength = removalInfo.Item2; var hasBeenRemoved = removalInfo.Item1; if (!hasBeenRemoved) { // It has been already removed (via event or direct call) // When it was removed, all the following checks have been made // No point in doing them again return; } HostConnectionPool.Logger.Info("Pool #{0} for host {1} removed a connection, new length: {2}", GetHashCode(), _host.Address, currentLength); } else { currentLength = _connections.Count; } if (IsClosing || currentLength >= _expectedConnectionLength) { // No need to reconnect return; } // We are using an IO thread Task.Run(async() => { // Use a lock for avoiding concurrent calls to SetNewConnectionTimeout() await _allConnectionClosedEventLock.WaitAsync().ConfigureAwait(false); try { if (currentLength == 0) { // All connections have been closed // If the node is UP, we should stop attempting to reconnect if (_host.IsUp && AllConnectionClosed != null) { // Raise the event and wait for a caller to decide AllConnectionClosed(_host, this); return; } } SetNewConnectionTimeout(_reconnectionSchedule); } finally { _allConnectionClosedEventLock.Release(); } }).Forget(); }