11<?php
22
3- namespace Utopia \Proxy \Server \TCP ;
3+ namespace Utopia \Proxy \Server \TCP \ Swoole ;
44
5- use Swoole \Coroutine ;
5+ use Swoole \Coroutine as SwooleCoroutine ;
66use Swoole \Coroutine \Channel ;
77use Swoole \Coroutine \Server as CoroutineServer ;
88use Swoole \Coroutine \Server \Connection ;
99use Swoole \Coroutine \Socket ;
1010use Utopia \Console ;
1111use Utopia \Proxy \Adapter \TCP as TCPAdapter ;
1212use Utopia \Proxy \Resolver ;
13+ use Utopia \Proxy \Server \TCP \Config ;
14+ use Utopia \Proxy \Server \TCP \TLS ;
15+ use Utopia \Proxy \Server \TCP \TLSContext ;
1316
1417/**
1518 * High-performance TCP proxy server (Swoole Coroutine Implementation)
2730 * ```php
2831 * $tls = new TLS(certificate: '/certs/server.crt', key: '/certs/server.key');
2932 * $config = new Config(ports: [5432, 3306], tls: $tls);
30- * $server = new SwooleCoroutine ($config, $resolver);
33+ * $server = new Coroutine ($config, $resolver);
3134 * $server->start();
3235 * ```
3336 */
34- class SwooleCoroutine
37+ class Coroutine
3538{
3639 /** @var array<int, CoroutineServer> */
3740 protected array $ servers = [];
@@ -87,8 +90,7 @@ protected function initAdapters(): void
8790
8891 protected function configureServers (): void
8992 {
90- // Global coroutine settings
91- Coroutine::set ([
93+ SwooleCoroutine::set ([
9294 'max_coroutine ' => $ this ->config ->maxCoroutine ,
9395 'socket_buffer_size ' => $ this ->config ->socketBufferSize ,
9496 'log_level ' => $ this ->config ->logLevel ,
@@ -99,7 +101,6 @@ protected function configureServers(): void
99101 foreach ($ this ->config ->ports as $ port ) {
100102 $ server = new CoroutineServer ($ this ->config ->host , $ port , $ ssl , $ this ->config ->enableReusePort );
101103
102- // Only socket-protocol settings are applicable to Coroutine\Server
103104 $ settings = [
104105 'open_tcp_nodelay ' => true ,
105106 'open_tcp_keepalive ' => true ,
@@ -111,14 +112,12 @@ protected function configureServers(): void
111112 'buffer_output_size ' => $ this ->config ->bufferOutputSize ,
112113 ];
113114
114- // Apply TLS settings when enabled
115115 if ($ this ->tlsContext !== null ) {
116116 $ settings = array_merge ($ settings , $ this ->tlsContext ->toSwooleConfig ());
117117 }
118118
119119 $ server ->set ($ settings );
120120
121- // Coroutine\Server::start() already spawns a coroutine per connection
122121 $ server ->handle (function (Connection $ connection ) use ($ port ): void {
123122 $ this ->handleConnection ($ connection , $ port );
124123 });
@@ -265,13 +264,13 @@ public function start(): void
265264 }
266265 };
267266
268- if (Coroutine ::getCid () > 0 ) {
267+ if (SwooleCoroutine ::getCid () > 0 ) {
269268 $ runner ();
270269
271270 return ;
272271 }
273272
274- Coroutine \run ($ runner );
273+ SwooleCoroutine \run ($ runner );
275274 }
276275
277276 /**
@@ -285,7 +284,7 @@ public function getStats(): array
285284 }
286285
287286 /** @var array<string, mixed> $coroutineStats */
288- $ coroutineStats = Coroutine ::stats ();
287+ $ coroutineStats = SwooleCoroutine ::stats ();
289288
290289 return [
291290 'connections ' => 0 ,
0 commit comments