All Products
Search
Document Center

Alibaba Cloud SDK:Configure a timeout period

Last Updated:Jul 09, 2024

This topic describes how to configure timeout periods in Alibaba Cloud SDK V2.0 for Node.js.

Configuration method

Note

The timeout periods configured in Alibaba Cloud SDK V2.0 for Node.js take effect in the following descending order: the timeout periods that you configure by using the RuntimeOptions object, the timeout periods that you configure by initiating the SDK client, and the default timeout periods.

  • The default timeout period for connection requests is 5,000 milliseconds, and the default timeout period for read requests is 10,000 milliseconds.

  • The following code provides examples on how to configure a timeout period by using the RuntimeOptions object.

    JavaScript

    const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = 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 an environment variable.
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // Obtain the AccessKey secret of the RAM user from an environment variable.
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // Specify the region that you want to access.
            regionId: 'cn-beijing',
        });
        const client = new Ecs20140526(config);
        const request = new ModifySecurityGroupRuleRequest({
            regionId: 'cn-beijing',
            securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
            securityGroupRuleId: 'sgr-2zec0dm6qi66XXXXXXXX',
            policy: 'accept',
            ipProtocol: 'tcp',
            portRange: '5004/5004',
        });
        // Create a RuntimeOptions instance and specify runtime parameters. 
        const runtime = new RuntimeOptions({
            // Configure a timeout period for connection requests.
            connectTimeout: 10000,
            // Configure a timeout period for read requests.
            readTimeout: 10000,
        });
        const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
        console.log(resp.headers);
        console.log(resp.body);
    }

    TypeScript

    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 an environment variable.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from an environment variable.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // Specify the region that you want to access.
                regionId: 'cn-beijing',
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
                regionId: 'cn-beijing',
                securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
                securityGroupRuleId: 'sgr-2zec0dm6qi66XXXXXXXX',
                policy: 'accept',
                ipProtocol: 'tcp',
                portRange: '5004/5004',
            });
            // Create a RuntimeOptions instance and specify runtime parameters. 
            const runtime = new $Util.RuntimeOptions({
                // Configure a timeout period for connection requests.
                connectTimeout: 10000,
                // Configure a timeout period for read requests.
                readTimeout: 10000,
            });
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }

  • The following code provides examples on how to configure a timeout period by using the Config object when you initialize the SDK client.

    JavaScript

    const { default: Ecs20140526, ModifySecurityGroupRuleRequest } = 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 RAM user from an environment variable.
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // Obtain the AccessKey secret of the RAM user from an environment variable.
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // Specify the region that you want to access.
            regionId: 'cn-beijing',
            // Configure a timeout period for connection requests.
            connectTimeout: 10000,
            // Configure a timeout period for read requests.
            readTimeout: 10000,
        });
        const client = new Ecs20140526(config);
        const request = new ModifySecurityGroupRuleRequest({
            regionId: 'cn-beijing',
            securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
            securityGroupRuleId: 'sgr-2zec0dm6qi66XXXXXXXX',
            policy: 'accept',
            ipProtocol: 'tcp',
            portRange: '5004/5004',
        });
        // Create a RuntimeOptions instance and specify runtime parameters. 
        const runtime = new RuntimeOptions();
        const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
        console.log(resp.headers);
        console.log(resp.body);
    }

    TypeScript

    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 an environment variable.
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // Obtain the AccessKey secret of the RAM user from an environment variable.
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // Specify the region that you want to access.
                regionId: 'cn-beijing',
                // Configure a timeout period for connection requests.
                connectTimeout: 10000,
                // Configure a timeout period for read requests.
                readTimeout: 10000,
            });
            const client = new Ecs20140526(config);
            const request = new $Ecs20140526.ModifySecurityGroupRuleRequest({
                regionId: 'cn-beijing',
                securityGroupId: 'sg-2zec0dm6qi66XXXXXXXX',
                securityGroupRuleId: 'sgr-2zec0dm6qi66XXXXXXXX',
                policy: 'accept',
                ipProtocol: 'tcp',
                portRange: '5004/5004',
            });
            // Create a RuntimeOptions instance and specify runtime parameters. 
            const runtime = new $Util.RuntimeOptions();
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }