From 32530171eda79ddac523bd6c09760ae3cd01c35d Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Mon, 25 Nov 2024 17:44:55 +0100 Subject: [PATCH 1/3] bugfix(logger): memory consumption --- CHANGELOG.md | 4 ++++ src/Client/ClientImplementation.php | 28 ++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a72b14..fd18473 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### [1.2.2] - 2024-11-25 +### Fixed +- Memory consumption in api client - logger calls consumes memory, specially when large responses are logged + ## [1.2.1] - 2020-12-08 ### Fixed - default headers should overwrite build in headers diff --git a/src/Client/ClientImplementation.php b/src/Client/ClientImplementation.php index b2bac6c..d4be903 100644 --- a/src/Client/ClientImplementation.php +++ b/src/Client/ClientImplementation.php @@ -4,6 +4,7 @@ namespace WPDesk\ApiClient\Client; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use WPDesk\HttpClient\HttpClient; use WPDesk\HttpClient\HttpClientResponse; use WPDesk\ApiClient\Request\Request; @@ -36,6 +37,9 @@ class ClientImplementation implements Client, LoggerAwareInterface /** @var int */ private $timeout; + /** @var bool */ + private $is_logger_available = false; + /** * Client constructor. * @param HttpClient $client @@ -59,6 +63,7 @@ class ClientImplementation implements Client, LoggerAwareInterface $this->apiUrl = $apiUri; $this->defaultRequestHeaders = $defaultRequestHeaders; $this->timeout = $timeout; + $this->is_logger_available = ! $logger instanceof NullLogger; } /** @@ -70,8 +75,10 @@ class ClientImplementation implements Client, LoggerAwareInterface */ public function sendRequest(Request $request) { - $this->logger->debug("Sends request with METHOD: {$request->getMethod()}; to ENDPOINT {$request->getEndpoint()}", - $this->getLoggerContext()); + if ($this->is_logger_available) { + $this->logger->debug("Sends request with METHOD: {$request->getMethod()}; to ENDPOINT {$request->getEndpoint()}", + $this->getLoggerContext()); + } try { $httpResponse = $this->client->send( $fullUrl = $this->prepareFullUrl($request), @@ -81,11 +88,16 @@ class ClientImplementation implements Client, LoggerAwareInterface $this->timeout ); - $this->logger->debug( - "Sent request with: URL: {$fullUrl};\n METHOD: {$method};\n BODY: {$body};\n" - . "HEADERS: " . json_encode($headers) . "\n\n and got response as CODE: {$httpResponse->getResponseCode()};\n" - . "with RESPONSE BODY {$httpResponse->getBody()}", - $this->getLoggerContext()); + if ( $this->is_logger_available ) { + $this->logger->debug( + "Sent request with: URL: {$fullUrl};\n METHOD: {$method};\n BODY: {$body};\n" + ."HEADERS: ".json_encode( + $headers + )."\n\n and got response as CODE: {$httpResponse->getResponseCode()};\n" + ."with RESPONSE BODY {$httpResponse->getBody()}", + $this->getLoggerContext() + ); + } return $this->mapHttpResponseToApiResponse($httpResponse); @@ -218,4 +230,4 @@ class ClientImplementation implements Client, LoggerAwareInterface } -} \ No newline at end of file +} -- GitLab From 98c7fb8dad7d8b6dae379500eb03fe1bd8841129 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Mon, 25 Nov 2024 17:47:59 +0100 Subject: [PATCH 2/3] bugfix(logger): memory consumption --- src/Client/ClientImplementation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/ClientImplementation.php b/src/Client/ClientImplementation.php index d4be903..ad01188 100644 --- a/src/Client/ClientImplementation.php +++ b/src/Client/ClientImplementation.php @@ -63,7 +63,7 @@ class ClientImplementation implements Client, LoggerAwareInterface $this->apiUrl = $apiUri; $this->defaultRequestHeaders = $defaultRequestHeaders; $this->timeout = $timeout; - $this->is_logger_available = ! $logger instanceof NullLogger; + $this->is_logger_available = ! $logger instanceof NullLogger; } /** -- GitLab From 56f797801f0a03f5d9ddfcbdc9c01fdc8bf18d76 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Mon, 25 Nov 2024 17:48:53 +0100 Subject: [PATCH 3/3] bugfix(logger): memory consumption --- src/Client/ClientImplementation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client/ClientImplementation.php b/src/Client/ClientImplementation.php index ad01188..a749f92 100644 --- a/src/Client/ClientImplementation.php +++ b/src/Client/ClientImplementation.php @@ -88,7 +88,7 @@ class ClientImplementation implements Client, LoggerAwareInterface $this->timeout ); - if ( $this->is_logger_available ) { + if ($this->is_logger_available) { $this->logger->debug( "Sent request with: URL: {$fullUrl};\n METHOD: {$method};\n BODY: {$body};\n" ."HEADERS: ".json_encode( -- GitLab