|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Advanced Retry Logic Example |
5 | 7 | * |
|
61 | 63 | echo "3. Custom Retry Conditions\n"; |
62 | 64 | echo str_repeat('-', 50)."\n"; |
63 | 65 |
|
| 66 | +$retryableStatusCodes = [408, 429, 500, 502, 503, 504]; |
| 67 | + |
64 | 68 | $transport3 = TransportBuilder::make() |
65 | 69 | ->withBaseUri('https://jsonplaceholder.typicode.com') |
66 | 70 | ->withRetries( |
67 | 71 | maxRetries: 5, |
68 | | - strategy: new ExponentialBackoffStrategy, |
| 72 | + strategy: new ExponentialBackoffStrategy(), |
69 | 73 | condition: RetryCondition::default() |
70 | | - ->onStatusCodes([408, 429, 500, 502, 503, 504]) // Retry on specific status codes |
| 74 | + ->when(function (\Throwable $exception, \Farzai\Transport\Retry\RetryContext $context) use ($retryableStatusCodes): bool { |
| 75 | + // Retry on specific status codes from HTTP exceptions |
| 76 | + if ($exception instanceof \Farzai\Transport\Exceptions\HttpException && $exception->hasResponse()) { |
| 77 | + return in_array($exception->getResponse()->getStatusCode(), $retryableStatusCodes, true); |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | + }) |
71 | 82 | ) |
72 | 83 | ->build(); |
73 | 84 |
|
|
83 | 94 | echo "4. Retry with Custom Condition Callback\n"; |
84 | 95 | echo str_repeat('-', 50)."\n"; |
85 | 96 |
|
86 | | -$transport4 = TransportBuilder::make() |
87 | | - ->withBaseUri('https://jsonplaceholder.typicode.com') |
88 | | - ->withRetries( |
89 | | - maxRetries: 3, |
90 | | - strategy: new ExponentialBackoffStrategy, |
91 | | - condition: RetryCondition::fromCallback( |
92 | | - function (\Throwable $exception, \Farzai\Transport\Retry\RetryContext $context): bool { |
93 | | - echo " Retry attempt {$context->attempt}/{$context->maxAttempts}\n"; |
94 | | - echo " Exception: {$exception->getMessage()}\n"; |
| 97 | +$customCondition = (new RetryCondition())->when( |
| 98 | + function (\Throwable $exception, \Farzai\Transport\Retry\RetryContext $context): bool { |
| 99 | + echo " Retry attempt {$context->attempt}/{$context->maxAttempts}\n"; |
| 100 | + echo " Exception: {$exception->getMessage()}\n"; |
95 | 101 |
|
96 | | - // Retry only on network errors or 5xx responses |
97 | | - if ($exception instanceof \Farzai\Transport\Exceptions\ServerException) { |
98 | | - echo " Decision: RETRY (Server error)\n\n"; |
| 102 | + // Retry only on network errors or 5xx responses |
| 103 | + if ($exception instanceof \Farzai\Transport\Exceptions\ServerException) { |
| 104 | + echo " Decision: RETRY (Server error)\n\n"; |
99 | 105 |
|
100 | | - return true; |
101 | | - } |
| 106 | + return true; |
| 107 | + } |
102 | 108 |
|
103 | | - if ($exception instanceof \Farzai\Transport\Exceptions\NetworkException) { |
104 | | - echo " Decision: RETRY (Network error)\n\n"; |
| 109 | + if ($exception instanceof \Farzai\Transport\Exceptions\NetworkException) { |
| 110 | + echo " Decision: RETRY (Network error)\n\n"; |
105 | 111 |
|
106 | | - return true; |
107 | | - } |
| 112 | + return true; |
| 113 | + } |
108 | 114 |
|
109 | | - echo " Decision: DO NOT RETRY\n\n"; |
| 115 | + echo " Decision: DO NOT RETRY\n\n"; |
110 | 116 |
|
111 | | - return false; |
112 | | - } |
113 | | - ) |
| 117 | + return false; |
| 118 | + } |
| 119 | +); |
| 120 | + |
| 121 | +$transport4 = TransportBuilder::make() |
| 122 | + ->withBaseUri('https://jsonplaceholder.typicode.com') |
| 123 | + ->withRetries( |
| 124 | + maxRetries: 3, |
| 125 | + strategy: new ExponentialBackoffStrategy(), |
| 126 | + condition: $customCondition |
114 | 127 | ) |
115 | 128 | ->build(); |
116 | 129 |
|
|
0 commit comments