how to implement exponential backoff in amazon sqs without writing code

喜欢而已 提交于 2021-01-27 07:19:12

问题


I have a simple task that requires a 3rd party. When a request comes, I push it to an amazon sqs queue, pull it in a worker and call the 3rd party. In case of a time out, I want to implement an exponential backoff (try again in 2 secs, then 4 then 8, then...) with a max retry.

Using python, boto -> sqs

I've looked for built in parameters to allow me to do so with as little code as possible (ideally, no code at all).

Something like

from boto import sqs

def handle_message(message):
    try:
      # send a post to api
    except TimeOut, err:
      # send_back_to_itself in 2/4/8 sec
      if delay < DELAY_LIMIT:
          queue.write(message, delay=secs)

回答1:


According to the AWS SDK docs, you get exponential backoff for free if you're using one of the official SDK libraries. So it seems all you need to do is set your max_attempts count, and everything after the first attempt will back off exponentially:

import boto3
from botocore.config import Config

config = Config(
    retries = dict(
        max_attempts = 10   # docs say default is 5
    )
)

sqs = boto3.client('sqs', config=config)
sqs.receive_message(QueueUrl='https://sqs.us-east-1.amazonaws.com/<your-account-num>/foobar')


来源:https://stackoverflow.com/questions/48138094/how-to-implement-exponential-backoff-in-amazon-sqs-without-writing-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!