Sha256: 5aa88c5b7ddd2899f82da2abe8001e1b47b500253036bc585b50a463765ac94f

Contents?: true

Size: 921 Bytes

Versions: 27

Compression:

Stored size: 921 Bytes

Contents

class Waiter
  def initialize(message = nil)
    @message = message || 'Waiting'
  end

  attr_accessor :message

  def wait(sleep_time, message: @message)
    while sleep_time > 0
      wait_time = [10, sleep_time].min
      puts "#{message} (#{sleep_time}s left)" if wait_time > 1
      sleep(wait_time)
      sleep_time -= wait_time
    end
  end
end

class RateLimitWaiter < Waiter
  def initialize(provider)
    super("Waiting on #{provider} rate-limit")
  end
end

class BackoffWaiter < Waiter
  def initialize(message, initial_delay:, multiplier:, max_delay: nil)
    super(message)

    @initial_delay = @current_delay = initial_delay
    @multiplier = multiplier
    @max_delay = max_delay
  end

  def reset
    @current_delay = @initial_delay
  end

  def wait(message: @message)
    super(@current_delay, message: message)
    @current_delay = [@current_delay * @multiplier, @max_delay].compact.min
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
record_store-8.0.5 lib/record_store/provider/provider_utils/waiter.rb
record_store-8.0.4 lib/record_store/provider/provider_utils/waiter.rb
record_store-8.0.3 lib/record_store/provider/provider_utils/waiter.rb
record_store-8.0.2 lib/record_store/provider/provider_utils/waiter.rb
record_store-8.0.1 lib/record_store/provider/provider_utils/waiter.rb
record_store-8.0.0 lib/record_store/provider/provider_utils/waiter.rb
record_store-7.1.1 lib/record_store/provider/provider_utils/waiter.rb
record_store-7.1.0 lib/record_store/provider/provider_utils/waiter.rb
record_store-7.0.1 lib/record_store/provider/provider_utils/waiter.rb
record_store-7.0.0 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.7.2 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.7.1 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.7.0 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.6.0 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.5.11 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.5.10 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.5.9 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.5.8 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.5.5 lib/record_store/provider/provider_utils/waiter.rb
record_store-6.5.4 lib/record_store/provider/provider_utils/waiter.rb