mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
refactor(geo): resolve locale per request in getGeoForIp and drop in-memory cache
Swoole workers reuse the singleton getGeoForIp closure across all requests. Capturing the Locale reference at first resolution would lock every subsequent request to the first request's language, and the per-worker $cache grew unbounded for the life of the process. Pass Locale into the closure invocation instead of closing over it, and rely on the per-request geoRecord resource to cache within a request.
This commit is contained in:
1 parent
1c1503a0d0
commit
f5cc846913
1 file changed
+7
-17
@@ -1323,7 +1323,7 @@ return function (Container $context): void {
|
||||
return new Agent($adapter);
|
||||
}, ['register']);
|
||||
|
||||
$context->set('geoRecord', function ($request, callable $getGeoForIp) {
|
||||
$context->set('geoRecord', function ($request, Locale $locale, callable $getGeoForIp) {
|
||||
$ip = $request->getIp();
|
||||
|
||||
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||
@@ -1331,17 +1331,11 @@ return function (Container $context): void {
|
||||
$ip = '0.0.0.0';
|
||||
}
|
||||
|
||||
return $getGeoForIp($ip);
|
||||
}, ['request', 'getGeoForIp']);
|
||||
|
||||
$context->set('getGeoForIp', function (Locale $locale) {
|
||||
$cache = [];
|
||||
|
||||
return function (string $ip) use ($locale, &$cache): GeoRecord {
|
||||
if (isset($cache[$ip])) {
|
||||
return $cache[$ip];
|
||||
}
|
||||
return $getGeoForIp($locale, $ip);
|
||||
}, ['request', 'locale', 'getGeoForIp']);
|
||||
|
||||
$context->set('getGeoForIp', function () {
|
||||
return function (Locale $locale, string $ip): GeoRecord {
|
||||
$record = null;
|
||||
$lookupSucceeded = false;
|
||||
$geoEndpoint = System::getEnv('_APP_GEO_ENDPOINT', '');
|
||||
@@ -1384,7 +1378,7 @@ return function (Container $context): void {
|
||||
|
||||
$autonomousSystemNumber = $record['autonomousSystemNumber'] ?? null;
|
||||
|
||||
$geoRecord = (new GeoRecord([
|
||||
return (new GeoRecord([
|
||||
'ip' => $ip,
|
||||
'countryCode' => $countryCode,
|
||||
'continentCode' => $continentCode,
|
||||
@@ -1404,10 +1398,6 @@ return function (Container $context): void {
|
||||
]))
|
||||
->setLocale($locale)
|
||||
->setLookupSucceeded($lookupSucceeded);
|
||||
|
||||
$cache[$ip] = $geoRecord;
|
||||
|
||||
return $geoRecord;
|
||||
};
|
||||
}, ['locale']);
|
||||
}, []);
|
||||
};
|
||||
Reference in new issue
Block a user