Sha256: 912b4daa96f3fedca15a6030ceae94c6213cd5745d163c553fd7017233332dd2

Contents?: true

Size: 1.97 KB

Versions: 203

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Aws
  module Plugins
    module Retries

      # @api private
      # Used in 'standard' and 'adaptive' retry modes.
      class RetryQuota
        INITIAL_RETRY_TOKENS = 500
        RETRY_COST = 5
        NO_RETRY_INCREMENT = 1
        TIMEOUT_RETRY_COST = 10

        def initialize(opts = {})
          @mutex              = Mutex.new
          @max_capacity       = opts.fetch(:max_capacity, INITIAL_RETRY_TOKENS)
          @available_capacity = @max_capacity
        end

        # check if there is sufficient capacity to retry
        # and return it.  If there is insufficient capacity
        # return 0
        # @return [Integer] The amount of capacity checked out
        def checkout_capacity(error_inspector)
          @mutex.synchronize do
            capacity_amount = if error_inspector.networking?
                                TIMEOUT_RETRY_COST
                              else
                                RETRY_COST
                              end

            # unable to acquire capacity
            return 0 if capacity_amount > @available_capacity

            @available_capacity -= capacity_amount
            capacity_amount
          end
        end

        # capacity_amount refers to the amount of capacity requested from
        # the last retry.  It can either be RETRY_COST, TIMEOUT_RETRY_COST,
        # or unset.
        def release(capacity_amount)
          # Implementation note:  The release() method is called for
          # every API call.  In the common case where the request is
          # successful and we're at full capacity, we can avoid locking.
          # We can't exceed max capacity so there's no work we have to do.
          return if @available_capacity == @max_capacity

          @mutex.synchronize do
            @available_capacity += capacity_amount || NO_RETRY_INCREMENT
            @available_capacity = [@available_capacity, @max_capacity].min
          end
        end
      end
    end
  end
end

Version data entries

203 entries across 203 versions & 1 rubygems

Version Path
aws-sdk-core-3.220.2 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.220.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.220.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.219.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.218.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.218.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.217.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.217.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.216.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.216.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.215.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.215.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.214.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.214.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.213.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.212.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.211.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.210.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.209.1 lib/aws-sdk-core/plugins/retries/retry_quota.rb
aws-sdk-core-3.209.0 lib/aws-sdk-core/plugins/retries/retry_quota.rb