public static async Task KillSession(long sessionId, byte[] sessionPassword, IObservable <ConnectionState> onConnectionStateChanged, string connectionString, TimeSpan timeout)
        {
            var zooKeeper = new ZooKeeperNetExClient(connectionString, 5000, null, sessionId, sessionPassword);

            try
            {
                var budged   = TimeBudget.StartNew(timeout);
                var observer = new WaitStateObserver(ConnectionState.Expired);
                onConnectionStateChanged.Subscribe(observer);

                while (!budged.HasExpired)
                {
                    if (zooKeeper.getState().Equals(ZooKeeperNetExClient.States.CONNECTED))
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }

                await zooKeeper.closeAsync().ConfigureAwait(false);

                if (await observer.Signal.Task.TryWaitAsync(budged.Remaining).ConfigureAwait(false))
                {
                    return;
                }

                throw new TimeoutException($"Expected to kill session within {timeout}, but failed to do so.");
            }
            finally
            {
                await zooKeeper.closeAsync().ConfigureAwait(false);
            }
        }
Example #2
0
 public static void Dispose(this ZooKeeperNetExClient client)
 {
     try
     {
         client?.closeAsync().Wait();
     }
     catch (Exception)
     {
         // ignored
     }
 }