Skip to content

Commit

Permalink
wire setLogger() calls for registered HTTP clients
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Apr 19, 2024
1 parent 924e4d8 commit 3d39484
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,8 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
new Reference($name.'.uri_template.inner'),
new Reference('http_client.uri_template_expander', ContainerInterface::NULL_ON_INVALID_REFERENCE),
$defaultUriTemplateVars,
]);
])
->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]);

$container->registerAliasForArgument($name, HttpClientInterface::class);

Expand Down Expand Up @@ -2541,7 +2542,8 @@ private function registerThrottlingHttpClient(string $rateLimiter, string $name,
$container
->register($name.'.throttling', ThrottlingHttpClient::class)
->setDecoratedService($name, null, 15) // higher priority than RetryableHttpClient (10)
->setArguments([new Reference($name.'.throttling.inner'), new Reference($name.'.throttling.limiter')]);
->setArguments([new Reference($name.'.throttling.inner'), new Reference($name.'.throttling.limiter')])
->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]);
}

private function registerRetryableHttpClient(array $options, string $name, ContainerBuilder $container): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
service('http_client.uri_template_expander')->nullOnInvalid(),
abstract_arg('default vars'),
])
->call('setLogger', [service('logger')->ignoreOnInvalid()])

->set('http_client.uri_template_expander.guzzle', \Closure::class)
->factory([\Closure::class, 'fromCallable'])
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"symfony/form": "^6.4|^7.0",
"symfony/expression-language": "^6.4|^7.0",
"symfony/html-sanitizer": "^6.4|^7.0",
"symfony/http-client": "^6.4|^7.0",
"symfony/http-client": "^7.1",
"symfony/lock": "^6.4|^7.0",
"symfony/mailer": "^6.4|^7.0",
"symfony/messenger": "^6.4|^7.0",
Expand Down Expand Up @@ -84,7 +84,7 @@
"symfony/console": "<6.4",
"symfony/dotenv": "<6.4",
"symfony/dom-crawler": "<6.4",
"symfony/http-client": "<6.4",
"symfony/http-client": "<7.1",
"symfony/form": "<6.4",
"symfony/lock": "<6.4",
"symfony/mailer": "<6.4",
Expand Down
14 changes: 5 additions & 9 deletions src/Symfony/Component/HttpClient/RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class RetryableHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
class RetryableHttpClient implements HttpClientInterface, ResetInterface
{
use AsyncDecoratorTrait;

Expand All @@ -46,6 +46,10 @@ public function __construct(HttpClientInterface $client, ?RetryStrategyInterface
$this->strategy = $strategy ?? new GenericRetryStrategy();
$this->maxRetries = $maxRetries;
$this->logger = $logger;

if (null !== $logger && $this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}

public function withOptions(array $options): static
Expand Down Expand Up @@ -164,14 +168,6 @@ public function request(string $method, string $url, array $options = []): Respo
});
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}

private function getDelayFromHeader(array $headers): ?int
{
if (null !== $after = $headers['retry-after'][0] ?? null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function shouldRetry(AsyncContext $context, ?string $responseContent, ?Tr
} catch (TransportExceptionInterface $e) {
$this->assertSame('Could not resolve host "does.not.exists".', $e->getMessage());
}
$this->assertCount(2, $logger->logs);
$this->assertCount(5, $logger->logs);
$this->assertSame('Try #{count} after {delay}ms: Could not resolve host "does.not.exists".', $logger->logs[0]);
}

Expand Down

0 comments on commit 3d39484

Please sign in to comment.