fix: overall perf improvements
* fix: ignore private ips * fix: performance related fixes * fix: simply event buffer * fix: default to 1 events queue shard * add: cleanup scripts * fix: comments * fix comments * fix * fix: groupmq * wip * fix: sync cachable * remove cluster names and add it behind env flag (if someone want to scale) * fix * wip * better logger * remove reqid and user agent * fix lock * remove wait_for_async_insert
This commit is contained in:
committed by
GitHub
parent
38cc53890a
commit
da59622dce
@@ -2,11 +2,12 @@ import { readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import type { ReaderModel } from '@maxmind/geoip2-node';
|
||||
import { Reader } from '@maxmind/geoip2-node';
|
||||
import { LRUCache } from 'lru-cache';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
import type { ReaderModel } from '@maxmind/geoip2-node';
|
||||
import { Reader } from '@maxmind/geoip2-node';
|
||||
|
||||
const filename = 'GeoLite2-City.mmdb';
|
||||
// From api or worker package
|
||||
@@ -50,24 +51,37 @@ const DEFAULT_GEO: GeoLocation = {
|
||||
|
||||
const ignore = ['127.0.0.1', '::1'];
|
||||
|
||||
const cache = new LRUCache<string, GeoLocation>({
|
||||
max: 1000,
|
||||
ttl: 1000 * 60 * 5,
|
||||
ttlAutopurge: true,
|
||||
});
|
||||
|
||||
export async function getGeoLocation(ip?: string): Promise<GeoLocation> {
|
||||
if (!ip || ignore.includes(ip)) {
|
||||
return DEFAULT_GEO;
|
||||
}
|
||||
|
||||
const cached = cache.get(ip);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
if (!reader) {
|
||||
await loadDatabase(dbPath);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await reader?.city(ip);
|
||||
return {
|
||||
const res = {
|
||||
city: response?.city?.names.en,
|
||||
country: response?.country?.isoCode,
|
||||
region: response?.subdivisions?.[0]?.names.en,
|
||||
longitude: response?.location?.longitude,
|
||||
latitude: response?.location?.latitude,
|
||||
};
|
||||
cache.set(ip, res);
|
||||
return res;
|
||||
} catch (error) {
|
||||
return DEFAULT_GEO;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user