Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add logical timeout helper to RetrySettings #319

Merged
merged 4 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/RetrySettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,36 @@
* ],
* ]);
* ```
*
* Configure the use of logical timeout
* ------------------------------------
*
* To configure the use of a logical timeout, where a logical timeout is the
* duration a method is given to complete one or more RPC attempts, with each
* attempt using only the time remaining in the logical timeout, use
* {@see Google\ApiCore\RetrySettings::logicalTimeout()} combined with
* {@see Google\ApiCore\RetrySettings::with()}.
*
* ```
* $timeoutSettings = RetrySettings::logicalTimeout(30000);
*
* $customRetrySettings = $customRetrySettings->with($timeoutSettings);
*
* $result = $client->listGroups($name, [
* 'retrySettings' => $customRetrySettings
* ]);
* ```
*
* {@see Google\ApiCore\RetrySettings::logicalTimeout()} can also be used on a
* method call independent of a RetrySettings instance.
*
* ```
* $timeoutSettings = RetrySettings::logicalTimeout(30000);
*
* $result = $client->listGroups($name, [
* 'retrySettings' => $timeoutSettings
* ]);
* ```
*/
class RetrySettings
{
Expand Down Expand Up @@ -349,6 +379,26 @@ public function with(array $settings)
return new RetrySettings($settings + $existingSettings);
}

/**
* Creates an associative array of the {@see Google\ApiCore\RetrySettings} timeout fields configured
* with the given timeout specified in the $timeout parameter interpreted as a logical timeout.
*
* @param int $timeout {
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
* The timeout in milliseconds to be used as a logical call timeout.
* }
* @return array
*/
public static function logicalTimeout($timeout)
{
return [
'initialRpcTimeoutMillis' => $timeout,
'maxRpcTimeoutMillis' => $timeout,
'totalTimeoutMillis' => $timeout,
'noRetriesRpcTimeoutMillis' => $timeout,
'rpcTimeoutMultiplier' => 1.0
];
}

/**
* @return bool Returns true if retries are enabled, otherwise returns false.
*/
Expand Down
60 changes: 60 additions & 0 deletions tests/Tests/Unit/Middleware/RetryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,64 @@ public function testTimeoutMillisCallSettingsOverwrite()
$middleware($call, $options);
$this->assertTrue($handlerCalled);
}

public function testRetryLogicalTimeout()
{
$timeout = 2000;
$call = $this->getMock(Call::class, [], [], '', false);
$retrySettings = RetrySettings::constructDefault()
->with([
'retriesEnabled' => true,
'retryableCodes' => [ApiStatus::CANCELLED],
])
->with(RetrySettings::logicalTimeout($timeout));
$callCount = 0;
$observedTimeouts = [];
$handler = function(Call $call, $options) use (&$callCount, &$observedTimeouts) {
$callCount += 1;
$observedTimeouts[] = $options['timeoutMillis'];
return $promise = new Promise(function () use (&$promise, $callCount) {
if ($callCount < 3) {
throw new ApiException('Cancelled!', Code::CANCELLED, ApiStatus::CANCELLED);
}
$promise->resolve('Ok!');
});
};
$middleware = new RetryMiddleware($handler, $retrySettings);
$response = $middleware(
$call,
[]
)->wait();

$this->assertEquals('Ok!', $response);
$this->assertEquals(3, $callCount);
$this->assertEquals(3, count($observedTimeouts));
$this->assertEquals($observedTimeouts[0], $timeout);
for ($i = 1; $i < count($observedTimeouts); $i++) {
$this->assertTrue($observedTimeouts[$i-1] > $observedTimeouts[$i]);
}
}

public function testNoRetryLogicalTimeout()
{
$timeout = 2000;
$call = $this->getMock(Call::class, [], [], '', false);
$retrySettings = RetrySettings::constructDefault()
->with(RetrySettings::logicalTimeout($timeout));
$observedTimeout = 0;
$handler = function(Call $call, $options) use (&$observedTimeout) {
$observedTimeout = $options['timeoutMillis'];
return $promise = new Promise(function () use (&$promise) {
$promise->resolve('Ok!');
});
};
$middleware = new RetryMiddleware($handler, $retrySettings);
$response = $middleware(
$call,
[]
)->wait();

$this->assertEquals('Ok!', $response);
$this->assertEquals($observedTimeout, $timeout);
}
}
17 changes: 17 additions & 0 deletions tests/Tests/Unit/RetrySettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ public function testWith($settings, $withSettings, $expectedValues)
$this->compare($withRetrySettings, $expectedValues);
}

public function testLogicalTimeout()
{
$timeout = 10000;
$expectedValues = [
'initialRpcTimeoutMillis' => $timeout,
'maxRpcTimeoutMillis' => $timeout,
'totalTimeoutMillis' => $timeout,
'noRetriesRpcTimeoutMillis' => $timeout,
'rpcTimeoutMultiplier' => 1.0
];
$timeoutSettings = RetrySettings::logicalTimeout($timeout);
$this->assertSame(
$expectedValues,
$timeoutSettings
);
}

private function compare(RetrySettings $retrySettings, $expectedValues)
{
$this->assertSame(
Expand Down