zoukankan      html  css  js  c++  java
  • AWS 中的错误重试和指数退避 Error Retries and Exponential Backoff in AWS

    AWS 中的错误重试和指数退避

    Error Retries and Exponential Backoff in AWS

    Do some asynchronous operation.

    retries = 0

    DO
    wait for (2^retries * 100) milliseconds

    status = Get the result of the asynchronous operation.

    IF status = SUCCESS
    retry = false
    ELSE IF status = NOT_READY
    retry = true
    ELSE IF status = THROTTLED
    retry = true
    ELSE
    Some other error occurred, so stop calling the API.
    retry = false
    END IF

    retries = retries + 1

    WHILE (retry AND (retries < MAX_RETRIES))

    ===============================

    public enum Results {
    SUCCESS,
    NOT_READY,
    THROTTLED,
    SERVER_ERROR
    }

    /*
    * Performs an asynchronous operation, then polls for the result of the
    * operation using an incremental delay.
    */
    public static void doOperationAndWaitForResult() {

    try {
    // Do some asynchronous operation.
    long token = asyncOperation();

    int retries = 0;
    boolean retry = false;

    do {
    long waitTime = Math.min(getWaitTimeExp(retries), MAX_WAIT_INTERVAL);

    System.out.print(waitTime + " ");

    // Wait for the result.
    Thread.sleep(waitTime);

    // Get the result of the asynchronous operation.
    Results result = getAsyncOperationResult(token);

    if (Results.SUCCESS == result) {
    retry = false;
    } else if (Results.NOT_READY == result) {
    retry = true;
    } else if (Results.THROTTLED == result) {
    retry = true;
    } else if (Results.SERVER_ERROR == result) {
    retry = true;
    }
    else {
    // Some other error occurred, so stop calling the API.
    retry = false;
    }

    } while (retry && (retries++ < MAX_RETRIES));
    }

    catch (Exception ex) {
    }
    }

    /*
    * Returns the next wait interval, in milliseconds, using an exponential
    * backoff algorithm.
    */
    public static long getWaitTimeExp(int retryCount) {

    long waitTime = ((long) Math.pow(2, retryCount) * 100L);

    return waitTime;
    }

  • 相关阅读:
    国庆清北 Day5 T3 holyshit
    清北国庆Day4 T2 r
    清北国庆Day4 T2 y
    国庆清北Day4 DP 题目
    国庆清北 DP
    国庆清北 搜索
    国庆清北 图论
    国庆清北 数据结构
    python爬虫学习:网页采集器、豆瓣电影爬取、百度翻译
    入门爬虫-requests模块
  • 原文地址:https://www.cnblogs.com/cloudrivers/p/11328875.html
Copyright © 2011-2022 走看看