From 10fdb25f26d6a85f9a3df75e871a3de2cb6b76d3 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 27 Jul 2026 21:00:56 +0200 Subject: [PATCH] Add Resource::waiting($read, $connect) to set timeouts --- .../com/amazon/aws/ServiceEndpoint.class.php | 13 +++++++--- .../php/com/amazon/aws/api/Resource.class.php | 26 +++++++++++++++++-- .../aws/unittest/ResourceTest.class.php | 10 +++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/php/com/amazon/aws/ServiceEndpoint.class.php b/src/main/php/com/amazon/aws/ServiceEndpoint.class.php index 38a2557..12969f7 100755 --- a/src/main/php/com/amazon/aws/ServiceEndpoint.class.php +++ b/src/main/php/com/amazon/aws/ServiceEndpoint.class.php @@ -202,7 +202,7 @@ public function sign($target, int $expires= 3600, $time= null): string { * * @throws io.OperationFailed */ - public function open(string $method, $target, array $headers, $hash= null, $time= null): Transfer { + public function open(string $method, $target, array $headers, $hash= null, $time= null, $timeouts= [null, null]): Transfer { $signature= new SignatureV4($this->credentials()); list($path, $encoded, $params)= $this->target($signature, $target); @@ -210,6 +210,10 @@ public function open(string $method, $target, array $headers, $hash= null, $time $conn= ($this->connections)('https://'.$host.$encoded); $conn->setTrace($this->cat); + // Use request timeouts if supplied, otherwise use those of the connection + isset($timeouts[0]) && $conn->setTimeout($timeouts[0]); + isset($timeouts[1]) && $conn->setConnectTimeout($timeouts[1]); + // Create and sign request $request= $conn->create(new HttpRequest()); $request->setMethod($method); @@ -265,16 +269,17 @@ public function open(string $method, $target, array $headers, $hash= null, $time * * @throws io.OperationFailed */ - public function request(string $method, $target, array $headers= [], $payload= null, $time= null): Response { + public function request(string $method, $target, array $headers= [], $payload= null, $time= null, $timeouts= [null, null]): Response { if (null === $payload) { - $transfer= $this->open($method, $target, $headers + ['Content-Length' => 0], SignatureV4::NO_PAYLOAD, $time); + $transfer= $this->open($method, $target, $headers + ['Content-Length' => 0], SignatureV4::NO_PAYLOAD, $time, $timeouts); } else { $transfer= $this->open( $method, $target, $headers + ['Content-Length' => strlen($payload)], hash(SignatureV4::HASH, $payload), - $time + $time, + $timeouts ); $transfer->write($payload); } diff --git a/src/main/php/com/amazon/aws/api/Resource.class.php b/src/main/php/com/amazon/aws/api/Resource.class.php index dacdb4b..29f80f5 100755 --- a/src/main/php/com/amazon/aws/api/Resource.class.php +++ b/src/main/php/com/amazon/aws/api/Resource.class.php @@ -9,6 +9,7 @@ class Resource { private $endpoint, $marshalling; private $headers= []; + public $timeouts= [null, null]; public $target; /** @@ -48,6 +49,18 @@ public function __construct($endpoint, $path, $segments= [], $marshalling= null) } } + /** + * Sets timeouts for reading and connecting + * + * @param ?float $read + * @param ?float $connect + * @return self + */ + public function waiting($read= null, $connect= null) { + $this->timeouts= [$read, $connect]; + return $this; + } + /** * Adds headers * @@ -92,7 +105,9 @@ public function transmit($payload, $type= 'application/json', $method= 'POST') { $method, $this->target, ['Content-Type' => $type] + $this->headers, - $this->serialize($this->marshalling->marshal($payload), $type) + $this->serialize($this->marshalling->marshal($payload), $type), + null, + $this->timeouts ); } @@ -104,6 +119,13 @@ public function transmit($payload, $type= 'application/json', $method= 'POST') { * @return com.amazon.aws.api.Transfer */ public function open(string $method, array $headers= []) { - return $this->endpoint->open($method, $this->target, $headers + $this->headers); + return $this->endpoint->open( + $method, + $this->target, + $headers + $this->headers, + null, // hash + null, // time + $this->timeouts + ); } } \ No newline at end of file diff --git a/src/test/php/com/amazon/aws/unittest/ResourceTest.class.php b/src/test/php/com/amazon/aws/unittest/ResourceTest.class.php index 9a0b1c1..d0419b5 100755 --- a/src/test/php/com/amazon/aws/unittest/ResourceTest.class.php +++ b/src/test/php/com/amazon/aws/unittest/ResourceTest.class.php @@ -47,4 +47,14 @@ public function serialize_rfc1738($payload, $expected) { public function serialize_unknown() { (new Resource($this->endpoint, '/'))->serialize(null, 'unknown/mime'); } + + #[Test] + public function default_timeouts() { + Assert::equals([null, null], (new Resource($this->endpoint, '/'))->timeouts); + } + + #[Test] + public function override_timeouts() { + Assert::equals([30, 4], (new Resource($this->endpoint, '/'))->waiting(30, 4)->timeouts); + } } \ No newline at end of file