All Products
Search
Document Center

Alibaba Cloud SDK:Configure a retry mechanism

Last Updated:Jul 29, 2024

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

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.

    <?php
    
    require_once 'vendor/autoload.php';
    use AlibabaCloud\SDK\Ecs\V20140526\Ecs;
    use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
    use Darabonba\OpenApi\Models\Config;
    use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeRegionsRequest;
    
    class ProxyDemo
    {
    
        public static function main()
        {
            $config = new Config([
                "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                "endpoint" => "ecs.cn-beijing.aliyuncs.com", // <endpoint>
            ]);
            $client = new Ecs($config);
    
            // Create a RuntimeOptions instance and specify runtime parameters. The settings take effect only on the requests that use the RuntimeOptions instance.
            $runtime = new RuntimeOptions([
                "autoretry" => true, // Enable the automatic retry mechanism.
                "maxAttempts" => 3, // Specify the maximum number of automatic retries.
            ]);
            $describeRegionsRequest = new DescribeRegionsRequest([
                "regionId" => "cn-beijing"
            ]);
            $resp = $client->describeRegionsWithOptions($describeRegionsRequest, $runtime);
            // var_dump($resp);
        }
    }
    ProxyDemo::main();