11import { CONFIG } from '@server/initializers/config.js'
22import { pathExists } from 'fs-extra/esm'
33import { writeFile } from 'fs/promises'
4- import throttle from 'lodash-es/throttle.js'
54import maxmind , { CityResponse , CountryResponse , Reader } from 'maxmind'
65import { join } from 'path'
76import { isArray } from './custom-validators/misc.js'
@@ -16,6 +15,9 @@ export class GeoIP {
1615 private countryReader : Reader < CountryResponse >
1716 private cityReader : Reader < CityResponse >
1817
18+ private lastInitTry : Date
19+ private initReadersPromise : Promise < any >
20+
1921 private readonly INIT_READERS_RETRY_INTERVAL = 1000 * 60 * 10 // 10 minutes
2022 private readonly countryDBPath = join ( CONFIG . STORAGE . BIN_DIR , 'dbip-country-lite-latest.mmdb' )
2123 private readonly cityDBPath = join ( CONFIG . STORAGE . BIN_DIR , 'dbip-city-lite-latest.mmdb' )
@@ -28,7 +30,9 @@ export class GeoIP {
2830 if ( CONFIG . GEO_IP . ENABLED === false ) return emptyResult
2931
3032 try {
31- await this . initReadersIfNeededThrottle ( )
33+ if ( ! this . initReadersPromise ) this . initReadersPromise = this . initReadersIfNeeded ( )
34+ await this . initReadersPromise
35+ this . initReadersPromise = undefined
3236
3337 const countryResult = this . countryReader ?. get ( ip )
3438 const cityResult = this . cityReader ?. get ( ip )
@@ -112,6 +116,9 @@ export class GeoIP {
112116 // ---------------------------------------------------------------------------
113117
114118 private async initReadersIfNeeded ( ) {
119+ if ( this . lastInitTry && this . lastInitTry . getTime ( ) > Date . now ( ) - this . INIT_READERS_RETRY_INTERVAL ) return
120+ this . lastInitTry = new Date ( )
121+
115122 if ( ! this . countryReader ) {
116123 let open = true
117124
@@ -137,8 +144,6 @@ export class GeoIP {
137144 }
138145 }
139146
140- private readonly initReadersIfNeededThrottle = throttle ( this . initReadersIfNeeded . bind ( this ) , this . INIT_READERS_RETRY_INTERVAL )
141-
142147 // ---------------------------------------------------------------------------
143148
144149 static get Instance ( ) {
0 commit comments