From f9ce5e95939b79e7d5561a2bc2dab27c47b69069 Mon Sep 17 00:00:00 2001 From: Don Hardman Date: Thu, 12 Mar 2026 18:21:48 +0300 Subject: [PATCH 1/2] feat(client): add delegated user context for daemon requests --- src/ManticoreSearch/Client.php | 81 +++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) mode change 100755 => 100644 src/ManticoreSearch/Client.php diff --git a/src/ManticoreSearch/Client.php b/src/ManticoreSearch/Client.php old mode 100755 new mode 100644 index 059d281..da43ac6 --- a/src/ManticoreSearch/Client.php +++ b/src/ManticoreSearch/Client.php @@ -51,6 +51,9 @@ class Client { /** @var ?string $authToken */ protected ?string $authToken = null; + /** @var ?string $delegatedUser */ + protected ?string $delegatedUser = null; + /** @var string $buddyVersion */ protected string $buddyVersion; @@ -90,6 +93,21 @@ function () { $this->clientMap = new Map; } + /** + * Ensure request-scoped cloned clients do not share mutable internals. + * @return void + */ + public function __clone() { + $this->connectionPool = new ConnectionPool( + function () { + $client = new HttpClient($this->host, $this->port); + $client->set(['timeout' => -1]); + return $client; + } + ); + $this->clientMap = new Map; + } + /** * Set server URL of Manticore searchd to send requests to * @param string $url it supports http:// prefixed and not @@ -121,6 +139,30 @@ public function getServerUrl(): string { return static::URL_PREFIX . $this->host . ':' . $this->port; } + /** + * Set delegated user context for outgoing daemon requests. + * + * Context is coroutine-scoped when running in coroutine mode. + * In sync mode it falls back to the instance-local value. + * + * @param ?string $user + * @return static + */ + public function setDelegatedUser(?string $user): static { + $normalizedUser = $this->normalizeDelegatedUser($user); + $this->delegatedUser = $normalizedUser; + + return $this; + } + + /** + * Clear delegated user context from current request scope. + * @return static + */ + public function clearDelegatedUser(): static { + return $this->setDelegatedUser(null); + } + /** * Send the request where request represents the SQL query to be send * @param string $request @@ -134,7 +176,7 @@ public function sendRequest( string $request, ?string $path = null, bool $disableAgentHeader = false, - string $requestMethod = 'POST' + string $requestMethod = 'POST', ): Response { $t = microtime(true); if ($request === '') { @@ -168,6 +210,15 @@ public function sendRequest( 'Content-Type' => $contentTypeHeader, 'User-Agent' => $userAgentHeader, ]; + + $delegatedUser = $this->resolveDelegatedUser(); + $delegatedUserLog = $delegatedUser ?? ''; + Buddy::debug("delegated user for daemon request on /{$path}: {$delegatedUserLog}"); + + if ($delegatedUser !== null) { + $headers['X-Manticore-User'] = $delegatedUser; + } + // Add authorization header if we have token if (isset($this->authToken)) { $headers['Authorization'] = "Bearer {$this->authToken}"; @@ -254,9 +305,37 @@ public function sendRequestToUrl( bool $disableAgentHeader = false ): Response { $client = $this->getClientForUrl($url); + + $delegatedUser = $this->resolveDelegatedUser(); + if ($delegatedUser === null) { + $client->clearDelegatedUser(); + } else { + $client->setDelegatedUser($delegatedUser); + } + return $client->sendRequest($request, $path, $disableAgentHeader); } + /** + * @param ?string $user + * @return ?string + */ + protected function normalizeDelegatedUser(?string $user): ?string { + if ($user === null) { + return null; + } + + $user = trim($user); + return $user === '' ? null : $user; + } + + /** + * @return ?string + */ + protected function resolveDelegatedUser(): ?string { + return $this->delegatedUser; + } + /** * @param string $url * @return Client From 589139f65e4f592ebd307e1a7ce1e507e27a1cdd Mon Sep 17 00:00:00 2001 From: Don Hardman Date: Thu, 12 Mar 2026 18:26:04 +0300 Subject: [PATCH 2/2] refactor(client): simplify delegated user normalization logic --- src/ManticoreSearch/Client.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/ManticoreSearch/Client.php b/src/ManticoreSearch/Client.php index da43ac6..957e6c2 100644 --- a/src/ManticoreSearch/Client.php +++ b/src/ManticoreSearch/Client.php @@ -149,9 +149,9 @@ public function getServerUrl(): string { * @return static */ public function setDelegatedUser(?string $user): static { - $normalizedUser = $this->normalizeDelegatedUser($user); - $this->delegatedUser = $normalizedUser; - + $normalizedUser = $user ? trim($user) : null; + // Empty string or null should be null + $this->delegatedUser = $normalizedUser ?: null; return $this; } @@ -316,19 +316,6 @@ public function sendRequestToUrl( return $client->sendRequest($request, $path, $disableAgentHeader); } - /** - * @param ?string $user - * @return ?string - */ - protected function normalizeDelegatedUser(?string $user): ?string { - if ($user === null) { - return null; - } - - $user = trim($user); - return $user === '' ? null : $user; - } - /** * @return ?string */