22
33namespace Utopia \Proxy \Adapter \TCP ;
44
5- use Utopia \Platform \Service ;
6- use Utopia \Proxy \Adapter ;
7- use Utopia \Proxy \Service \TCP as TCPService ;
85use Swoole \Coroutine \Client ;
6+ use Utopia \Proxy \Adapter ;
7+ use Utopia \Proxy \Resolver ;
98
109/**
1110 * TCP Protocol Adapter (Swoole Implementation)
1413 *
1514 * Routing:
1615 * - Input: Database hostname extracted from SNI or startup message
17- * - Resolution: Provided by application via resolve action
16+ * - Resolution: Provided by Resolver implementation
1817 * - Output: Backend endpoint (IP:port)
1918 *
2019 * Performance:
2524 *
2625 * Example:
2726 * ```php
28- * $adapter = new TCP(port: 5432);
29- * $service = new \Utopia\Proxy\Service\TCP();
30- * $service->addAction('resolve', (new class extends \Utopia\Platform\Action {})
31- * ->callback(fn($hostname) => $myBackend->resolve($hostname)));
32- * $adapter->setService($service);
27+ * $resolver = new MyDatabaseResolver();
28+ * $adapter = new TCP($resolver, port: 5432);
3329 * ```
3430 */
3531class Swoole extends Adapter
3632{
37- protected function defaultService (): ?Service
38- {
39- return new TCPService ();
40- }
41-
4233 /** @var array<string, Client> */
4334 protected array $ backendConnections = [];
4435
4536 public function __construct (
37+ Resolver $ resolver ,
4638 protected int $ port
4739 ) {
48- parent ::__construct ();
40+ parent ::__construct ($ resolver );
4941 }
5042
5143 /**
5244 * Get adapter name
53- *
54- * @return string
5545 */
5646 public function getName (): string
5747 {
@@ -60,8 +50,6 @@ public function getName(): string
6050
6151 /**
6252 * Get protocol type
63- *
64- * @return string
6553 */
6654 public function getProtocol (): string
6755 {
@@ -70,8 +58,6 @@ public function getProtocol(): string
7058
7159 /**
7260 * Get adapter description
73- *
74- * @return string
7561 */
7662 public function getDescription (): string
7763 {
@@ -80,8 +66,6 @@ public function getDescription(): string
8066
8167 /**
8268 * Get listening port
83- *
84- * @return int
8569 */
8670 public function getPort (): int
8771 {
@@ -94,9 +78,6 @@ public function getPort(): int
9478 * For PostgreSQL: Extract from SNI or startup message
9579 * For MySQL: Extract from initial handshake
9680 *
97- * @param string $data
98- * @param int $fd
99- * @return string
10081 * @throws \Exception
10182 */
10283 public function parseDatabaseId (string $ data , int $ fd ): string
@@ -113,8 +94,6 @@ public function parseDatabaseId(string $data, int $fd): string
11394 *
11495 * Format: "database\0db-abc123\0"
11596 *
116- * @param string $data
117- * @return string
11897 * @throws \Exception
11998 */
12099 protected function parsePostgreSQLDatabaseId (string $ data ): string
@@ -170,8 +149,6 @@ protected function parsePostgreSQLDatabaseId(string $data): string
170149 *
171150 * For MySQL, we typically get the database from subsequent COM_INIT_DB packet
172151 *
173- * @param string $data
174- * @return string
175152 * @throws \Exception
176153 */
177154 protected function parseMySQLDatabaseId (string $ data ): string
@@ -224,9 +201,6 @@ protected function parseMySQLDatabaseId(string $data): string
224201 *
225202 * Performance: Reuses connections for same database
226203 *
227- * @param string $databaseId
228- * @param int $clientFd
229- * @return Client
230204 * @throws \Exception
231205 */
232206 public function getBackendConnection (string $ databaseId , int $ clientFd ): Client
@@ -242,12 +216,12 @@ public function getBackendConnection(string $databaseId, int $clientFd): Client
242216 $ result = $ this ->route ($ databaseId );
243217
244218 // Create new TCP connection to backend
245- [$ host , $ port ] = explode (': ' , $ result ->endpoint . ': ' . $ this ->port );
246- $ port = (int )$ port ;
219+ [$ host , $ port ] = explode (': ' , $ result ->endpoint . ': ' . $ this ->port );
220+ $ port = (int ) $ port ;
247221
248222 $ client = new Client (SWOOLE_SOCK_TCP );
249223
250- if (!$ client ->connect ($ host , $ port , 30 )) {
224+ if (! $ client ->connect ($ host , $ port , 30 )) {
251225 throw new \Exception ("Failed to connect to backend: {$ host }: {$ port }" );
252226 }
253227
@@ -258,10 +232,6 @@ public function getBackendConnection(string $databaseId, int $clientFd): Client
258232
259233 /**
260234 * Close backend connection
261- *
262- * @param string $databaseId
263- * @param int $clientFd
264- * @return void
265235 */
266236 public function closeBackendConnection (string $ databaseId , int $ clientFd ): void
267237 {
0 commit comments