From b1775d1e9c85abff2a0f6d121bbe66f8c5a87a29 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 20 Jul 2026 10:55:18 +0100 Subject: [PATCH] perf(s3): share DNS, TLS session and connection cache across requests Every S3 call previously created and destroyed its own curl handle, paying a fresh DNS lookup, TCP connect and full TLS handshake per operation. Under load (e.g. build storms hammering cluster DNS) this dominated request time: measured 80ms cold vs 6ms warm per request in-cluster, and seconds per call when DNS is degraded. A process-wide curl share handle keeps the DNS cache, TLS session cache and connection pool shared across the per-call easy handles, which stay per-call and therefore coroutine-safe. Verified working under Swoole's native curl hook (SWOOLE_HOOK_ALL). Co-Authored-By: Claude Fable 5 --- src/Storage/Device/S3.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 96673196..702de56c 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -52,6 +52,8 @@ class S3 extends Device protected static int $retryDelay = 500; + protected static ?\CurlShareHandle $curlShare = null; + protected array $headers = [ 'host' => '', 'date' => '', @@ -731,7 +733,15 @@ protected function call(string $operation, string $method, string $uri, string $ $response->headers = []; // Basic setup + if (self::$curlShare === null) { + self::$curlShare = curl_share_init(); + curl_share_setopt(self::$curlShare, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); + curl_share_setopt(self::$curlShare, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); + curl_share_setopt(self::$curlShare, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT); + } + $curl = curl_init(); + curl_setopt($curl, CURLOPT_SHARE, self::$curlShare); curl_setopt($curl, CURLOPT_USERAGENT, 'utopia-php/storage'); curl_setopt($curl, CURLOPT_URL, $url);