From 1872e975dd84044636e149adb19bab14fa782c36 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 25 Aug 2026 19:45:13 +0100 Subject: [PATCH] refactor(geo): split transport into a pooled Geo\Client Mirror the cloud Geo\Client shape: a PSR-18 client over pooled Swoole coroutine connections with reuse, created once per worker and injected as 'geoClient'. The Geo service now only validates, caches, and builds records; a null client (empty _APP_GEO_ENDPOINT) disables lookups. Cloud can drop its own copy and reuse this class. Co-Authored-By: Claude Fable 5 --- app/init/resources.php | 8 ++++ app/init/resources/request.php | 8 ++-- src/Appwrite/Locale/Geo.php | 35 ++--------------- src/Appwrite/Locale/Geo/Client.php | 63 ++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 src/Appwrite/Locale/Geo/Client.php diff --git a/app/init/resources.php b/app/init/resources.php index 9403e53f31..714dc98bb8 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -18,6 +18,7 @@ use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; +use Appwrite\Locale\Geo\Client as GeoClient; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; use Appwrite\Screenshots\Client as ScreenshotsClient; use Appwrite\Usage\Connection as UsageConnection; @@ -361,6 +362,13 @@ function getDevice(string $root, string $connection = ''): Device } } +$container->set('geoClient', function () { + $endpoint = System::getEnv('_APP_GEO_ENDPOINT', ''); + $secret = System::getEnv('_APP_GEO_SECRET', ''); + + return empty($endpoint) || empty($secret) ? null : GeoClient::pooled($endpoint, $secret); +}, []); + $container->set('passwordsDictionary', fn ($register) => $register->get('passwordsDictionary'), ['register']); $container->set('servers', function () { diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 6b7456d229..c1f96e7ccd 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -16,6 +16,7 @@ use Appwrite\Extend\Exception; use Appwrite\Functions\EventProcessor; use Appwrite\GraphQL\Schema; use Appwrite\Locale\Geo; +use Appwrite\Locale\Geo\Client as GeoClient; use Appwrite\Locking\Lock; use Appwrite\Network\Cors; use Appwrite\Network\Platform; @@ -1214,10 +1215,9 @@ return function (Container $context): void { return new Agent($adapter); }, ['register']); - $context->set('geo', fn (Locale $locale, Table $geoRecords) => new Geo( - System::getEnv('_APP_GEO_ENDPOINT', ''), - System::getEnv('_APP_GEO_SECRET', ''), + $context->set('geo', fn (Locale $locale, ?GeoClient $geoClient, Table $geoRecords) => new Geo( + $geoClient, $locale, $geoRecords - ), ['locale', 'geoRecords']); + ), ['locale', 'geoClient', 'geoRecords']); }; diff --git a/src/Appwrite/Locale/Geo.php b/src/Appwrite/Locale/Geo.php index fbc6b391a0..4a0263bbd5 100644 --- a/src/Appwrite/Locale/Geo.php +++ b/src/Appwrite/Locale/Geo.php @@ -2,10 +2,10 @@ namespace Appwrite\Locale; +use Appwrite\Locale\Geo\Client; use Swoole\Table; use Utopia\Config\Config; use Utopia\Console; -use Utopia\Fetch\Client; use Utopia\Locale\Locale; class Geo @@ -14,8 +14,7 @@ class Geo public const CACHE_VALUE_SIZE = 512; public function __construct( - private string $endpoint, - private string $secret, + private ?Client $client, private Locale $locale, private ?Table $cache = null, ) { @@ -31,7 +30,7 @@ class Geo $record = $this->cached($ip); if ($record === null) { - $record = $this->fetch($ip); + $record = $this->client?->lookup($ip); if ($record !== null) { $this->store($ip, $record); } @@ -117,32 +116,4 @@ class Geo $this->cache->set($ip, ['value' => $value]); } } - - /** - * @return array|null - */ - private function fetch(string $ip): ?array - { - if (empty($this->endpoint) || empty($this->secret)) { - return null; - } - - try { - $client = new Client(); - $client->addHeader('Authorization', 'Bearer ' . $this->secret); - $client->setTimeout(3000); - - $response = $client->fetch(\rtrim($this->endpoint, '/') . "/ips/{$ip}", Client::METHOD_GET); - if ($response->getStatusCode() === 200) { - $body = $response->json(); - if (\is_array($body)) { - return $body; - } - } - } catch (\Throwable $th) { - Console::warning('Geo service unavailable: ' . $th->getMessage()); - } - - return null; - } } diff --git a/src/Appwrite/Locale/Geo/Client.php b/src/Appwrite/Locale/Geo/Client.php new file mode 100644 index 0000000000..59accb0ff8 --- /dev/null +++ b/src/Appwrite/Locale/Geo/Client.php @@ -0,0 +1,63 @@ +requests = new Factory(); + } + + public static function pooled(string $endpoint, string $secret, float $timeout = 3.0, int $poolSize = 64): self + { + $host = \parse_url($endpoint, PHP_URL_HOST) ?: $endpoint; + + return new self(new HttpClientPool(new Connections( + new SwoolePoolAdapter(), + "geo.{$host}", + $poolSize, + fn () => (new HttpClient((new SwooleClientAdapter())->withConnectionReuse()->withTimeout($timeout)->withConnectTimeout($timeout))) + ->withBaseUri(\rtrim($endpoint, '/') . '/') + ->withBearerAuth($secret), + timeout: 3.0 + ))); + } + + /** + * @return array|null + */ + public function lookup(string $ip): ?array + { + if ($ip === '') { + return null; + } + + try { + // Relative path so the endpoint's base path (e.g. `/v1`) is kept. + $response = $this->client->sendRequest($this->requests->createRequest(Method::GET, "ips/{$ip}")); + if ($response->getStatusCode() === 200) { + $record = \json_decode((string) $response->getBody(), true); + if (\is_array($record) && $record !== []) { + return $record; + } + } + } catch (\Throwable $th) { + Console::error($th->getMessage()); + } + + return null; + } +}