diff --git a/README.md b/README.md index e03c6ba..4e21f79 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,9 @@ If you wish to define custom behaviour when request is rate-limited you can via $account_tx->setCooldownSeconds(10); //(optional) Override default number of tries when request is rate-limited (default is 3) $account_tx->setTries(5); +//(optional) Set http timeout to 3 seconds (default is 0 - eg no timeout) +// After 3 seconds method will throw \XRPLWin\XRPL\Exceptions\BadRequestException on timeout +$account_tx->setTimeout(3); //(optional) Define custom cooldown callback $account_tx->setCooldownHandler( diff --git a/src/Api/AbstractMethod.php b/src/Api/AbstractMethod.php index ad0f9f4..72cf6bd 100644 --- a/src/Api/AbstractMethod.php +++ b/src/Api/AbstractMethod.php @@ -28,6 +28,7 @@ abstract class AbstractMethod protected ?Closure $cooldown_callback = null; protected int $tries = 3; //how much times request is retried if rate limiting is reached protected int $tries_tracker = 0; //how much tries are executed so far + protected int $timeout = 0; //http timeout, 0 is no timeout - see guzzle docs protected ?Throwable $lastException; @@ -106,6 +107,7 @@ public function requestAsync(): PromiseInterface ->requestAsync('POST', $this->endpoint, [ 'http_errors' => true, 'connect_timeout' => 10, + 'timeout' => $this->timeout, 'body' => \json_encode($p), 'headers' => $this->client->getHeaders() ]); @@ -168,6 +170,7 @@ protected function sendOnce(bool $silent = false) ->request('POST', $this->endpoint, [ 'http_errors' => false, 'connect_timeout' => 10, + 'timeout' => $this->timeout, 'body' => \json_encode($p), 'headers' => $this->client->getHeaders() ]); @@ -389,6 +392,7 @@ protected function cooldown(int $current_try = 1): void /** * Sets how much times script will try to re-query Ledger in case of Rate limited response. * Default value is 5 seconds. + * @param int $tries * @return self */ public function setTries(int $tries): self @@ -399,4 +403,16 @@ public function setTries(int $tries): self $this->tries = $tries; return $this; } + + /** + * Set timeout value of http request, this value will be sent to guzzle http client. + * Default value is 0 + * @param int $seconds + * @return self + */ + public function setTimeout(int $seconds): self + { + $this->timeout = $seconds; + return $this; + } }