From e122fdc2f03b46c001b8de94363e3967f476d3bc Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 20 Sep 2014 13:58:31 +0200 Subject: [PATCH 1/6] allow providing an onError callable that is called on every error --- src/retry.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/retry.php b/src/retry.php index 17fc7dd..3a50db3 100644 --- a/src/retry.php +++ b/src/retry.php @@ -4,12 +4,15 @@ class FailingTooHardException extends \Exception {} -function retry($retries, callable $fn) +function retry($retries, callable $fn, callable $onError = null) { beginning: try { return $fn(); } catch (\Exception $e) { + if ($onError) { + $onError($e); + } if (!$retries) { throw new FailingTooHardException('', 0, $e); } From 368ab3144b5a70af4f2f7fa048a6473a0b8c60f7 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Guillaume Date: Fri, 31 Jan 2020 21:49:40 +0100 Subject: [PATCH 2/6] :tada: dummy commit with data : line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 3 0 E > RECV !0 1 RECV !1 2 RECV_INIT !2 null 6 3 > NOP 7 4 INIT_DYNAMIC_CALL !1 5 DO_FCALL 0 $4 6 > RETURN $4 7* JMP ->24 8 8 E > > CATCH last 'Exception' 9 9 > > JMPZ !2, ->13 10 10 > INIT_DYNAMIC_CALL !2 11 SEND_VAR_EX !3 12 DO_FCALL 0 12 13 > BOOL_XOR ~6 !0 14 > JMPZ ~6, ->21 13 15 > NEW $7 :18 16 SEND_VAL_EX '' 17 SEND_VAL_EX 0 18 SEND_VAR_EX !3 19 DO_FCALL 0 20 > THROW 0 $7 15 21 > POST_DEC ~9 !0 22 FREE ~9 16 23 > JMP ->3 18 24* > RETURN null --- src/retry.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/retry.php b/src/retry.php index 3a50db3..7603ce4 100644 --- a/src/retry.php +++ b/src/retry.php @@ -20,3 +20,4 @@ function retry($retries, callable $fn, callable $onError = null) goto beginning; } } + From 8f25d57b9083e62b22cc0383725a3884eec1a3c6 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Guillaume Date: Fri, 31 Jan 2020 21:50:51 +0100 Subject: [PATCH 3/6] :tada: we move the code outside of the catch block : line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 3 0 E > RECV !0 1 RECV !1 2 RECV_INIT !2 null 6 3 > NOP 7 4 INIT_DYNAMIC_CALL !1 5 DO_FCALL 0 $4 6 > RETURN $4 7* JMP ->9 8 8 E > > CATCH last 'Exception' 10 9 > > JMPZ !2, ->13 11 10 > INIT_DYNAMIC_CALL !2 11 SEND_VAR_EX !3 12 DO_FCALL 0 13 13 > BOOL_XOR ~6 !0 14 > JMPZ ~6, ->21 14 15 > NEW $7 :18 16 SEND_VAL_EX '' 17 SEND_VAL_EX 0 18 SEND_VAR_EX !3 19 DO_FCALL 0 20 > THROW 0 $7 16 21 > POST_DEC ~9 !0 22 FREE ~9 17 23 > JMP ->3 18 24* > RETURN null --- src/retry.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/retry.php b/src/retry.php index 7603ce4..296e9fe 100644 --- a/src/retry.php +++ b/src/retry.php @@ -9,15 +9,14 @@ function retry($retries, callable $fn, callable $onError = null) beginning: try { return $fn(); - } catch (\Exception $e) { - if ($onError) { - $onError($e); - } - if (!$retries) { - throw new FailingTooHardException('', 0, $e); - } - $retries--; - goto beginning; + } catch (\Exception $e) {} + if ($onError) { + $onError($e); } + if (!$retries) { + throw new FailingTooHardException('', 0, $e); + } + $retries--; + goto beginning; } From f403c4480ab34474a35097b74d1c451a04e0f5e8 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Guillaume Date: Fri, 31 Jan 2020 21:52:56 +0100 Subject: [PATCH 4/6] We replace "if (!$retries) { ... } $retries--" with "if (!$retries--) {...}" because postfix operation returns a copy of the previous value line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > RECV !0 1 RECV !1 2 RECV_INIT !2 null 5 3 > NOP 6 4 INIT_DYNAMIC_CALL !1 5 DO_FCALL 0 $4 6 > RETURN $4 7* JMP ->9 7 8 E > > CATCH last 'Exception' 8 9 > > JMPZ !2, ->13 9 10 > INIT_DYNAMIC_CALL !2 11 SEND_VAR_EX !3 12 DO_FCALL 0 11 13 > POST_DEC ~6 !0 14 BOOL_XOR ~7 ~6 15 > JMPZ ~7, ->22 12 16 > NEW $8 :14 17 SEND_VAL_EX '' 18 SEND_VAL_EX 0 19 SEND_VAR_EX !3 20 DO_FCALL 0 21 > THROW 0 $8 14 22 > > JMP ->3 15 23* > RETURN null --- src/retry.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/retry.php b/src/retry.php index 296e9fe..a949f65 100644 --- a/src/retry.php +++ b/src/retry.php @@ -13,10 +13,9 @@ function retry($retries, callable $fn, callable $onError = null) if ($onError) { $onError($e); } - if (!$retries) { + if (!$retries--) { throw new FailingTooHardException('', 0, $e); } - $retries--; goto beginning; } From 72e0aa3c27e767176baeff5485bed08ae25a8fd4 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Guillaume Date: Fri, 31 Jan 2020 21:57:29 +0100 Subject: [PATCH 5/6] We invert the if condition, now if $retries is non-zero we goto beginning, and if we dont, we fail too hard. line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > RECV !0 1 RECV !1 2 RECV_INIT !2 null 5 3 > NOP 6 4 INIT_DYNAMIC_CALL !1 5 DO_FCALL 0 $4 6 > RETURN $4 7* JMP ->9 7 8 E > > CATCH last 'Exception' 8 9 > > JMPZ !2, ->13 9 10 > INIT_DYNAMIC_CALL !2 11 SEND_VAR_EX !3 12 DO_FCALL 0 11 13 > POST_DEC ~6 !0 14 > JMPZ ~6, ->16 12 15 > > JMP ->3 14 16 > NEW $7 :13 17 SEND_VAL_EX '' 18 SEND_VAL_EX 0 19 SEND_VAR_EX !3 20 DO_FCALL 0 21 > THROW 0 $7 15 22* > RETURN null --- src/retry.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/retry.php b/src/retry.php index a949f65..aa2e65e 100644 --- a/src/retry.php +++ b/src/retry.php @@ -13,9 +13,9 @@ function retry($retries, callable $fn, callable $onError = null) if ($onError) { $onError($e); } - if (!$retries--) { - throw new FailingTooHardException('', 0, $e); + if ($retries--) { + goto beginning; } - goto beginning; + throw new FailingTooHardException('', 0, $e); } From 281c0c2f93862a877dd03ed8b048c0e153e7e647 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Guillaume Date: Fri, 31 Jan 2020 21:59:20 +0100 Subject: [PATCH 6/6] We transform the 'if condition goto' which does two jumps into a do ... while condition, that does only one line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > RECV !0 1 RECV !1 2 RECV_INIT !2 null 7 3 > INIT_DYNAMIC_CALL !1 4 DO_FCALL 0 $4 5 > RETURN $4 6* JMP ->8 8 7 E > > CATCH last 'Exception' 9 8 > > JMPZ !2, ->12 10 9 > INIT_DYNAMIC_CALL !2 10 SEND_VAR_EX !3 11 DO_FCALL 0 13 12 > POST_DEC ~6 !0 13 > JMPNZ ~6, ->3 14 14 > NEW $7 :12 15 SEND_VAL_EX '' 16 SEND_VAL_EX 0 17 SEND_VAR_EX !3 18 DO_FCALL 0 19 > THROW 0 $7 15 20* > RETURN null --- src/retry.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/retry.php b/src/retry.php index aa2e65e..49eede1 100644 --- a/src/retry.php +++ b/src/retry.php @@ -6,16 +6,16 @@ class FailingTooHardException extends \Exception {} function retry($retries, callable $fn, callable $onError = null) { - beginning: - try { - return $fn(); - } catch (\Exception $e) {} - if ($onError) { - $onError($e); - } - if ($retries--) { - goto beginning; + do + { + try { + return $fn(); + } catch (\Exception $e) {} + if ($onError) { + $onError($e); + } } + while ($retries--); throw new FailingTooHardException('', 0, $e); }