public void When_GetTerseStreamingUri_Called_With_useSsl_True_Url_Uses_Https()
        {
            const bool useSsl = true;
            var        node   = _bucket1.Nodes.First();
            var        url    = _bucket1.GetTerseStreamingUri(node, useSsl);

            Assert.AreEqual(new Uri("https://192.168.56.101:18091/pools/default/bs/default"), url);
        }
示例#2
0
        /// <summary>
        /// Starts the streaming connection to couchbase server that will
        /// listen for configuration changes and then update the client as needed.
        /// </summary>
        /// <remarks>
        /// Should not be used when a <see cref="SynchronizationContext" /> is present on the thread, as this
        /// could cause deadlocks.  This method is currently only used from within a dedicated thread,
        /// created by <see cref="HttpStreamingProvider.RegisterObserver"/>, so it is safe because there will not
        /// be a SynchronizationContext present on the thread.
        /// </remarks>
        public void ListenForConfigChanges()
        {
            var count = 0;

            //Make a copy of the nodes and shuffle them for randomness
            var nodes = _bucketConfig.Nodes.ToList();

            using (var httpClient =
                       new HttpClient(new AuthenticatingHttpClientHandler(_bucketConfig.Name, _bucketConfig.Password)))
            {
                httpClient.Timeout = Timeout.InfiniteTimeSpan;

                //This will keep trying until it runs out of servers to try in the cluster
                while (nodes.ToList().Any())
                {
                    try
                    {
                        //If the main thread has canceled, break out of the loop otherwise
                        //the next node in the server list will be tried; but in this case
                        //we want to shut things down and terminate the thread
                        if (_cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }
                        nodes = nodes.Shuffle();
                        var node = nodes[0];
                        nodes.Remove(node);

                        var streamingUri = _bucketConfig.GetTerseStreamingUri(node, _bucketConfig.UseSsl);
                        Log.Info(m => m("Listening to {0}", streamingUri));

                        var response =
                            httpClient.GetAsync(streamingUri, HttpCompletionOption.ResponseHeadersRead,
                                                _cancellationToken)
                            .Result;
                        response.EnsureSuccessStatusCode();

                        using (var stream = response.Content.ReadAsStreamAsync().Result)
                        {
                            //this will cancel the infinite wait below
                            _cancellationToken.Register(stream.Dispose);

                            stream.ReadTimeout = Timeout.Infinite;

                            using (var reader = new StreamReader(stream, Encoding.UTF8, false))
                            {
                                string config;
                                while (!_cancellationToken.IsCancellationRequested &&
                                       ((config = reader.ReadLineAsync().Result) != null))
                                {
                                    if (config != String.Empty)
                                    {
                                        Log.Info(m => m("configuration changed count: {0}", count++));
                                        Log.Info(m => m("Worker Thread: {0}", Thread.CurrentThread.ManagedThreadId));
                                        var config1 = config;
                                        Log.Debug(m => m("{0}", config1));

                                        config = config.Replace("$HOST", streamingUri.Host);
                                        var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(config);
                                        bucketConfig.SurrogateHost = GetSurrogateHost(streamingUri);
                                        if (_configChangedDelegate != null)
                                        {
                                            bucketConfig.Password = _bucketConfig.Password;
                                            _configChangedDelegate(bucketConfig);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (HttpRequestException e)
                    {
                        Log.Info(e);
                    }
                    catch (IOException e)
                    {
                        Log.Info(e);
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);
                    }
                }
            }

            //We tried all nodes in the current configuration, alert the provider that we
            //need to try to re-bootstrap from the beginning
            if (nodes.Count == 0)
            {
                _errorOccurredDelegate(_bucketConfig);
            }
        }
        /// <summary>
        /// Starts the streaming connection to couchbase server that will
        /// listen for configuration changes and then update the client as needed.
        /// </summary>
        public void ListenForConfigChanges()
        {
            var count = 0;

            //Make a copy of the nodes and shuffle them for randomness
            var nodes = _bucketConfig.Nodes.ToList();

            //This will keep trying until it runs out of servers to try in the cluster
            while (nodes.ToList().Any())
            {
                try
                {
                    //If the main thread has canceled, break out of the loop otherwise
                    //the next node in the server list will be tried; but in this case
                    //we want to shut things down and terminate the thread
                    if (_cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    nodes = nodes.Shuffle();
                    var node = nodes[0];
                    nodes.Remove(node);

                    var streamingUri = _bucketConfig.GetTerseStreamingUri(node, _bucketConfig.UseSsl);
                    Log.Info(m => m("Listening to {0}", streamingUri));

                    using (var webClient = new AuthenticatingWebClient(_bucketConfig.Name, _bucketConfig.Password))
                        using (var stream = webClient.OpenRead(streamingUri))
                        {
                            //this will cancel the infinite wait below - the temp variable removes
                            //chance of deadlock when dispose is called on the closure
                            var temp = webClient;
                            _cancellationToken.Register(temp.CancelAsync);

                            if (stream == null)
                            {
                                return;
                            }
                            stream.ReadTimeout = Timeout.Infinite;
                            using (var reader = new StreamReader(stream, Encoding.UTF8, false))
                            {
                                string config;
                                while ((config = reader.ReadLine()) != null)
                                {
                                    if (config != String.Empty)
                                    {
                                        Log.Info(m => m("configuration changed count: {0}", count++));
                                        Log.Info(m => m("Worker Thread: {0}", Thread.CurrentThread.ManagedThreadId));
                                        var config1 = config;
                                        Log.Debug(m => m("{0}", config1));

                                        var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(config);
                                        bucketConfig.SurrogateHost = GetSurrogateHost(streamingUri);
                                        if (_configChangedDelegate != null)
                                        {
                                            _configChangedDelegate(bucketConfig);
                                        }
                                    }
                                }
                            }
                        }
                }
                catch (WebException e)
                {
                    Log.Error(e);
                }
                catch (IOException e)
                {
                    Log.Error(e);
                }
            }

            //We tried all nodes in the current configuration, alert the provider that we
            //need to try to re-bootstrap from the beginning
            if (nodes.Count == 0)
            {
                _errorOccurredDelegate(_bucketConfig);
            }
        }
        /// <summary>
        /// Starts the streaming connection to couchbase server that will
        /// listen for configuration changes and then update the client as needed.
        /// </summary>
        /// <remarks>
        /// Should not be used when a <see cref="SynchronizationContext" /> is present on the thread, as this
        /// could cause deadlocks.  This method is currently only used from within a dedicated thread,
        /// created by <see cref="HttpStreamingProvider.RegisterObserver"/>, so it is safe because there will not
        /// be a SynchronizationContext present on the thread.
        /// </remarks>
        public void ListenForConfigChanges()
        {
            var count = 0;

            //Make a copy of the nodes and shuffle them for randomness
            var nodes = _bucketConfig.Nodes.ToList();

            //if RBAC is being used with >= CB 5.0, then use the username otherwise use the bucket name
            var bucketNameOrUserName = string.IsNullOrWhiteSpace(_bucketConfig.Username)
                ? _bucketConfig.Name
                : _bucketConfig.Username;

            using (var httpClient = new CouchbaseHttpClient(bucketNameOrUserName, _bucketConfig.Password, _clientConfiguration))
            {
                httpClient.Timeout = Timeout.InfiniteTimeSpan;

                //This will keep trying until it runs out of servers to try in the cluster
                while (nodes.ToList().Any())
                {
                    try
                    {
                        //If the main thread has canceled, break out of the loop otherwise
                        //the next node in the server list will be tried; but in this case
                        //we want to shut things down and terminate the thread
                        if (_cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }
                        nodes = nodes.Shuffle();
                        var node = nodes[0];
                        nodes.Remove(node);

                        var streamingUri = _bucketConfig.GetTerseStreamingUri(node, _bucketConfig.UseSsl);
                        Log.Info("Listening to {0}", streamingUri);

                        var response =
                            httpClient.GetAsync(streamingUri, HttpCompletionOption.ResponseHeadersRead,
                                                _cancellationToken)
                            .Result;
                        response.EnsureSuccessStatusCode();

                        using (var stream = response.Content.ReadAsStreamAsync().Result)
                        {
                            //this will cancel the infinite wait below
                            _cancellationToken.Register(stream.Dispose);

                            using (var reader = new StreamReader(stream, Encoding.UTF8, false))
                            {
                                string config;
                                while (!_cancellationToken.IsCancellationRequested &&
                                       ((config = reader.ReadLineAsync().Result) != null))
                                {
                                    if (config != String.Empty)
                                    {
                                        Log.Info("configuration changed count: {0}", count++);
                                        Log.Info("Worker Thread: {0}", Thread.CurrentThread.ManagedThreadId);
                                        var config1 = config;
                                        Log.Debug("{0}", config1);

                                        config = config.Replace("$HOST", streamingUri.Host);
                                        var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(config);
                                        bucketConfig.SurrogateHost = GetSurrogateHost(streamingUri);
                                        if (_configChangedDelegate != null)
                                        {
                                            bucketConfig.Password = _bucketConfig.Password;
                                            bucketConfig.Username = _bucketConfig.Username;
                                            _configChangedDelegate(bucketConfig);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (AggregateException e)
                    {
                        var exceptions = e.Flatten().InnerExceptions;
                        if (exceptions.OfType <ObjectDisposedException>().Any())
                        {
                            Log.Info("The config listener has shut down.");
                        }
                        foreach (var ex in exceptions.Where(x => x.GetType() != typeof(ObjectDisposedException)))
                        {
                            Log.Error(ex);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e);
                    }
                }
            }

            //We tried all nodes in the current configuration, alert the provider that we
            //need to try to re-bootstrap from the beginning
            if (nodes.Count == 0)
            {
                _errorOccurredDelegate(_bucketConfig);
            }
        }