/// <summary> /// Try to connect with next server in config /// </summary> /// <returns>new connection</returns> private void TryNextServer() { bool connected = false; OperationNotSupportedException exceptionThrown = null; int retries = this._connectionRetries; try { _lock.AcquireWriterLock(_connectionMutexTimeout); } catch (Exception) { return; //lock could not be granted before the timeout expires. } try { CheckRetryConnectionDelay(); //[ Checking if retry connection Interval is over or not] if (!_retryConnection) return; while (retries-- > 0 && !_isDisposing) { try { if (_connection.IsConnected) connected = true; else { this._connection.StatusLatch.SetStatusBit(ConnectionStatus.Connecting, ConnectionStatus.Connected | ConnectionStatus.Disconnected); if (_clientConfig == null) _clientConfig = new ClientConfiguration(_cacheId); int nretries = 3; while (true) { try { _clientConfig.LoadConfiguration(); break; } catch (IOException) { if (--nretries == 0) throw; Thread.Sleep(500); } } if (_clientConfig.ServerCount > 0) { RemoteServer nextServer = _clientConfig.NextServer; RemoteServer startingServer = nextServer; while (!connected) { if (nextServer == null) break; if (nextServer.IP != null) { for (int i = 0; i < nextServer.PortRange; i++) { try { if (!connected) { Exception exception = null; connected = ConnectRemoteServer(this._connection, nextServer.IP, nextServer.Port + i, this._balanceNode, this._importHashmap, true, ref exception); } if (connected) break; } catch (OperationNotSupportedException operationException) { exceptionThrown = operationException; } } } if (!connected) { nextServer = _clientConfig.NextServer; if (startingServer.Equals(nextServer)) break; } } //if the connection is established, exit the outer loop. //otherwise sleep for the sleep interval and retry. if (connected) break; Thread.Sleep(_retryInterval); continue; } else { throw new ConfigurationException("'client.ncconf' not found or does not contain server information"); } } } catch (Exception) { throw; } } } catch (Exception) { throw; } finally { //set the connection status byte setStatus = connected ? ConnectionStatus.Connected : ConnectionStatus.Disconnected; byte unsetStatus = (byte) ((!connected ? ConnectionStatus.Connected : ConnectionStatus.Disconnected) | ConnectionStatus.Connecting); _connection.StatusLatch.SetStatusBit(setStatus, unsetStatus); _retryConnection = connected; //release the lock _lock.ReleaseWriterLock(); if (!connected) { if (exceptionThrown != null) throw exceptionThrown; } } }
private Connection TryPool() { Connection connection = null; if (_clientConfig == null) _clientConfig = new ClientConfiguration(_cacheId); int retries = 3; while (true && !_isDisposing) { try { _clientConfig.LoadConfiguration(); break; } catch (System.IO.IOException ie) { if (--retries == 0) throw ie; System.Threading.Thread.Sleep(500); } } RemoteServer nextServer = _clientConfig.NextServer; RemoteServer startingServer = nextServer; while (true && !_isDisposing) { if (_clientConfig.ServerCount > 0) { connection = this._pool[new Address(nextServer.IP.ToString(), nextServer.Port)]; if (connection != null && connection.IsConnected) break; else { nextServer = _clientConfig.NextServer; if (startingServer.Equals(nextServer)) break; } } else break; } if (connection == null) { while (true && !_isDisposing) { if (_clientConfig.ServerCount > 0) { if (this._pool.Contains(new Address(nextServer.IP.ToString(), nextServer.Port))) { connection = this._pool[new Address(nextServer.IP.ToString(), nextServer.Port)]; } else { connection = new Connection(this._commandReieved, this._serverLost, this._logger, _perfStatsColl, _responseIntegrator, _clientConfig.BindIP); } if (!connection.IsConnected) { connection = ReconnectServer(connection, nextServer); if (connection.IsConnected) { if (!this._pool.Contains(new Address(nextServer.IP.ToString(), nextServer.Port))) this._pool[new Address(nextServer.IP.ToString(), nextServer.Port)] = connection; break; } else { nextServer = _clientConfig.NextServer; if (startingServer.Equals(nextServer)) break; } } else break; } else break; } } return connection; }
private Broker(RemoteCache cache, bool importHashMap, PerfStatsCollector2 perfStatsColl, CacheInitParams initParams) { _bulkEventCallback = new WaitCallback(RaiseBulkEvent); this._clientConfig = new ClientConfiguration(cache.CacheId, initParams); this._cache = cache; this._balanceNode = _clientConfig.BalanceNodes; this._importHashmap = _clientConfig.ImportHashmap; this._operationTimeout = _clientConfig.Timeout; this._connectionTimeout = _clientConfig.ConnectionTimeout; this._connectionRetries = _clientConfig.ConnectionRetries; this._retryInterval = _clientConfig.RetryInterval; this._retryConnnectionDelay = _clientConfig.RetryConnectionDelay; this._retryConnectionDelayInMinutes = Convert.ToDouble(_retryConnnectionDelay)/60000; //Conversion to minutes from milliseconds; _perfStatsColl2 = perfStatsColl; int pid = System.Diagnostics.Process.GetCurrentProcess().Id; string instanceName = "Client." + cache.CacheId + "." + pid; if (_perfStatsColl == null || !_perfStatsColl.InstanceName.Equals(instanceName)) { _perfStatsColl = new PerfStatsCollector(instanceName, 0); } this._commandReieved = new OnCommandRecieved(CommandReceived); this._serverLost = new OnServerLost(ServerLost); this._requestTable = Hashtable.Synchronized(new Hashtable(10000, 0.75f)); this._pool = new ConnectionPool(); }