Skip to content

Commit 86ca764

Browse files
committed
Update adapters and servers to use Resolver
1 parent 19a370c commit 86ca764

10 files changed

Lines changed: 575 additions & 538 deletions

File tree

src/Adapter.php

Lines changed: 114 additions & 267 deletions
Large diffs are not rendered by default.

src/Adapter/HTTP/Swoole.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Utopia\Proxy\Adapter\HTTP;
44

5-
use Utopia\Platform\Service;
65
use Utopia\Proxy\Adapter;
7-
use Utopia\Proxy\Service\HTTP as HTTPService;
6+
use Utopia\Proxy\Resolver;
87

98
/**
109
* HTTP Protocol Adapter (Swoole Implementation)
@@ -13,7 +12,7 @@
1312
*
1413
* Routing:
1514
* - Input: Hostname (e.g., func-abc123.appwrite.network)
16-
* - Resolution: Provided by application via resolve action
15+
* - Resolution: Provided by Resolver implementation
1716
* - Output: Backend endpoint (IP:port)
1817
*
1918
* Performance:
@@ -24,24 +23,19 @@
2423
*
2524
* Example:
2625
* ```php
27-
* $service = new \Utopia\Proxy\Service\HTTP();
28-
* $service->addAction('resolve', (new class extends \Utopia\Platform\Action {})
29-
* ->callback(fn($hostname) => $myBackend->resolve($hostname)));
30-
* $adapter = new HTTP();
31-
* $adapter->setService($service);
26+
* $resolver = new MyFunctionResolver();
27+
* $adapter = new HTTP($resolver);
3228
* ```
3329
*/
3430
class Swoole extends Adapter
3531
{
36-
protected function defaultService(): ?Service
32+
public function __construct(Resolver $resolver)
3733
{
38-
return new HTTPService();
34+
parent::__construct($resolver);
3935
}
4036

4137
/**
4238
* Get adapter name
43-
*
44-
* @return string
4539
*/
4640
public function getName(): string
4741
{
@@ -50,8 +44,6 @@ public function getName(): string
5044

5145
/**
5246
* Get protocol type
53-
*
54-
* @return string
5547
*/
5648
public function getProtocol(): string
5749
{
@@ -60,8 +52,6 @@ public function getProtocol(): string
6052

6153
/**
6254
* Get adapter description
63-
*
64-
* @return string
6555
*/
6656
public function getDescription(): string
6757
{

src/Adapter/SMTP/Swoole.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Utopia\Proxy\Adapter\SMTP;
44

5-
use Utopia\Platform\Service;
65
use Utopia\Proxy\Adapter;
7-
use Utopia\Proxy\Service\SMTP as SMTPService;
6+
use Utopia\Proxy\Resolver;
87

98
/**
109
* SMTP Protocol Adapter (Swoole Implementation)
@@ -13,7 +12,7 @@
1312
*
1413
* Routing:
1514
* - Input: Email domain (e.g., tenant123.appwrite.io)
16-
* - Resolution: Provided by application via resolve action
15+
* - Resolution: Provided by Resolver implementation
1716
* - Output: Backend endpoint (IP:port)
1817
*
1918
* Performance:
@@ -23,24 +22,19 @@
2322
*
2423
* Example:
2524
* ```php
26-
* $adapter = new SMTP();
27-
* $service = new \Utopia\Proxy\Service\SMTP();
28-
* $service->addAction('resolve', (new class extends \Utopia\Platform\Action {})
29-
* ->callback(fn($domain) => $myBackend->resolve($domain)));
30-
* $adapter->setService($service);
25+
* $resolver = new MyEmailResolver();
26+
* $adapter = new SMTP($resolver);
3127
* ```
3228
*/
3329
class Swoole extends Adapter
3430
{
35-
protected function defaultService(): ?Service
31+
public function __construct(Resolver $resolver)
3632
{
37-
return new SMTPService();
33+
parent::__construct($resolver);
3834
}
3935

4036
/**
4137
* Get adapter name
42-
*
43-
* @return string
4438
*/
4539
public function getName(): string
4640
{
@@ -49,8 +43,6 @@ public function getName(): string
4943

5044
/**
5145
* Get protocol type
52-
*
53-
* @return string
5446
*/
5547
public function getProtocol(): string
5648
{
@@ -59,8 +51,6 @@ public function getProtocol(): string
5951

6052
/**
6153
* Get adapter description
62-
*
63-
* @return string
6454
*/
6555
public function getDescription(): string
6656
{

src/Adapter/TCP/Swoole.php

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Utopia\Proxy\Adapter\TCP;
44

5-
use Utopia\Platform\Service;
6-
use Utopia\Proxy\Adapter;
7-
use Utopia\Proxy\Service\TCP as TCPService;
85
use Swoole\Coroutine\Client;
6+
use Utopia\Proxy\Adapter;
7+
use Utopia\Proxy\Resolver;
98

109
/**
1110
* TCP Protocol Adapter (Swoole Implementation)
@@ -14,7 +13,7 @@
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:
@@ -25,33 +24,24 @@
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
*/
3531
class 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
{

src/ConnectionResult.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
*/
88
class ConnectionResult
99
{
10+
/**
11+
* @param array<string, mixed> $metadata
12+
*/
1013
public function __construct(
1114
public string $endpoint,
1215
public string $protocol,
1316
public array $metadata = []
14-
) {}
17+
) {
18+
}
1519
}

0 commit comments

Comments
 (0)