Sha256: ad005f5ea99c736c3d77798bd5dbbc3c2d3dc9ef2794fa8d18c7ecf99f3f1ebf

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

module EventMachine
  module Twitter
    module Reconnectors
      class NetworkFailure

        START       = 0.25
        INCREMENTOR = 0.25

        MAX_RECONNECTS    = 10
        DEFAULT_RECONNECT = 0
        MAX_TIMEOUT       = 16

        attr_reader :reconnect_count
        attr_writer :reconnect_timeout

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

        def reconnect_timeout
          [@reconnect_timeout, MAX_TIMEOUT].min
        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/network_failure.rb
em-twitter-0.2.0 lib/em-twitter/reconnectors/network_failure.rb
em-twitter-0.1.4 lib/em-twitter/reconnectors/network_failure.rb
em-twitter-0.1.3 lib/em-twitter/reconnectors/network_failure.rb
em-twitter-0.1.2 lib/em-twitter/reconnectors/network_failure.rb