Module: ZK::Client::StateMixin

Included in:
Base
Defined in:
lib/z_k/client/state_mixin.rb

Overview

Provides client-state related methods. Included in ZK::Client::Base. (refactored out to this class to ease documentation overload)

Instance Method Summary collapse

Instance Method Details

#associating?bool

is the underlying connection is in the associating state?

Returns:

  • (bool)


13
14
15
# File 'lib/z_k/client/state_mixin.rb', line 13

def associating?
  wrap_state_closed_error { cnx and cnx.associating? }
end

#connected?Boolean

Returns true if the underlying connection is in the connected state.

Returns:

  • (Boolean)


7
8
9
# File 'lib/z_k/client/state_mixin.rb', line 7

def connected?
  wrap_state_closed_error { cnx and cnx.connected? }
end

#connecting?bool

is the underlying connection is in the connecting state?

Returns:

  • (bool)


19
20
21
# File 'lib/z_k/client/state_mixin.rb', line 19

def connecting?
  wrap_state_closed_error { cnx and cnx.connecting? }
end

#expired_session?bool

is the underlying connection is in the expired_session state?

Returns:

  • (bool)


25
26
27
28
29
30
31
32
33
# File 'lib/z_k/client/state_mixin.rb', line 25

def expired_session?
  return nil unless @cnx

  if defined?(::JRUBY_VERSION)
    cnx.state == Java::OrgApacheZookeeper::ZooKeeper::States::EXPIRED_SESSION
  else
    wrap_state_closed_error { cnx.state == Zookeeper::ZOO_EXPIRED_SESSION_STATE }
  end
end

#on_connecting(&block) ⇒ Object

register a block to be called when the client is attempting to reconnect to the zookeeper server. the documentation says that this state should be taken to mean that the application should enter into “safe mode” and operate conservatively, as it won’t be getting updates until it has reconnected



70
71
72
# File 'lib/z_k/client/state_mixin.rb', line 70

def on_connecting(&block)
  watcher.register_state_handler(:connecting, &block)
end

#on_expired_session(&block) ⇒ Object

TODO:

need to come up with a way to test this

register a block to be called when our session has expired. This usually happens due to a network partitioning event, and means that all callbacks and watches must be re-registered with the server



79
80
81
# File 'lib/z_k/client/state_mixin.rb', line 79

def on_expired_session(&block)
  watcher.register_state_handler(:expired_session, &block)
end

#stateObject

returns the current state of the connection as reported by the underlying driver as a symbol. The possible values are [:closed, :expired_session, :auth_failed :connecting, :connected, :associating].

See the Zookeeper session documentation for more information



43
44
45
46
47
48
49
# File 'lib/z_k/client/state_mixin.rb', line 43

def state
  if defined?(::JRUBY_VERSION) 
    cnx.state.to_string.downcase.to_sym
  else
    STATE_SYM_MAP.fetch(cnx.state) { |k| raise IndexError, "unrecognized state: #{k}" }
  end
end

#wrap_state_closed_errorObject (protected)



84
85
86
87
88
89
90
# File 'lib/z_k/client/state_mixin.rb', line 84

def wrap_state_closed_error
  yield
rescue RuntimeError => e
  # gah, lame error parsing here
  raise e unless e.message == 'zookeeper handle is closed'
  false
end