private void ConfigurationStreamRequestEndGetResponse(IAsyncResult ar) { try { var request = (WebRequest)ar.AsyncState; var response = request.EndGetResponse(ar); // Allocate new string reader and start reading _linesStreamReader = new AsyncLinesStreamReader(response.GetResponseStream()); _linesStreamReader.OnError += HandleLinesStreamError; _linesStreamReader.OnChunk += HandleConfigurationUpdate; _linesStreamReader.StartReading(); } catch (Exception e) { if (OnError != null) { OnError(e); } // Handle HTTP query errors by trying another host RetryConnectingToConfigurationStream(1); } }
public CouchbaseCluster(MemcacheClientConfiguration configuration, string bucket, IPEndPoint[] configurationHosts) { if (configurationHosts.Length == 0) { throw new ArgumentException("There should be at least one value in the list", "configurationHosts"); } _isInitialized = false; _linesStreamReader = null; _webResponse = null; _configuration = configuration; if (_configuration.Authenticator == null) { _configuration.Authenticator = MemcacheClientConfiguration.SaslPlainAuthenticatorFactory(string.Empty, bucket, string.Empty); } _bucket = bucket; _currentConfigurationHost = 0; _configurationHosts = configurationHosts; _memcacheNodes = new Dictionary <string, IMemcacheNode>(); _locator = null; _connectionTimer = new Timer(_ => ConnectToConfigurationStream(), null, Timeout.Infinite, Timeout.Infinite); _receivedInitialConfigurationBarrier = new ManualResetEventSlim(); }
public void TearDown() { _reader.Dispose(); _reader = null; _pipe.Close(); _pipe = null; }
public void TearDown() { _reader.Dispose(); _reader = null; _stream.Dispose(); _stream = null; }
public void SetUp() { _errors = _chunks = 0; _stream = Stream.Synchronized(new MemoryStream()); _reader = new AsyncLinesStreamReader(_stream); _reader.OnError += _ => { _errors++; }; _reader.OnChunk += _ => { _chunks++; }; _reader.StartReading(); }
public void SetUp() { var localCounters = new TestCounters(); _counters = localCounters; _pipe = new Aqueduct(); _reader = new AsyncLinesStreamReader(_pipe.Out); _reader.OnError += e => { localCounters.IncrementErrors(); Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); }; _reader.OnChunk += _ => { localCounters.IncrementChunks(); }; _reader.StartReading(); }
private void KillCurrentConnection() { if (_linesStreamReader != null) { _linesStreamReader.Dispose(); _linesStreamReader = null; } if (_webResponse != null) { _webResponse.Dispose(); _webResponse = null; } }
private void ConnectToConfigurationStream() { if (_linesStreamReader != null) { _linesStreamReader.Dispose(); _linesStreamReader = null; } // Start loop at 1 to make sure we always try a server different from the last one upon (re)connecting. var connecting = false; for (var i = 1; !connecting && i <= _configurationHosts.Length; i++) { connecting = true; _currentConfigurationHost = (_currentConfigurationHost + i) % _configurationHosts.Length; var url = string.Format( "http://{0}:{1}/pools/default/bucketsStreaming/{2}", _configurationHosts[_currentConfigurationHost].Address, _configurationHosts[_currentConfigurationHost].Port, _bucket); var request = WebRequest.Create(url); try { request.BeginGetResponse(ConfigurationStreamRequestEndGetResponse, request); } catch (Exception e) { // Try the next host connecting = false; if (OnError != null) { OnError(e); } } } // In case no host can be reached, try again in 30s if (!connecting) { RetryConnectingToConfigurationStream(30); } }
private void ConfigurationStreamRequestEndGetResponse(IAsyncResult ar) { if (_linesStreamReader != null) { OnError(new MemcacheException("Sanity check: The previous AsyncLinesStreamReader was not disposed")); return; } if (_webResponse != null) { OnError(new MemcacheException("Sanity check: The previous WebResponse was not disposed")); return; } WebResponse response = null; AsyncLinesStreamReader linesStreamReader = null; try { var request = (WebRequest)ar.AsyncState; response = request.EndGetResponse(ar); // Allocate new string reader and start reading linesStreamReader = new AsyncLinesStreamReader(response.GetResponseStream()); linesStreamReader.OnError += HandleLinesStreamError; linesStreamReader.OnChunk += HandleConfigurationUpdate; lock (this) if (!_isDisposed) { _webResponse = response; _linesStreamReader = linesStreamReader; } else { response.Dispose(); linesStreamReader.Dispose(); return; } _linesStreamReader.StartReading(); } catch (Exception e) { if (OnError != null) { OnError(e); } if (response != null) { response.Dispose(); } _webResponse = null; if (linesStreamReader != null) { linesStreamReader.Dispose(); } _linesStreamReader = null; // Handle HTTP query errors by trying another host RetryConnectingToConfigurationStream(delaySeconds: 1.0); } }