如何在不编写代码的情况下在 amazon sqs 中实现指数退避
how to implement exponential backoff in amazon sqs without writing code
我有一个简单的任务需要第 3 方。
当请求到来时,我将其推送到 amazon sqs
队列,将其拉入 worker 并调用 3rd party
。如果超时,我想实施指数退避(2 秒后重试,然后 4 秒,然后 8 秒,然后......)
最大重试。
使用python
、boto -> sqs
我一直在寻找内置参数,以允许我使用尽可能少的代码(理想情况下,根本不需要代码)。
类似
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)
根据 AWS SDK docs,如果您使用官方 SDK 库之一,您可以免费获得指数退避。所以看起来你需要做的就是设置你的 max_attempts
计数,第一次尝试后的一切都会以指数方式退缩:
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')
我认为到 2021 年夏天“完全没有代码”是不可能的。但是有一篇很棒的博客 post 介绍了如何使用编码示例 https://aws.amazon.com/blogs/compute/using-amazon-sqs-dead-letter-queues-to-replay-messages/
我有一个简单的任务需要第 3 方。
当请求到来时,我将其推送到 amazon sqs
队列,将其拉入 worker 并调用 3rd party
。如果超时,我想实施指数退避(2 秒后重试,然后 4 秒,然后 8 秒,然后......)
最大重试。
使用python
、boto -> sqs
我一直在寻找内置参数,以允许我使用尽可能少的代码(理想情况下,根本不需要代码)。
类似
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)
根据 AWS SDK docs,如果您使用官方 SDK 库之一,您可以免费获得指数退避。所以看起来你需要做的就是设置你的 max_attempts
计数,第一次尝试后的一切都会以指数方式退缩:
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')
我认为到 2021 年夏天“完全没有代码”是不可能的。但是有一篇很棒的博客 post 介绍了如何使用编码示例 https://aws.amazon.com/blogs/compute/using-amazon-sqs-dead-letter-queues-to-replay-messages/