Sha256: b42fb969ea37b2016f15064126dc8ebd439fc5faf48ea653d7fca7ac188df13c

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

module EventMachine
  module Twitter
    module Reconnectors
      class ApplicationFailure

        START       = 10
        INCREMENTOR = 2

        MAX_RECONNECTS    = 10
        DEFAULT_RECONNECT = 0
        MAX_TIMEOUT       = 320

        attr_reader :reconnect_count
        attr_writer :reconnect_timeout

        def initialize(options = {})
          @reconnect_count    = options.delete(:reconnect_count) || DEFAULT_RECONNECT
          @reconnect_timeout  = options.delete(:reconnect_timeout) || START
        end

        def reconnect_timeout
          @reconnect_timeout
        end

        def increment
          if maximum_reconnects?
            raise EM::Twitter::ReconnectLimitError.new("#{@reconnect_count} Reconnects")
          end

          yield @reconnect_timeout if block_given?

          @reconnect_count += 1
          @reconnect_timeout *= INCREMENTOR
        end

        def reset
          @reconnect_timeout = START
          @reconnect_count = DEFAULT_RECONNECT
        end

        private

        def maximum_reconnects?
          @reconnect_count > MAX_RECONNECTS || @reconnect_timeout > MAX_TIMEOUT
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
em-twitter-0.2.1 lib/em-twitter/reconnectors/application_failure.rb
em-twitter-0.2.0 lib/em-twitter/reconnectors/application_failure.rb
em-twitter-0.1.4 lib/em-twitter/reconnectors/application_failure.rb
em-twitter-0.1.3 lib/em-twitter/reconnectors/application_failure.rb
em-twitter-0.1.2 lib/em-twitter/reconnectors/application_failure.rb