From 7ccff9f2dac248749ee1557b0e15a629319456ba Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Sat, 18 Nov 2023 18:40:38 +0000 Subject: [PATCH 1/2] Fix cavalcade runner memory usage leak We recently enabled `opcache.enable_cli` so Cavalcade Runner now has the opcache enabled. This is a bug, it turns out in the opcache whereby including files over and over again will lead to a memory leak. See https://github.com/php/php-src/issues/9812 for details. This bug causes a very large memory leak in Cavalcade Runner because we naively re-init the Cloudwatchclient on every call to put_metric_data() etc. So, it makes sense to only create the cloudwatch client once. THis should also drastically reduce the impact of the PHP bug. --- .../namespace.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/inc/cavalcade_runner_to_cloudwatch/namespace.php b/inc/cavalcade_runner_to_cloudwatch/namespace.php index 0efeb65b..4daed7d7 100644 --- a/inc/cavalcade_runner_to_cloudwatch/namespace.php +++ b/inc/cavalcade_runner_to_cloudwatch/namespace.php @@ -158,12 +158,17 @@ function ( $name, $value ) { * @return CloudWatchClient */ function cloudwatch_client() : CloudWatchClient { - return Altis\get_aws_sdk()->createCloudWatch( [ - 'version' => '2010-08-01', - 'http' => [ - 'synchronous' => false, - ], - ] ); + static $client; + if ( ! $client ) { + $client = Altis\get_aws_sdk()->createCloudWatch( [ + 'version' => '2010-08-01', + 'http' => [ + 'synchronous' => false, + ], + ] ); + } + + return $client; } /** From b0ea14aa432f9d649528a3baebe756a4104a07b3 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Sat, 18 Nov 2023 18:44:11 +0000 Subject: [PATCH 2/2] Do the same for logs client --- inc/cavalcade_runner_to_cloudwatch/namespace.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/inc/cavalcade_runner_to_cloudwatch/namespace.php b/inc/cavalcade_runner_to_cloudwatch/namespace.php index 4daed7d7..51159b02 100644 --- a/inc/cavalcade_runner_to_cloudwatch/namespace.php +++ b/inc/cavalcade_runner_to_cloudwatch/namespace.php @@ -177,12 +177,16 @@ function cloudwatch_client() : CloudWatchClient { * @return CloudWatchLogsClient */ function cloudwatch_logs_client() : CloudWatchLogsClient { - return Altis\get_aws_sdk()->createCloudWatchLogs( [ - 'version' => '2014-03-28', - 'http' => [ - 'synchronous' => true, - ], - ] ); + static $client; + if ( ! $client ) { + $client = Altis\get_aws_sdk()->createCloudWatchLogs( [ + 'version' => '2014-03-28', + 'http' => [ + 'synchronous' => true, + ], + ] ); + } + return $client; } /**