private AsyncPatternResult<TcpClient, ReconnectAttempt> OnMemcachedReconnectionError(IAsyncResult result, ReconnectAttempt reconnectAttempt, Exception e) { lock (_gate) { var cluster = _cluster; var serverInLatestCluster = cluster.GetServerById(reconnectAttempt.Server.Id); if (_hasQuit || serverInLatestCluster == null || reconnectAttempt.HasBeenCanceled) { _currentlyUnderwayMemcachedReconnectAttempts.Remove(reconnectAttempt); reconnectAttempt.Cancel(); } else { const int millisecondsToWaitBetweenReconnectAttempts = 1000; reconnectAttempt.Timer = new Timer( OnReconnectTimerElapsed, reconnectAttempt, millisecondsToWaitBetweenReconnectAttempts, Timeout.Infinite); } return _reconnectMemcachedAsync.Stop(); } }
private AsyncPatternResult<TcpClient, ReconnectAttempt> OnMemcacachedReconnectionCompleted(IAsyncResult result, ReconnectAttempt reconnectAttempt) { TcpClient tcpClient = reconnectAttempt.TcpClient; tcpClient.EndConnect(result); lock (_gate) { var cluster = _cluster; //Update the cluster after we have successfully reconnected. var newCluster = cluster.Clone(); var serverInLatestCluster = newCluster.GetServerById(reconnectAttempt.Server.Id); if (_hasQuit || serverInLatestCluster == null || reconnectAttempt.HasBeenCanceled) { _currentlyUnderwayMemcachedReconnectAttempts.Remove(reconnectAttempt); reconnectAttempt.Cancel(); } else { MemcachedClient newMemcachedClient = new MemcachedClient(serverInLatestCluster.Id, serverInLatestCluster.HostName, serverInLatestCluster.MemcachedPort); newMemcachedClient.OnDisconnected += OnMemcachedDisconnection; newMemcachedClient.OnRecoverableError += OnPossiblyRecoverableMemcachedError; newMemcachedClient.Connect(tcpClient); serverInLatestCluster.MemcachedClient = newMemcachedClient; _currentlyUnderwayMemcachedReconnectAttempts.Remove(reconnectAttempt); _cluster = newCluster; } return _reconnectMemcachedAsync.Stop(); } }