如何在不编写代码的情况下在Amazon sqs中实现指数补偿 [英] how to implement exponential backoff in amazon sqs without writing code

查看:132
本文介绍了如何在不编写代码的情况下在Amazon sqs中实现指数补偿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的任务,需要第三方参加.当请求到来时,我将其推送到 amazon sqs 队列中,将其拉入工作进程并致电第三方.如果超时,我想实现指数补偿(请在2秒内重试,然后再尝试4秒再尝试8秒,然后...)进行最大重试.

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.

使用 python boto->平方

我一直在寻找内置参数,以允许我使用尽可能少的代码(理想情况下,根本没有代码).

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

类似

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文档,如果您使用的是官方SDK库之一,则可以免费获得指数补偿.因此,您似乎所需要做的就是设置您的 max_attempts 计数,并且第一次尝试后的所有操作都会以指数形式退缩:

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')

这篇关于如何在不编写代码的情况下在Amazon sqs中实现指数补偿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆