From da2237950b9b4f58520733b1b6adae736242e07b Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 29 Jul 2025 22:58:13 +0300 Subject: [PATCH 1/2] Refactor Auth::encodeCredentials to remove utf8_encode and update Client constructor to conditionally use coroutines --- src/Auth.php | 2 +- src/Client.php | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index ca0aa59..eb24efa 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -283,6 +283,6 @@ private function generateCnonce(): string */ public static function encodeCredentials($username, $password): string { - return \md5(\utf8_encode($username . ':mongo:' . $password)); + return \md5($username . ':mongo:' . $password); } } diff --git a/src/Client.php b/src/Client.php index 164a700..014624c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -7,6 +7,9 @@ use Swoole\Client as SwooleClient; use Swoole\Coroutine\Client as CoroutineClient; use stdClass; +use Swoole\Coroutine; + + class Client { @@ -78,13 +81,25 @@ public function __construct( int $port, string $user, string $password, - bool $useCoroutine = true + bool $useCoroutine = false ) { $this->id = uniqid('utopia.mongo.client'); $this->database = $database; $this->host = $host; $this->port = $port; + // Only use coroutines if explicitly requested and we're in a coroutine context + if ($useCoroutine) { + try { + $cid = \Swoole\Coroutine::getCid(); + if ($cid === false || $cid === 0) { + $useCoroutine = false; + } + } catch (\Throwable $e) { + $useCoroutine = false; + } + } + $this->client = $useCoroutine ? new CoroutineClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP) : new SwooleClient(SWOOLE_SOCK_TCP | SWOOLE_KEEP); From cf12c216dc7bd380ca4ddc41973742846979ff8d Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 30 Jul 2025 15:11:56 +0300 Subject: [PATCH 2/2] Fix coroutine check in Client class to handle negative CIDs correctly --- src/Client.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 014624c..9f7ef2e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -9,8 +9,6 @@ use stdClass; use Swoole\Coroutine; - - class Client { /** @@ -92,7 +90,7 @@ public function __construct( if ($useCoroutine) { try { $cid = \Swoole\Coroutine::getCid(); - if ($cid === false || $cid === 0) { + if ($cid === false || $cid < 0) { $useCoroutine = false; } } catch (\Throwable $e) {