Sha256: a23b6dbb18309eea9e61cd379849ecb3b167c38795032824fa4296506dd525b8

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module ZK
  module EventHandlerSubscription
    # Stealing some ideas from Celluloid, this event handler subscription
    # (basically, the wrapper around the user block), will spin up its own
    # thread for delivery, and use a queue. This gives us the basis for better
    # concurrency (event handlers run in parallel), but preserves the
    # underlying behavior that a single-event-thread ZK gives us, which is that
    # a single callback block is inherently serial. Without this, you have to
    # make sure your callbacks are either synchronized, or totally reentrant,
    # so that multiple threads could be calling your block safely (which is
    # really difficult, and annoying).
    #
    # Using this delivery mechanism means that the block still must not block
    # forever, however each event will "wait its turn" and all callbacks will
    # receive their events in the same order (which is what ZooKeeper
    # guarantees), just perhaps at different times.
    #
    class Actor < Base
      extend Forwardable

      def_delegators :@threaded_callback, :call

      def initialize(*a)
        super
        @threaded_callback = ThreadedCallback.new(@callback)
      end

      def unsubscribe
        @threaded_callback.shutdown
        super
      end

      def async?
        true
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zk-1.1.1 lib/zk/event_handler_subscription/actor.rb
zk-1.1.0 lib/zk/event_handler_subscription/actor.rb