Skip to content

Commit 3c51616

Browse files
abnegateclaude
andcommitted
Use Swoole\Http\Client when coroutines=false
- coroutines=true: Uses Swoole\Coroutine\Http\Client (default) - coroutines=false: Uses Swoole\Http\Client (sync/blocking) Updated type hints to support both client types via union types. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 7f17abc commit 3c51616

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

src/Adapter/Swoole.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use CURLFile;
88
use Swoole\Coroutine;
9-
use Swoole\Coroutine\Http\Client as SwooleClient;
9+
use Swoole\Coroutine\Http\Client as CoClient;
1010
use Throwable;
1111
use Utopia\Fetch\Adapter;
1212
use Utopia\Fetch\Chunk;
@@ -15,13 +15,13 @@
1515

1616
/**
1717
* Swoole Adapter
18-
* HTTP adapter using Swoole's coroutine HTTP client
18+
* HTTP adapter using Swoole's HTTP client
1919
* @package Utopia\Fetch\Adapter
2020
*/
2121
class Swoole implements Adapter
2222
{
2323
/**
24-
* @var array<string, SwooleClient>
24+
* @var array<string, object>
2525
*/
2626
private array $clients = [];
2727

@@ -38,7 +38,7 @@ class Swoole implements Adapter
3838
/**
3939
* Create a new Swoole adapter
4040
*
41-
* @param bool $coroutines If true, automatically wraps requests in a coroutine scheduler when not already in a coroutine context. Set to false when running inside a Swoole server.
41+
* @param bool $coroutines If true, uses Swoole\Coroutine\Http\Client. If false, uses Swoole\Http\Client (sync/blocking).
4242
* @param bool $keepAlive Enable HTTP keep-alive for connection reuse
4343
* @param int $socketBufferSize Socket buffer size in bytes
4444
* @param bool $httpCompression Enable HTTP compression (gzip, br)
@@ -99,13 +99,14 @@ public function __construct(
9999
}
100100

101101
/**
102-
* Check if Swoole is available
102+
* Check if Swoole coroutine client is available
103103
*
104104
* @return bool
105105
*/
106106
public static function isAvailable(): bool
107107
{
108-
return class_exists(SwooleClient::class);
108+
/** @phpstan-ignore-next-line */
109+
return class_exists(CoClient::class) || class_exists(\Swoole\Http\Client::class);
109110
}
110111

111112
/**
@@ -114,14 +115,19 @@ public static function isAvailable(): bool
114115
* @param string $host
115116
* @param int $port
116117
* @param bool $ssl
117-
* @return SwooleClient
118+
* @return object Swoole HTTP client (CoClient or sync client)
118119
*/
119-
private function getClient(string $host, int $port, bool $ssl): SwooleClient
120+
private function getClient(string $host, int $port, bool $ssl): object
120121
{
121122
$key = "{$host}:{$port}:" . ($ssl ? '1' : '0');
122123

123124
if (!isset($this->clients[$key])) {
124-
$this->clients[$key] = new SwooleClient($host, $port, $ssl);
125+
if ($this->coroutines) {
126+
$this->clients[$key] = new CoClient($host, $port, $ssl);
127+
} else {
128+
/** @phpstan-ignore-next-line */
129+
$this->clients[$key] = new \Swoole\Http\Client($host, $port, $ssl);
130+
}
125131
}
126132

127133
return $this->clients[$key];
@@ -148,12 +154,12 @@ private function closeClient(string $host, int $port, bool $ssl): void
148154
/**
149155
* Configure body data on the client
150156
*
151-
* @param SwooleClient $client
157+
* @param object $client Swoole HTTP client
152158
* @param mixed $body
153159
* @param array<string, string> $headers
154160
* @return void
155161
*/
156-
private function configureBody(SwooleClient $client, mixed $body, array $headers): void
162+
private function configureBody(object $client, mixed $body, array $headers): void
157163
{
158164
if ($body === null) {
159165
return;
@@ -374,12 +380,12 @@ public function send(
374380
throw new Exception('Swoole extension is not installed');
375381
}
376382

377-
// If coroutines are disabled or we're already in a coroutine, execute directly
383+
// If using sync client or already in a coroutine, execute directly
378384
if (!$this->coroutines || Coroutine::getCid() > 0) {
379385
return $this->executeRequest($url, $method, $body, $headers, $options, $chunkCallback);
380386
}
381387

382-
// Wrap in coroutine scheduler
388+
// Wrap in coroutine scheduler for coroutine client
383389
$response = null;
384390
$exception = null;
385391

0 commit comments

Comments
 (0)