All Products
Search
Document Center

Alibaba Cloud SDK:Configure a retry mechanism

Last Updated:Jul 08, 2024

This topic describes how to configure a retry mechanism in Alibaba Cloud SDK V2.0 for Node.js.

Note

In Alibaba Cloud SDK V2.0, the API request processing logic includes the built-in retry logic for network exceptions. When a network exception occurs, the system automatically retries to reinitiate the request to ensure service stability and reliability. If an error is returned due to a business logic error, such as a parameter error or unavailable resources, the system performs no retries. In this case, you must adjust the request based on the corresponding error information instead of performing retries.

Methods

Note

The priority levels of methods that are used to configure a retry mechanism are listed in descending order: use RuntimeOptions and use the default retry settings.

  • We recommend that you use the default retry settings. By default, no retries are performed when exceptions occur. If you enable the automatic retry mechanism but do not specify the maximum number of retries, up to three retries are performed by default.

  • Configure a retry mechanism by using RuntimeOptions.

    JavaScript example

    const { default: Ecs20140526, ModifySecurityGroupRuleRequest, DescribeRegionsRequest } = require('@alicloud/ecs20140526');
    const { Config } = require('@alicloud/openapi-client');
    const { RuntimeOptions } = require('@alicloud/tea-util');
    
    async function main() {
        const config = new Config({
            // Obtain the AccessKey ID of the Resource Access Management (RAM) user from environment variables.
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // Obtain the AccessKey secret of the RAM user from environment variables.
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // The region ID.
            regionId: 'cn-beijing',
        });
        const client = new Ecs20140526(config);
        const request = new DescribeRegionsRequest();
        // Create a RuntimeObject instance and configure runtime parameters. 
        const runtime = new RuntimeOptions({
            // Enable the automatic retry mechanism.
            autoretry: true,
            // Specify the maximum number of automatic retries.
            maxAttempts: 3,
        });
        const resp = await client.describeRegionsWithOptions(request, runtime);
        console.log(resp.headers);
        console.log(resp.body);
    }
    
    main();

    TypeScript example

    import Ecs20140526, * as $Ecs20140526 from '@alicloud/ecs20140526';
    import * as $OpenApi from '@alicloud/openapi-client';
    import * as $Util from '@alicloud/tea-util';
    
    export default class Client {
        static async main(): Promise<void> {
            const config = new $OpenApi.Config({
                // Obtain the AccessKey ID of the RAM user from environment variables.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from environment variables.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // The region ID.
                regionId: 'cn-beijing',
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.DescribeRegionsRequest();
            // Create a RuntimeObject instance and configure runtime parameters. 
            const runtime = new $Util.RuntimeOptions({
                // Enable the automatic retry mechanism.
                autoretry: true,
                // Specify the maximum number of automatic retries.
                maxAttempts: 3,
            });
            const resp = await client.describeRegionsWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }