Set customBackoff for AWS SDK JavaScript V3 retries
Asked Answered
A

3

6

I just upgraded to AWS SDK V3 and I have no idea how to configure retryDelayOptions and customBackoff with it. I couldn't find any example code in AWS's own API reference or online. This is what I was doing previously:

retryDelayOptions: { customBackoff: (retryCount) => 2 ** (retryCount * 100) },
maxRetries: 2

I was passing the above as options into the client constructor. The retries seems to have changed quite a bit with V3 and I cannot make sense of the API without any examples. Any help is much appreciated

Regards, Deepak

Apprehension answered 10/3, 2022 at 18:21 Comment(1)
Note that ^ is a bitwise operator - I believe you intended to use **Wesle
A
3

I think I figured it out

const { StandardRetryStrategy } = require("@aws-sdk/middleware-retry");

module.exports = (maxAttempts)  => 
    new StandardRetryStrategy(async () => maxAttempts, {
    // eslint-disable-next-line no-bitwise
        delayDecider: (delayBase, attempts) => 2 ** (attempts * 100)
    })

and then you can pass this as retryStrategy

Apprehension answered 10/3, 2022 at 19:34 Comment(2)
Thanks! Unfortunately, the api is deprecated. You should now use @aws-sdk/util-retry instead. But looking at the docs there, it is not obvious how to configure custom backoff.Roguish
Note that ^ is a bitwise operator - I believe you intended to use **Wesle
G
2

As @philipp commented, the @aws-sdk/middleware-retry lib is deprecated, the way to set it up right now it is using @aws-sdk/util-retry lib.

To configure it can be found here

import { S3Client } from "@aws-sdk/client-s3";
import { ConfiguredRetryStrategy } from "@aws-sdk/util-retry";

const client = new S3Client({
  retryStrategy: new ConfiguredRetryStrategy(
    4, // max attempts.
    (attempt: number) => 100 + attempt * 1000 // backoff function.
  ),
});
Gardenia answered 17/5, 2023 at 1:31 Comment(0)
A
0

Updating the above 2 answers: @aws-sdk/util-retry is also deprecated. Use @smithy/util-retry instead.

import { S3Client } from "@aws-sdk/client-s3";
import { ConfiguredRetryStrategy } from "@smithy/util-retry";

const client = new S3Client({
  retryStrategy: new ConfiguredRetryStrategy(
    4, // max attempts.
    (attempt: number) => 100 + attempt * 1000 // backoff function.
  ),
});

Here is the docs

Acaleph answered 18/10 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.