Skip to content

Commit 5d11230

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 5d11230

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

src/Adapter/Swoole.php

Lines changed: 18 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, CoClient|\Swoole\Http\Client>
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,13 @@ 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+
return class_exists(CoClient::class) || class_exists(\Swoole\Http\Client::class);
109109
}
110110

111111
/**
@@ -114,14 +114,19 @@ public static function isAvailable(): bool
114114
* @param string $host
115115
* @param int $port
116116
* @param bool $ssl
117-
* @return SwooleClient
117+
* @return CoClient|\Swoole\Http\Client
118118
*/
119-
private function getClient(string $host, int $port, bool $ssl): SwooleClient
119+
private function getClient(string $host, int $port, bool $ssl): object
120120
{
121121
$key = "{$host}:{$port}:" . ($ssl ? '1' : '0');
122122

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

127132
return $this->clients[$key];
@@ -148,12 +153,12 @@ private function closeClient(string $host, int $port, bool $ssl): void
148153
/**
149154
* Configure body data on the client
150155
*
151-
* @param SwooleClient $client
156+
* @param CoClient|\Swoole\Http\Client $client
152157
* @param mixed $body
153158
* @param array<string, string> $headers
154159
* @return void
155160
*/
156-
private function configureBody(SwooleClient $client, mixed $body, array $headers): void
161+
private function configureBody(object $client, mixed $body, array $headers): void
157162
{
158163
if ($body === null) {
159164
return;
@@ -374,12 +379,12 @@ public function send(
374379
throw new Exception('Swoole extension is not installed');
375380
}
376381

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

382-
// Wrap in coroutine scheduler
387+
// Wrap in coroutine scheduler for coroutine client
383388
$response = null;
384389
$exception = null;
385390

0 commit comments

Comments
 (0)