Configure a timeout period
更新时间:
复制 MD 格式
Appropriate timeout periods prevent requests from waiting indefinitely, reduce resource consumption, and improve program reliability.
Configuration methods
Note
Timeout priority in descending order: request-level configuration, client-level configuration, and default settings.
-
Use the default settings. The default connection timeout is 5 seconds and the default read timeout is 10 seconds.
-
Configure a timeout period in a request.
<?php require_once 'vendor/autoload.php'; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\Ecs\Ecs; try { // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')) ->asDefaultClient(); $request = Ecs::v20140526()->describeRegions(); $result = $request ->scheme('https') ->version('2014-05-26') ->product('Ecs') ->action('DescribeRegions') ->regionId('cn-hangzhou') ->host("ecs.cn-hangzhou.aliyuncs.com") ->connectTimeout(5) // Set the timeout period for connection requests to 5 seconds. ->timeout(10) // Set the timeout period for read requests to 10 seconds. ->request(); print_r($result->toArray()); } catch (ClientException $exception) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. echo $exception->getMessage() . PHP_EOL; } catch (ServerException $exception) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. echo $exception->getMessage() . PHP_EOL; echo $exception->getErrorCode() . PHP_EOL; echo $exception->getRequestId() . PHP_EOL; echo $exception->getErrorMessage() . PHP_EOL; } -
Configure a timeout period in a client.
<?php require_once 'vendor/autoload.php'; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\Ecs\Ecs; try { // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')) ->asDefaultClient() ->connectTimeout(5) // Set the timeout period for connection requests to 5 seconds. ->timeout(10); // Set the timeout period for read requests to 10 seconds. $request = Ecs::v20140526()->describeRegions(); $result = $request ->scheme('https') ->version('2014-05-26') ->product('Ecs') ->action('DescribeRegions') ->regionId('cn-hangzhou') ->host("ecs.cn-hangzhou.aliyuncs.com") ->request(); print_r($result->toArray()); } catch (ClientException $exception) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. echo $exception->getMessage() . PHP_EOL; } catch (ServerException $exception) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. echo $exception->getMessage() . PHP_EOL; echo $exception->getErrorCode() . PHP_EOL; echo $exception->getRequestId() . PHP_EOL; echo $exception->getErrorMessage() . PHP_EOL; }
该文章对您有帮助吗?