全部产品
Search
文档中心

阿里云SDK:超时机制

更新时间:Jun 14, 2024

本文为您介绍V2.0 Node.js SDK的超时配置方式。

超时配置方式

说明

超时机制优先级为:RuntimeOption配置->Client设置 -> 默认,优先级依次降低。

  • 使用默认配置。默认连接超时为5000毫秒,读超时为10000毫秒。

  • 通过运行时参数(RuntimeOptions)配置超时时间。

    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({
            // 从环境变量中获取RAM用户的AccessKey ID
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // 从环境变量中获取RAM用户的AccessKey Secret
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // 访问的区域
            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',
        });
        // 创建RuntimeObject实例并设置运行参数。
        const runtime = new RuntimeOptions({
            // 设置链接超时时间
            connectTimeout: 10000,
            // 设置读取超时时间
            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({
                // 从环境变量中获取RAM用户的AccessKey ID
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // 从环境变量中获取RAM用户的AccessKey Secret
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // 访问的区域
                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',
            });
            // 创建RuntimeObject实例并设置运行参数。
            const runtime = new $Util.RuntimeOptions({
                // 设置链接超时时间
                connectTimeout: 10000,
                // 设置读取超时时间
                readTimeout: 10000,
            });
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }

  • 通过Config在初始化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({
            // 从环境变量中获取RAM用户的AccessKey ID
            accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
            // 从环境变量中获取RAM用户的AccessKey Secret
            accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
            // 访问的区域
            regionId: 'cn-beijing',
            // 设置链接超时时间
            connectTimeout: 10000,
            // 设置读取超时时间
            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',
        });
        // 创建RuntimeObject实例并设置运行参数。
        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({
                // 从环境变量中获取RAM用户的AccessKey ID
                accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
                // 从环境变量中获取RAM用户的AccessKey Secret
                accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET,
                // 访问的区域
                regionId: 'cn-beijing',
                // 设置链接超时时间
                connectTimeout: 10000,
                // 设置读取超时时间
                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',
            });
            // 创建RuntimeObject实例并设置运行参数。
            const runtime = new $Util.RuntimeOptions();
            const resp = await client.modifySecurityGroupRuleWithOptions(request, runtime);
            console.log(resp.headers);
            console.log(resp.body);
        }
    }