Skip to content

Commit

Permalink
feat: re-download geo db when autoDL && load fail
Browse files Browse the repository at this point in the history
  • Loading branch information
haruue committed Feb 21, 2024
1 parent e22aa06 commit 6dea0ad
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions app/internal/utils/geoloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (l *GeoLoader) shouldDownload(filename string) bool {
if os.IsNotExist(err) {
return true
}
if info.Size() == 0 {
// empty files are loadable by v2geo, but we consider it broken
return true
}
dt := time.Now().Sub(info.ModTime())
if l.UpdateInterval == 0 {
return dt > geoDefaultUpdateInterval
Expand Down Expand Up @@ -101,7 +105,15 @@ func (l *GeoLoader) LoadGeoIP() (map[string]*v2geo.GeoIP, error) {
autoDL = true
filename = geoipFilename
}
if autoDL && l.shouldDownload(filename) {
if autoDL {
if !l.shouldDownload(filename) {
m, err := v2geo.LoadGeoIP(filename)
if err == nil {
l.geoipMap = m
return m, nil
}
// file is broken, download it again
}
err := l.downloadAndCheck(filename, geoipURL, func(filename string) error {
_, err := v2geo.LoadGeoIP(filename)
return err
Expand All @@ -128,7 +140,15 @@ func (l *GeoLoader) LoadGeoSite() (map[string]*v2geo.GeoSite, error) {
autoDL = true
filename = geositeFilename
}
if autoDL && l.shouldDownload(filename) {
if autoDL {
if !l.shouldDownload(filename) {
m, err := v2geo.LoadGeoSite(filename)
if err == nil {
l.geositeMap = m
return m, nil
}
// file is broken, download it again
}
err := l.downloadAndCheck(filename, geositeURL, func(filename string) error {
_, err := v2geo.LoadGeoSite(filename)
return err
Expand Down

0 comments on commit 6dea0ad

Please sign in to comment.