public void Dispose(bool disposing) { try { ConfigLock.EnterWriteLock(); if (!Disposed && disposing) { GC.SuppressFinalize(this); } foreach (var configObserver in ConfigObservers) { UnRegisterObserver(configObserver.Value); } ConfigObservers.Clear(); Disposed = true; } finally { ConfigLock.ExitWriteLock(); } }
/// <summary> /// Raised when a configuration update has occurred. All observers will be notified of the changes. /// </summary> /// <param name="bucketConfig">The new configuration.</param> void ConfigChangedHandler(IBucketConfig bucketConfig) { try { ConfigLock.EnterWriteLock(); var configObserver = ConfigObservers[bucketConfig.Name]; IConfigInfo configInfo; if (Configs.TryGetValue(configObserver.Name, out configInfo)) { var staleBucketConfig = configInfo.BucketConfig; if (bucketConfig.Rev > staleBucketConfig.Rev) { configInfo.LoadConfig(bucketConfig); } } else { configInfo = CreateConfigInfo(bucketConfig); Configs.TryAdd(bucketConfig.Name, configInfo); } try { ClientConfig.UpdateBootstrapList(bucketConfig); configObserver.NotifyConfigChanged(configInfo); } catch (Exception e) { Log.Error(e); } SignalCountdownEvent(); } finally { ConfigLock.ExitWriteLock(); } }
/// <summary> /// Starts the HTTP streaming connection to the Couchbase Server and gets the latest configuration for a SASL authenticated Bucket. /// </summary> /// <param name="bucketName">The name of the Couchbase Bucket.</param> /// <param name="password">The SASL password used to connect to the Bucket.</param> /// <returns>A <see cref="IConfigInfo"/> object representing the latest configuration.</returns> public override IConfigInfo GetConfig(string bucketName, string password) { var bucketConfiguration = GetOrCreateConfiguration(bucketName); StartProvider(bucketName, password); var bucketConfig = GetBucketConfig(bucketName, password); try { ConfigLock.EnterWriteLock(); IConfigInfo configInfo = null; var nodes = bucketConfig.Nodes.ToList(); while (nodes.Any()) { try { nodes.Shuffle(); var node = nodes.First(); nodes.Remove(node); IBucketConfig newConfig; var uri = bucketConfig.GetTerseUri(node, bucketConfiguration.UseSsl); using (new SynchronizationContextExclusion()) { using ( var httpClient = new HttpClient(new AuthenticatingHttpClientHandler(bucketName, password))) { var body = httpClient.GetStringAsync(uri).Result; body = body.Replace("$HOST", uri.Host); newConfig = JsonConvert.DeserializeObject <BucketConfig>(body); } } newConfig.Password = password; configInfo = CreateConfigInfo(newConfig); Configs[bucketName] = configInfo; break; } catch (AggregateException e) { Log.Error(e.InnerException); } catch (IOException e) { Log.Error(e); } } if (configInfo == null) { throw new BucketNotFoundException(); } return(configInfo); } finally { ConfigLock.ExitWriteLock(); } }
/// <summary> /// Starts the HTTP streaming connection to the Couchbase Server and gets the latest configuration for a SASL authenticated Bucket. /// </summary> /// <param name="bucketName">The name of the Couchbase Bucket.</param> /// <param name="password">The SASL password used to connect to the Bucket.</param> /// <returns>A <see cref="IConfigInfo"/> object representing the latest configuration.</returns> public override IConfigInfo GetConfig(string bucketName, string username, string password) { var bucketConfiguration = GetOrCreateConfiguration(bucketName); //if the client is using a password make sure the client configuration references it password = string.IsNullOrEmpty(password) ? bucketConfiguration.Password : password; if (string.IsNullOrEmpty(bucketConfiguration.Password)) { bucketConfiguration.Password = password; } StartProvider(bucketName, username, password); var bucketConfig = GetBucketConfig(bucketName, password, username); try { ConfigLock.EnterWriteLock(); IConfigInfo configInfo = null; var nodes = bucketConfig.Nodes.ToList(); while (nodes.Any()) { try { nodes.Shuffle(); var node = nodes.First(); nodes.Remove(node); IBucketConfig newConfig; using (new SynchronizationContextExclusion()) { using (var httpClient = new CouchbaseHttpClient(username, password, ClientConfig)) { // try to get config using terse uri var uri = bucketConfig.GetTerseUri(node, bucketConfiguration.UseSsl); var response = httpClient.GetAsync(uri).Result; if (!response.IsSuccessStatusCode) { // try to get config using verbose uri uri = bucketConfig.GetUri(node, bucketConfiguration.UseSsl); response = httpClient.GetAsync(uri).Result; } // if still not a success, let it throw response.EnsureSuccessStatusCode(); var body = response.Content.ReadAsStringAsync().Result; body = body.Replace("$HOST", uri.Host); newConfig = JsonConvert.DeserializeObject <BucketConfig>(body); } } if (string.IsNullOrWhiteSpace(newConfig.BucketType) && newConfig.BucketCapabilities != null) { newConfig.BucketType = (newConfig.BucketCapabilities.Contains("couchapi", StringComparer.OrdinalIgnoreCase) ? BucketTypeEnum.Couchbase : BucketTypeEnum.Ephemeral).ToString().ToLowerInvariant(); } newConfig.Password = password; newConfig.Username = username; if (ClientConfig.UseSsl) { foreach (var ipEndPoint in bucketConfig.VBucketServerMap.IPEndPoints) { ipEndPoint.Port = ClientConfig.SslPort; } } configInfo = CreateConfigInfo(newConfig, username, password); Configs[bucketName] = configInfo; break; } catch (AggregateException e) { Log.Error(e.InnerException); } catch (IOException e) { Log.Error(e); } } if (configInfo == null) { throw new BucketNotFoundException(); } return(configInfo); } finally { ConfigLock.ExitWriteLock(); } }
/// <summary> /// Starts the HTTP streaming connection to the Couchbase Server and gets the latest configuration for a SASL authenticated Bucket. /// </summary> /// <param name="bucketName">The name of the Couchbase Bucket.</param> /// <param name="password">The SASL password used to connect to the Bucket.</param> /// <returns>A <see cref="IConfigInfo"/> object representing the latest configuration.</returns> public override IConfigInfo GetConfig(string bucketName, string username, string password) { var bucketConfiguration = GetOrCreateConfiguration(bucketName); StartProvider(bucketName, password); var bucketConfig = GetBucketConfig(bucketName, password); try { ConfigLock.EnterWriteLock(); IConfigInfo configInfo = null; var nodes = bucketConfig.Nodes.ToList(); while (nodes.Any()) { try { nodes.Shuffle(); var node = nodes.First(); nodes.Remove(node); IBucketConfig newConfig; var uri = bucketConfig.GetTerseUri(node, bucketConfiguration.UseSsl); using (new SynchronizationContextExclusion()) { using (var httpClient = new CouchbaseHttpClient(username, password)) { var body = httpClient.GetStringAsync(uri).Result; body = body.Replace("$HOST", uri.Host); newConfig = JsonConvert.DeserializeObject <BucketConfig>(body); } } if (string.IsNullOrWhiteSpace(newConfig.BucketType) && newConfig.BucketCapabilities != null) { newConfig.BucketType = (newConfig.BucketCapabilities.Contains("couchapi", StringComparer.OrdinalIgnoreCase) ? BucketTypeEnum.Couchbase : BucketTypeEnum.Ephemeral).ToString().ToLowerInvariant(); } newConfig.Password = password; configInfo = CreateConfigInfo(newConfig); Configs[bucketName] = configInfo; break; } catch (AggregateException e) { Log.Error(e.InnerException); } catch (IOException e) { Log.Error(e); } } if (configInfo == null) { throw new BucketNotFoundException(); } return(configInfo); } finally { ConfigLock.ExitWriteLock(); } }