From a31122b432524d73e040ede85b16bd6369db7031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9F=8E=E9=93=AD?= <767036228@qq.com> Date: Fri, 12 Jul 2019 16:22:14 +0800 Subject: [PATCH 1/4] Optimized code --- src/utils/src/Functions.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/src/Functions.php b/src/utils/src/Functions.php index 24d90e33b..973a56b8d 100644 --- a/src/utils/src/Functions.php +++ b/src/utils/src/Functions.php @@ -72,15 +72,13 @@ if (! function_exists('retry')) { */ function retry($times, callable $callback, $sleep = 0) { - --$times; beginning: try { return $callback(); } catch (\Throwable $e) { - if ($times <= 0) { + if (--$times <= 0) { throw $e; } - --$times; if ($sleep) { usleep($sleep * 1000); } From 15256bcf6b4b47706115e64bd0702f53d006bbef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9F=8E=E9=93=AD?= <767036228@qq.com> Date: Fri, 12 Jul 2019 16:35:40 +0800 Subject: [PATCH 2/4] Updated retry times --- src/utils/src/Functions.php | 2 +- src/utils/tests/FunctionTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/utils/src/Functions.php b/src/utils/src/Functions.php index 973a56b8d..c278421e6 100644 --- a/src/utils/src/Functions.php +++ b/src/utils/src/Functions.php @@ -76,7 +76,7 @@ if (! function_exists('retry')) { try { return $callback(); } catch (\Throwable $e) { - if (--$times <= 0) { + if (--$times < 0) { throw $e; } if ($sleep) { diff --git a/src/utils/tests/FunctionTest.php b/src/utils/tests/FunctionTest.php index 9a7faced3..46ba8039c 100644 --- a/src/utils/tests/FunctionTest.php +++ b/src/utils/tests/FunctionTest.php @@ -58,6 +58,22 @@ class FunctionTest extends TestCase ++$result; throw new RetryException('Retry Test'); }); + } finally { + $this->assertSame(3, $result); + } + } + + /** + * @expectedException \HyperfTest\Utils\Exception\RetryException + */ + public function testOneTimesRetry() + { + $result = 0; + try { + retry(1, function () use (&$result) { + ++$result; + throw new RetryException('Retry Test'); + }); } finally { $this->assertSame(2, $result); } From a5a1ce17238d4d656333f6c1394ca9aa0a035c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9F=8E=E9=93=AD?= <767036228@qq.com> Date: Fri, 12 Jul 2019 17:08:46 +0800 Subject: [PATCH 3/4] Updated changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 181d03b76..709f1b066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - [#185](https://github.com/hyperf-cloud/hyperf/pull/185) Added support for xml format of response. +# Changed + +- [#195](https://github.com/hyperf-cloud/hyperf/pull/195) Optimized retry() function: Make the $times parameter more predictable + ## Fixed - [#176](https://github.com/hyperf-cloud/hyperf/pull/176) Fixed TypeError: Return value of LengthAwarePaginator::nextPageUrl() must be of the type string or null, none returned. From 26e4fb206f3c7cff176eb988b75b5a838147903a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Fri, 12 Jul 2019 17:20:42 +0800 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 709f1b066..f803d2151 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ # Changed -- [#195](https://github.com/hyperf-cloud/hyperf/pull/195) Optimized retry() function: Make the $times parameter more predictable +- [#195](https://github.com/hyperf-cloud/hyperf/pull/195) Changed the behavior of parameter `$times` of `retry()` function, means the retry times of the callable function. ## Fixed