示例#1
0
            private async Task <InternalConnection> InternalConnectAsync(ZooKeeperConnectionInfo connectionInfo)
            {
                var watcher   = new ConnectionWatcher(connectionInfo.SessionTimeout);
                var zooKeeper = new ZooKeeper(
                    connectstring: connectionInfo.ConnectionString,
                    sessionTimeout: connectionInfo.SessionTimeout.InMilliseconds,
                    watcher: watcher
                    );

                using var timeoutSource       = new CancellationTokenSource(connectionInfo.ConnectTimeout.TimeSpan);
                using var timeoutRegistration = timeoutSource.Token.Register(
                          () => watcher.TaskCompletionSource.TrySetException(new TimeoutException($"Timed out connecting to ZooKeeper after {connectionInfo.ConnectTimeout.InMilliseconds}ms"))
                          );

                foreach (var authInfo in connectionInfo.AuthInfo)
                {
                    zooKeeper.addAuthInfo(authInfo.Scheme, authInfo.Auth.ToArray());
                }

                try
                {
                    await watcher.TaskCompletionSource.Task.ConfigureAwait(false);

                    return(new InternalConnection(zooKeeper, watcher));
                }
                catch
                {
                    // on failure, clean up the instance we created
                    try { await zooKeeper.closeAsync().ConfigureAwait(false); }
                    finally { watcher.Dispose(); }
                    throw;
                }
            }
示例#2
0
        public static ClientHolderState CreateActiveClientHolderState(ZooKeeperClientSettings settings)
        {
            var connectionString  = settings.ConnectionStringProvider();
            var connectionWatcher = new ConnectionWatcher(_ => {});

            var client = new Lazy <ZooKeeperNetExClient>(
                () =>
            {
                using (ExecutionContext.SuppressFlow())
                {
                    return(new ZooKeeperNetExClient(
                               connectionString,
                               settings.ToInnerConnectionTimeout(),
                               connectionWatcher,
                               settings.CanBeReadOnly));
                }
            },
                LazyThreadSafetyMode.ExecutionAndPublication);

            return(ClientHolderState.CreateActive(client, connectionWatcher, ConnectionState.Connected, connectionString, settings));
        }
示例#3
0
 public InternalConnection(ZooKeeper zooKeeper, ConnectionWatcher watcher)
 {
     this.ZooKeeper = zooKeeper;
     this._watcher  = watcher;
 }