Throttling Requests with the Ruby aws-sdk

A common problem of late is throttling requests when using the ruby aws-sdk gem to access AWS services. Handling these exceptions is fairly trivial with a while loop like the following:

retry_count   = 0 
retry_success = 0 

while retry_success == 0
  retry_success = 1
  begin

    #
    # enter code to interact with AWS here
    #

  rescue Aws::APIGateway::Errors::TooManyRequestsException => tmre

  #
  # note that different AWS services have different exceptions
  # for this type of response, be sure to check your error output
  #

    sleep_time = ( 2 ** retry_count )
    retry_success = 0 
    sleep sleep_time
    retry_count = retry_count + 1 

  end
end

Note that there are different exceptions for different services that might indicate a throttling scenario so be sure to check the output received or the documentation around which exception to handle. Also note that additional exceptions should be handled around bad requests, missing, duplicate, unavailable, or mal-formed objects.

Leave a Reply

Your email address will not be published. Required fields are marked *