全部產品
Search
文件中心

Alibaba Cloud SDK:逾時機制

更新時間:Jul 01, 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);
        }
    }