Exemplo n.º 1
0
        private void TryToReconnectServerAndUpdateClusterIfServerStillExists(string serverId)
        {
            lock (_gate)
            {
                Console.WriteLine("reconnecting");
                Cluster cluster = _cluster;
                var serverWithDisconnectedMemcachedClient = cluster.GetServerById(serverId);

                if (_hasQuit ||
                    serverWithDisconnectedMemcachedClient == null)
                {
                    return;
                }

                TcpClient client = new TcpClient();
                var reconnectAttempt = new ReconnectAttempt(client, serverWithDisconnectedMemcachedClient);

                _reconnectMemcachedAsync.BeginAsync(client, reconnectAttempt);
            }
        }
Exemplo n.º 2
0
        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();
            }
        }
Exemplo n.º 3
0
        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();
            }
        }
Exemplo n.º 4
0
        private IAsyncResult BeginMemcachedReconnection(TcpClient tcpClient, AsyncPattern<TcpClient, ReconnectAttempt> pattern, ReconnectAttempt reconnectAttempt)
        {
            var result = tcpClient.BeginConnect(
                reconnectAttempt.Server.HostName,
                reconnectAttempt.Server.MemcachedPort,
                pattern.OnCompleted,
                reconnectAttempt);

            return result;
        }