geo load 1 time

pull/627/head
form1ca 2021-04-13 12:01:54 +03:00
parent ce35a2d0ab
commit 0fb4046251
1 changed files with 19 additions and 1 deletions

View File

@ -49,6 +49,24 @@ export function getDevice(screen, browser, os) {
}
}
let lookupGeo;
let loadingPromise;
// load geo db only 1 time
async function loadGeo() {
if (lookupGeo) return lookupGeo;
if (loadingPromise) return loadingPromise;
loadingPromise = new Promise((resolve, reject) =>
maxmind
.open(path.resolve('./public/geo/GeoLite2-Country.mmdb'))
.then(res => {
lookupGeo = res;
resolve(lookupGeo);
})
.catch(reject),
);
return loadingPromise;
}
export async function getCountry(req, ip) {
// Cloudflare
if (req.headers['cf-ipcountry']) {
@ -61,7 +79,7 @@ export async function getCountry(req, ip) {
}
// Database lookup
const lookup = await maxmind.open(path.resolve('./public/geo/GeoLite2-Country.mmdb'));
const lookup = await loadGeo();
const result = lookup.get(ip);