Sha256: 002885063e0e4bc30f7a8ca5f533a0931a80a4858ed6f4aa252aadbce766d968

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

require 'rate_throttle_client'

module Ecoportal
  module API
    module Common
      class Client
        module RateThrottling
          private

          def rate_throttling(strategy: nil, &block)
            throttle(strategy).call(&block)
          end

          def throttle(strategy = nil)
            @throttle ||= {}

            return @throttle[strategy] if @throttle.key?(strategy)

            @throttle[strategy] = throttle_class(strategy).new
          end

          def throttle_class(strategy = default_throttle_strategy)
            case strategy
            when :gradual_decrease
              RateThrottleClient::ExponentialIncreaseGradualDecrease
            when :proportional_decrease
              RateThrottleClient::ExponentialIncreaseProportionalDecrease
            when :remaining_decrease
              # requires RateLimit-Remaining in the response
              RateThrottleClient::ExponentialIncreaseProportionalRemainingDecrease
            else # :backoff
              RateThrottleClient::ExponentialBackoff
            end
          end

          def default_throttle_strategy
            :proportional_decrease
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ecoportal-api-0.10.8 lib/ecoportal/api/common/client/rate_throttling.rb
ecoportal-api-0.10.7 lib/ecoportal/api/common/client/rate_throttling.rb
ecoportal-api-0.10.6 lib/ecoportal/api/common/client/rate_throttling.rb
ecoportal-api-0.10.5 lib/ecoportal/api/common/client/rate_throttling.rb
ecoportal-api-0.10.4 lib/ecoportal/api/common/client/rate_throttling.rb