Skip to content

Commit dd74515

Browse files
Merge pull request #11 from utopia-php/feat-per-request-timeouts
feat: per request timeouts
2 parents 83986d1 + 5fd32e9 commit dd74515

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
vendor
22
*.cache
33
composer.lock
4-
state.json
4+
state.json

src/Client.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class Client
2525

2626
/** @var array<string, string> headers */
2727
private array $headers = [];
28-
private int $timeout = 15;
29-
private int $connectTimeout = 60;
28+
private int $timeout = 15000; // milliseconds (15 seconds)
29+
private int $connectTimeout = 60000; // milliseconds (60 seconds)
3030
private int $maxRedirects = 5;
3131
private bool $allowRedirects = true;
3232
private string $userAgent = '';
@@ -51,7 +51,7 @@ public function addHeader(string $key, string $value): self
5151
/**
5252
* Set the request timeout.
5353
*
54-
* @param int $timeout
54+
* @param int $timeout Timeout in milliseconds
5555
* @return self
5656
*/
5757
public function setTimeout(int $timeout): self
@@ -87,7 +87,7 @@ public function setMaxRedirects(int $maxRedirects): self
8787
/**
8888
* Set the connection timeout.
8989
*
90-
* @param int $connectTimeout
90+
* @param int $connectTimeout Timeout in milliseconds
9191
* @return self
9292
*/
9393
public function setConnectTimeout(int $connectTimeout): self
@@ -235,6 +235,8 @@ private function withRetries(callable $callback): mixed
235235
* @param array<string>|array<string, mixed> $body
236236
* @param array<string, mixed> $query
237237
* @param ?callable $chunks Optional callback function that receives a Chunk object
238+
* @param ?int $timeoutMs Optional request timeout in milliseconds
239+
* @param ?int $connectTimeoutMs Optional connection timeout in milliseconds
238240
* @return Response
239241
*/
240242
public function fetch(
@@ -243,6 +245,8 @@ public function fetch(
243245
?array $body = [],
244246
?array $query = [],
245247
?callable $chunks = null,
248+
?int $timeoutMs = null,
249+
?int $connectTimeoutMs = null,
246250
): Response {
247251
if (!in_array($method, [self::METHOD_PATCH, self::METHOD_GET, self::METHOD_CONNECT, self::METHOD_DELETE, self::METHOD_POST, self::METHOD_HEAD, self::METHOD_OPTIONS, self::METHOD_PUT, self::METHOD_TRACE])) {
248252
throw new Exception("Unsupported HTTP method");
@@ -297,8 +301,8 @@ public function fetch(
297301
}
298302
return strlen($data);
299303
},
300-
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
301-
CURLOPT_TIMEOUT => $this->timeout,
304+
CURLOPT_CONNECTTIMEOUT_MS => $connectTimeoutMs ?? $this->connectTimeout,
305+
CURLOPT_TIMEOUT_MS => $timeoutMs ?? $this->timeout,
302306
CURLOPT_MAXREDIRS => $this->maxRedirects,
303307
CURLOPT_FOLLOWLOCATION => $this->allowRedirects,
304308
CURLOPT_USERAGENT => $this->userAgent
@@ -339,7 +343,7 @@ public function fetch(
339343
}
340344

341345
/**
342-
* Get the request timeout.
346+
* Get the request timeout in milliseconds.
343347
*
344348
* @return int
345349
*/
@@ -369,7 +373,7 @@ public function getMaxRedirects(): int
369373
}
370374

371375
/**
372-
* Get the connection timeout.
376+
* Get the connection timeout in milliseconds.
373377
*
374378
* @return int
375379
*/

0 commit comments

Comments
 (0)