Skip to content

Commit 4e51c83

Browse files
committed
Fix geoip crash
1 parent 5b7d7b4 commit 4e51c83

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

server/core/helpers/geo-ip.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { CONFIG } from '@server/initializers/config.js'
22
import { pathExists } from 'fs-extra/esm'
33
import { writeFile } from 'fs/promises'
4-
import throttle from 'lodash-es/throttle.js'
54
import maxmind, { CityResponse, CountryResponse, Reader } from 'maxmind'
65
import { join } from 'path'
76
import { 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

Comments
 (0)