private Task OnStateChanged(WatchedEvent arg)
        {
            switch (arg.getState())
            {
            case Watcher.Event.KeeperState.Disconnected:
                _connectedEvent.Reset();
                break;

            case Watcher.Event.KeeperState.SyncConnected:
                _connectedEvent.Set();
                return(LoadAsync());

            case Watcher.Event.KeeperState.AuthFailed:
                //todo:throw custom exception.
                throw new Exception("connect to zookeeper auth failed");

            case Watcher.Event.KeeperState.ConnectedReadOnly:
                //we won't connect readonly when instantiate zookeeper.
                break;

            case Watcher.Event.KeeperState.Expired:
                _connectedEvent.Reset();
                _zooKeeper = _zooKeeperFactory.CreateZooKeeper(_option.ConnectionString, _option.SessionTimeout, out _watcher);
                break;

            default:
                break;
            }

            return(Task.CompletedTask);
        }
 /// <summary>
 /// Initializes a new instance.
 /// </summary>
 /// <param name="option">the zookeeper option.</param>
 /// <param name="zookeeperFactory">the zookeeper factory.</param>
 public ZookeeperConfigurationProvider(ZookeeperOption option, IZooKeeperFactory zookeeperFactory)
 {
     _option                = option;
     _zooKeeperFactory      = zookeeperFactory;
     _zooKeeper             = _zooKeeperFactory.CreateZooKeeper(_option.ConnectionString, _option.SessionTimeout, out _watcher);
     _watcher.StateChanged += OnStateChanged;
     _watcher.NodeChanged  += OnNodeChanged;
     _connectedEvent        = new ManualResetEvent(false);
 }
Example #3
0
        private async Task OnStateChanged(WatchedEvent arg)
        {
            switch (arg.getState())
            {
            case Watcher.Event.KeeperState.Disconnected:
                _connectedEvent.Reset();
                break;

            case Watcher.Event.KeeperState.SyncConnected:
                _connectedEvent.Set();
                try
                {
                    await LoadAsync();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
                finally
                {
                    _loadCompletedEvent.Set();
                }

                break;

            case Watcher.Event.KeeperState.AuthFailed:
                //todo:throw custom exception.
                throw new Exception("connect to zookeeper auth failed");

            case Watcher.Event.KeeperState.ConnectedReadOnly:
                //we won't connect readonly when instantiate zookeeper.
                break;

            case Watcher.Event.KeeperState.Expired:

                _connectedEvent.Reset();
                _loadCompletedEvent.Set();
                _zooKeeper             = _zooKeeperFactory.CreateZooKeeper(_option.ConnectionString, _option.SessionTimeout, _option.AuthInfo, out _watcher);
                _watcher.StateChanged += OnStateChanged;
                _watcher.NodeChanged  += OnNodeChanged;
                break;

            default:
                break;
            }
        }