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 <noreply@anthropic.com>
This commit is contained in:
loks0nandClaude Fable 5 committed 2026-08-25 19:45:13 +01:00
1 parent 7df32a86a3
commit 1872e975dd
4 files changed
+78 -36

No files matched your search

+8
View File
@@ -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 () {
+4 -4
View File
@@ -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']);
};
+3 -32
View File
@@ -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<string, mixed>|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;
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
namespace Appwrite\Locale\Geo;
use Psr\Http\Client\ClientInterface;
use Utopia\Client\Adapter\SwooleCoroutine\Client as SwooleClientAdapter;
use Utopia\Client as HttpClient;
use Utopia\Client\Pool as HttpClientPool;
use Utopia\Console;
use Utopia\Pools\Adapter\Swoole as SwoolePoolAdapter;
use Utopia\Pools\Pool as Connections;
use Utopia\Psr7\Method;
use Utopia\Psr7\Request\Factory;
class Client
{
private readonly Factory $requests;
public function __construct(private readonly ClientInterface $client)
{
$this->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<string, mixed>|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;
}
}