Skip to content

Commit 923633a

Browse files
committed
Rename HttpCodeDecider and RetryHttpClient
1 parent 731b2d3 commit 923633a

File tree

11 files changed

+83
-122
lines changed

11 files changed

+83
-122
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bundle\FullStack;
1818
use Symfony\Component\Asset\Package;
1919
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20+
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
2021
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
2122
use Symfony\Component\Config\Definition\ConfigurationInterface;
2223
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -1471,43 +1472,7 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
14711472
->variableNode('md5')->end()
14721473
->end()
14731474
->end()
1474-
->arrayNode('retry_failed')
1475-
->fixXmlConfig('http_code')
1476-
->canBeEnabled()
1477-
->addDefaultsIfNotSet()
1478-
->beforeNormalization()
1479-
->always(function ($v) {
1480-
if (isset($v['backoff_service']) && (isset($v['delay']) || isset($v['multiplier']) || isset($v['max_delay']))) {
1481-
throw new \InvalidArgumentException('The "backoff_service" option cannot be used along with the "delay", "multiplier" or "max_delay" options.');
1482-
}
1483-
if (isset($v['decider_service']) && (isset($v['http_codes']))) {
1484-
throw new \InvalidArgumentException('The "decider_service" option cannot be used along with the "http_codes" options.');
1485-
}
1486-
1487-
return $v;
1488-
})
1489-
->end()
1490-
->children()
1491-
->scalarNode('backoff_service')->defaultNull()->info('service id to override the retry backoff entirely')->end()
1492-
->scalarNode('decider_service')->defaultNull()->info('service id to override the retry decider entirely')->end()
1493-
->arrayNode('http_codes')
1494-
->performNoDeepMerging()
1495-
->beforeNormalization()
1496-
->ifArray()
1497-
->then(function ($v) {
1498-
return array_filter(array_values($v));
1499-
})
1500-
->end()
1501-
->prototype('scalar')->end()
1502-
->info('A list of HTTP status code that triggers a retry')
1503-
->defaultValue([423, 425, 429, 500, 502, 503, 504, 507, 510])
1504-
->end()
1505-
->integerNode('max_retries')->defaultValue(3)->min(0)->end()
1506-
->integerNode('delay')->defaultValue(1000)->min(0)->info('Time in ms to delay (or the initial value when multiplier is used)')->end()
1507-
->floatNode('multiplier')->defaultValue(2)->min(1)->info('If greater than 1, delay will grow exponentially for each retry: this delay = (delay * (multiple ^ retries))')->end()
1508-
->integerNode('max_delay')->defaultValue(0)->min(0)->info('Max time in ms that a retry should ever be delayed (0 = infinite)')->end()
1509-
->end()
1510-
->end()
1475+
->append($this->addHttpClientRetrySection())
15111476
->end()
15121477
->end()
15131478
->scalarNode('mock_response_factory')
@@ -1650,43 +1615,7 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
16501615
->variableNode('md5')->end()
16511616
->end()
16521617
->end()
1653-
->arrayNode('retry_failed')
1654-
->fixXmlConfig('http_code')
1655-
->canBeEnabled()
1656-
->addDefaultsIfNotSet()
1657-
->beforeNormalization()
1658-
->always(function ($v) {
1659-
if (isset($v['backoff_service']) && (isset($v['delay']) || isset($v['multiplier']) || isset($v['max_delay']))) {
1660-
throw new \InvalidArgumentException('The "backoff_service" option cannot be used along with the "delay", "multiplier" or "max_delay" options.');
1661-
}
1662-
if (isset($v['decider_service']) && (isset($v['http_codes']))) {
1663-
throw new \InvalidArgumentException('The "decider_service" option cannot be used along with the "http_codes" options.');
1664-
}
1665-
1666-
return $v;
1667-
})
1668-
->end()
1669-
->children()
1670-
->scalarNode('backoff_service')->defaultNull()->info('service id to override the retry backoff entirely')->end()
1671-
->scalarNode('decider_service')->defaultNull()->info('service id to override the retry decider entirely')->end()
1672-
->arrayNode('http_codes')
1673-
->performNoDeepMerging()
1674-
->beforeNormalization()
1675-
->ifArray()
1676-
->then(function ($v) {
1677-
return array_filter(array_values($v));
1678-
})
1679-
->end()
1680-
->prototype('scalar')->end()
1681-
->info('A list of HTTP status code that triggers a retry')
1682-
->defaultValue([423, 425, 429, 500, 502, 503, 504, 507, 510])
1683-
->end()
1684-
->integerNode('max_retries')->defaultValue(3)->min(0)->end()
1685-
->integerNode('delay')->defaultValue(1000)->min(0)->info('Time in ms to delay (or the initial value when multiplier is used)')->end()
1686-
->floatNode('multiplier')->defaultValue(2)->min(1)->info('If greater than 1, delay will grow exponentially for each retry: this delay = (delay * (multiple ^ retries))')->end()
1687-
->integerNode('max_delay')->defaultValue(0)->min(0)->info('Max time in ms that a retry should ever be delayed (0 = infinite)')->end()
1688-
->end()
1689-
->end()
1618+
->append($this->addHttpClientRetrySection())
16901619
->end()
16911620
->end()
16921621
->end()
@@ -1696,6 +1625,50 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
16961625
;
16971626
}
16981627

1628+
private function addHttpClientRetrySection()
1629+
{
1630+
$root = new NodeBuilder();
1631+
1632+
return $root
1633+
->arrayNode('retry_failed')
1634+
->fixXmlConfig('http_code')
1635+
->canBeEnabled()
1636+
->addDefaultsIfNotSet()
1637+
->beforeNormalization()
1638+
->always(function ($v) {
1639+
if (isset($v['backoff_service']) && (isset($v['delay']) || isset($v['multiplier']) || isset($v['max_delay']))) {
1640+
throw new \InvalidArgumentException('The "backoff_service" option cannot be used along with the "delay", "multiplier" or "max_delay" options.');
1641+
}
1642+
if (isset($v['decider_service']) && (isset($v['http_codes']))) {
1643+
throw new \InvalidArgumentException('The "decider_service" option cannot be used along with the "http_codes" options.');
1644+
}
1645+
1646+
return $v;
1647+
})
1648+
->end()
1649+
->children()
1650+
->scalarNode('backoff_service')->defaultNull()->info('service id to override the retry backoff')->end()
1651+
->scalarNode('decider_service')->defaultNull()->info('service id to override the retry decider')->end()
1652+
->arrayNode('http_codes')
1653+
->performNoDeepMerging()
1654+
->beforeNormalization()
1655+
->ifArray()
1656+
->then(function ($v) {
1657+
return array_filter(array_values($v));
1658+
})
1659+
->end()
1660+
->prototype('integer')->end()
1661+
->info('A list of HTTP status code that triggers a retry')
1662+
->defaultValue([423, 425, 429, 500, 502, 503, 504, 507, 510])
1663+
->end()
1664+
->integerNode('max_retries')->defaultValue(3)->min(0)->end()
1665+
->integerNode('delay')->defaultValue(1000)->min(0)->info('Time in ms to delay (or the initial value when multiplier is used)')->end()
1666+
->floatNode('multiplier')->defaultValue(2)->min(1)->info('If greater than 1, delay will grow exponentially for each retry: (delay * (multiple ^ retries))')->end()
1667+
->integerNode('max_delay')->defaultValue(0)->min(0)->info('Max time in ms that a retry should ever be delayed (0 = infinite)')->end()
1668+
->end()
1669+
;
1670+
}
1671+
16991672
private function addMailerSection(ArrayNodeDefinition $rootNode)
17001673
{
17011674
$rootNode

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
use Symfony\Component\Form\FormTypeGuesserInterface;
6565
use Symfony\Component\Form\FormTypeInterface;
6666
use Symfony\Component\HttpClient\MockHttpClient;
67-
use Symfony\Component\HttpClient\RetryHttpClient;
67+
use Symfony\Component\HttpClient\RetryableHttpClient;
6868
use Symfony\Component\HttpClient\ScopingHttpClient;
6969
use Symfony\Component\HttpFoundation\Request;
7070
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
@@ -2048,8 +2048,8 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
20482048

20492049
private function registerHttpClientRetry(array $retryOptions, string $name, ContainerBuilder $container)
20502050
{
2051-
if (!class_exists(RetryHttpClient::class)) {
2052-
throw new LogicException('Retry failed request support cannot be enabled as the component is not installed in the right version. Try running "composer require symfony/http-client:^5.2".');
2051+
if (!class_exists(RetryableHttpClient::class)) {
2052+
throw new LogicException('Retry failed request support cannot be enabled as version 5.2+ of the HTTP Client component is required.');
20532053
}
20542054

20552055
if (null !== $retryOptions['backoff_service']) {
@@ -2069,7 +2069,7 @@ private function registerHttpClientRetry(array $retryOptions, string $name, Cont
20692069
$deciderReference = new Reference($retryOptions['decider_service']);
20702070
} else {
20712071
$retryServiceId = $name.'.retry.decider';
2072-
$retryDefinition = new ChildDefinition('http_client.retry.abstract_httpcode_decider');
2072+
$retryDefinition = new ChildDefinition('http_client.retry.abstract_httpstatuscode_decider');
20732073
$retryDefinition
20742074
->replaceArgument(0, $retryOptions['http_codes']);
20752075
$container->setDefinition($retryServiceId, $retryDefinition);
@@ -2078,15 +2078,9 @@ private function registerHttpClientRetry(array $retryOptions, string $name, Cont
20782078
}
20792079

20802080
$container
2081-
->register($name.'.retry', RetryHttpClient::class)
2082-
->setDecoratedService($name) // pass after TraceableHttpClient
2083-
->setArguments([
2084-
new Reference($name.'.retry.inner'),
2085-
$deciderReference,
2086-
$backoffReference,
2087-
$retryOptions['max_retries'],
2088-
new Reference('logger'),
2089-
])
2081+
->register($name.'.retry', RetryableHttpClient::class)
2082+
->setDecoratedService($name)
2083+
->setArguments([new Reference($name.'.retry.inner'), $deciderReference, $backoffReference, $retryOptions['max_retries'], new Reference('logger')])
20902084
->addTag('monolog.logger', ['channel' => 'http_client']);
20912085
}
20922086

src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
use Symfony\Component\HttpClient\HttplugClient;
1919
use Symfony\Component\HttpClient\Psr18Client;
2020
use Symfony\Component\HttpClient\Retry\ExponentialBackOff;
21-
use Symfony\Component\HttpClient\Retry\HttpCodeDecider;
22-
use Symfony\Component\HttpClient\Retry\HttpHeaderBackOff;
21+
use Symfony\Component\HttpClient\Retry\HttpStatusCodeDecider;
2322
use Symfony\Contracts\HttpClient\HttpClientInterface;
2423

2524
return static function (ContainerConfigurator $container) {
@@ -60,12 +59,7 @@
6059
abstract_arg('multiplier'),
6160
abstract_arg('max delay ms'),
6261
])
63-
->set('http_client.retry.abstract_httpheader_backoff', HttpHeaderBackOff::class)
64-
->abstract()
65-
->args([
66-
abstract_arg('fallback backOff'),
67-
])
68-
->set('http_client.retry.abstract_httpcode_decider', HttpCodeDecider::class)
62+
->set('http_client.retry.abstract_httpstatuscode_decider', HttpStatusCodeDecider::class)
6963
->abstract()
7064
->args([
7165
abstract_arg('http codes'),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
use Symfony\Component\DependencyInjection\Reference;
4343
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4444
use Symfony\Component\HttpClient\MockHttpClient;
45-
use Symfony\Component\HttpClient\RetryHttpClient;
45+
use Symfony\Component\HttpClient\RetryableHttpClient;
4646
use Symfony\Component\HttpClient\ScopingHttpClient;
4747
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
4848
use Symfony\Component\Messenger\Transport\TransportFactory;
@@ -1486,7 +1486,7 @@ public function testHttpClientOverrideDefaultOptions()
14861486

14871487
public function testHttpClientRetry()
14881488
{
1489-
if (!class_exists(RetryHttpClient::class)) {
1489+
if (!class_exists(RetryableHttpClient::class)) {
14901490
$this->expectException(LogicException::class);
14911491
}
14921492
$container = $this->createContainerFromFile('http_client_retry');
@@ -1497,7 +1497,7 @@ public function testHttpClientRetry()
14971497
$this->assertSame(0, $container->getDefinition('http_client.retry.exponential_backoff')->getArgument(2));
14981498
$this->assertSame(2, $container->getDefinition('http_client.retry')->getArgument(3));
14991499

1500-
$this->assertSame(RetryHttpClient::class, $container->getDefinition('foo.retry')->getClass());
1500+
$this->assertSame(RetryableHttpClient::class, $container->getDefinition('foo.retry')->getClass());
15011501
$this->assertSame(4, $container->getDefinition('foo.retry.exponential_backoff')->getArgument(1));
15021502
}
15031503

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CHANGELOG
1010
* added `MockResponse::getRequestMethod()` and `getRequestUrl()` to allow inspecting which request has been sent
1111
* added `EventSourceHttpClient` a Server-Sent events stream implementing the [EventSource specification](https://www.w3.org/TR/eventsource/#eventsource)
1212
* added option "extra.curl" to allow setting additional curl options in `CurlHttpClient`
13-
* added `RetryHttpClient` to automatically retry failed requests.
13+
* added `RetryableHttpClient` to automatically retry failed HTTP requests.
1414

1515
5.1.0
1616
-----

src/Symfony/Component/HttpClient/Retry/ExponentialBackOff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ final class ExponentialBackOff implements RetryBackOffInterface
4242
public function __construct(int $delayMilliseconds = 1000, float $multiplier = 2, int $maxDelayMilliseconds = 0)
4343
{
4444
if ($delayMilliseconds < 0) {
45-
throw new InvalidArgumentException(sprintf('Delay must be greater than or equal to zero: "%s" passed.', $delayMilliseconds));
45+
throw new InvalidArgumentException(sprintf('Delay must be greater than or equal to zero: "%s" given.', $delayMilliseconds));
4646
}
4747
$this->delayMilliseconds = $delayMilliseconds;
4848

4949
if ($multiplier < 1) {
50-
throw new InvalidArgumentException(sprintf('Multiplier must be greater than zero: "%s" passed.', $multiplier));
50+
throw new InvalidArgumentException(sprintf('Multiplier must be greater than zero: "%s" given.', $multiplier));
5151
}
5252
$this->multiplier = $multiplier;
5353

5454
if ($maxDelayMilliseconds < 0) {
55-
throw new InvalidArgumentException(sprintf('Max delay must be greater than or equal to zero: "%s" passed.', $maxDelayMilliseconds));
55+
throw new InvalidArgumentException(sprintf('Max delay must be greater than or equal to zero: "%s" given.', $maxDelayMilliseconds));
5656
}
5757
$this->maxDelayMilliseconds = $maxDelayMilliseconds;
5858
}

src/Symfony/Component/HttpClient/Retry/HttpCodeDecider.php renamed to src/Symfony/Component/HttpClient/Retry/HttpStatusCodeDecider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Jérémy Derussé <jeremy@derusse.com>
2121
*/
22-
final class HttpCodeDecider implements RetryDeciderInterface
22+
final class HttpStatusCodeDecider implements RetryDeciderInterface
2323
{
2424
private $statusCodes;
2525

src/Symfony/Component/HttpClient/RetryHttpClient.php renamed to src/Symfony/Component/HttpClient/RetryableHttpClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\HttpClient\Response\AsyncResponse;
1818
use Symfony\Component\HttpClient\Response\MockResponse;
1919
use Symfony\Component\HttpClient\Retry\ExponentialBackOff;
20-
use Symfony\Component\HttpClient\Retry\HttpCodeDecider;
20+
use Symfony\Component\HttpClient\Retry\HttpStatusCodeDecider;
2121
use Symfony\Component\HttpClient\Retry\RetryBackOffInterface;
2222
use Symfony\Component\HttpClient\Retry\RetryDeciderInterface;
2323
use Symfony\Contracts\HttpClient\ChunkInterface;
@@ -26,11 +26,11 @@
2626
use Symfony\Contracts\HttpClient\ResponseInterface;
2727

2828
/**
29-
* Automatically retries the failing requests.
29+
* Automatically retries failing HTTP requests.
3030
*
3131
* @author Jérémy Derussé <jeremy@derusse.com>
3232
*/
33-
class RetryHttpClient implements HttpClientInterface
33+
class RetryableHttpClient implements HttpClientInterface
3434
{
3535
use AsyncDecoratorTrait;
3636

@@ -45,7 +45,7 @@ class RetryHttpClient implements HttpClientInterface
4545
public function __construct(HttpClientInterface $client, RetryDeciderInterface $decider = null, RetryBackOffInterface $strategy = null, int $maxRetries = 3, LoggerInterface $logger = null)
4646
{
4747
$this->client = $client;
48-
$this->decider = $decider ?? new HttpCodeDecider();
48+
$this->decider = $decider ?? new HttpStatusCodeDecider();
4949
$this->strategy = $strategy ?? new ExponentialBackOff();
5050
$this->maxRetries = $maxRetries;
5151
$this->logger = $logger ?: new NullLogger();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy