Skip to content

Commit f8b27b9

Browse files
committed
chore: auto update geo
1 parent e749c7e commit f8b27b9

File tree

3 files changed

+42
-60
lines changed

3 files changed

+42
-60
lines changed

component/updater/update_geo.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func updateGeoDatabases() error {
9898
return nil
9999
}
100100

101-
func UpdateGeoDatabases(updateNotification chan struct{}) error {
101+
func UpdateGeoDatabases(onSuccess func()) error {
102102
log.Infoln("[GEO] Start updating GEO database")
103103

104104
if UpdatingGeo.Load() {
@@ -115,7 +115,7 @@ func UpdateGeoDatabases(updateNotification chan struct{}) error {
115115
return err
116116
}
117117

118-
updateNotification <- struct{}{}
118+
onSuccess()
119119
return nil
120120
}
121121

@@ -136,17 +136,16 @@ func getUpdateTime() (err error, time time.Time) {
136136
return nil, fileInfo.ModTime()
137137
}
138138

139-
func RegisterGeoUpdater(updateNotification chan struct{}) {
139+
func RegisterGeoUpdater(onSuccess func()) {
140140
if C.GeoUpdateInterval <= 0 {
141141
log.Errorln("[GEO] Invalid update interval: %d", C.GeoUpdateInterval)
142142
return
143143
}
144144

145-
ticker := time.NewTicker(time.Duration(C.GeoUpdateInterval) * time.Hour)
146-
defer ticker.Stop()
147-
148-
log.Infoln("[GEO] update GEO database every %d hours", C.GeoUpdateInterval)
149145
go func() {
146+
ticker := time.NewTicker(time.Duration(C.GeoUpdateInterval) * time.Hour)
147+
defer ticker.Stop()
148+
150149
err, lastUpdate := getUpdateTime()
151150
if err != nil {
152151
log.Errorln("[GEO] Get GEO database update time error: %s", err.Error())
@@ -156,14 +155,15 @@ func RegisterGeoUpdater(updateNotification chan struct{}) {
156155
log.Infoln("[GEO] last update time %s", lastUpdate)
157156
if lastUpdate.Add(time.Duration(C.GeoUpdateInterval) * time.Hour).Before(time.Now()) {
158157
log.Infoln("[GEO] Database has not been updated for %v, update now", time.Duration(C.GeoUpdateInterval)*time.Hour)
159-
if err := UpdateGeoDatabases(updateNotification); err != nil {
158+
if err := UpdateGeoDatabases(onSuccess); err != nil {
160159
log.Errorln("[GEO] Failed to update GEO database: %s", err.Error())
161160
return
162161
}
163162
}
164163

165164
for range ticker.C {
166-
if err := UpdateGeoDatabases(updateNotification); err != nil {
165+
log.Infoln("[GEO] updating database every %d hours", C.GeoUpdateInterval)
166+
if err := UpdateGeoDatabases(onSuccess); err != nil {
167167
log.Errorln("[GEO] Failed to update GEO database: %s", err.Error())
168168
}
169169
}

hub/route/configs.go

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -364,47 +364,31 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
364364
}
365365

366366
func updateGeoDatabases(w http.ResponseWriter, r *http.Request) {
367-
updateNotification := make(chan struct{})
368-
errorChannel := make(chan error, 1)
369-
done := make(chan struct{})
370-
defer func() {
371-
close(updateNotification)
372-
close(errorChannel)
373-
}()
374-
375-
go func() {
376-
defer close(done)
377-
for {
378-
select {
379-
case <-updateNotification:
380-
cfg, err := executor.ParseWithPath(C.Path.Config())
381-
if err != nil {
382-
log.Errorln("[REST-API] update GEO databases failed: %v", err)
383-
render.Status(r, http.StatusInternalServerError)
384-
render.JSON(w, r, newError("Error parsing configuration"))
385-
return
386-
}
387-
388-
log.Warnln("[REST-API] update GEO databases success, applying config")
389-
executor.ApplyConfig(cfg, false)
390-
return
391-
case err := <-errorChannel:
392-
log.Errorln("[REST-API] update GEO databases failed: %v", err)
393-
render.Status(r, http.StatusInternalServerError)
394-
render.JSON(w, r, err.Error())
395-
return
396-
}
397-
}
398-
}()
399-
400-
go func() {
401-
err := updater.UpdateGeoDatabases(updateNotification)
367+
if updater.UpdatingGeo.Load() {
368+
render.Status(r, http.StatusConflict)
369+
render.JSON(w, r, newError("GEO database is updating, skip"))
370+
return
371+
}
372+
373+
onSuccess := func() {
374+
cfg, err := executor.ParseWithPath(C.Path.Config())
402375
if err != nil {
403-
errorChannel <- err
376+
log.Errorln("[GEO] update GEO databases failed: %v", err)
377+
return
404378
}
405-
}()
406379

407-
<-done
380+
log.Warnln("[GEO] update GEO databases success, applying config")
381+
382+
executor.ApplyConfig(cfg, false)
383+
}
384+
385+
err := updater.UpdateGeoDatabases(onSuccess)
386+
if err != nil {
387+
render.Status(r, http.StatusInternalServerError)
388+
render.JSON(w, r, newError(err.Error()))
389+
return
390+
}
408391

392+
onSuccess()
409393
render.NoContent(w, r)
410394
}

main.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,21 @@ func main() {
113113
}
114114

115115
if C.GeoAutoUpdate {
116-
updateNotification := make(chan struct{})
117-
go updater.RegisterGeoUpdater(updateNotification)
116+
onSuccess := func() {
117+
cfg, err := executor.ParseWithPath(C.Path.Config())
118+
if err != nil {
119+
log.Errorln("[GEO] update GEO databases failed: %v", err)
120+
return
121+
}
118122

119-
go func() {
120-
for range updateNotification {
121-
cfg, err := executor.ParseWithPath(C.Path.Config())
122-
if err != nil {
123-
log.Errorln("[GEO] update GEO databases failed: %v", err)
124-
return
125-
}
123+
log.Warnln("[GEO] update GEO databases success, applying config")
126124

127-
log.Warnln("[GEO] update GEO databases success, applying config")
125+
executor.ApplyConfig(cfg, false)
126+
}
128127

129-
executor.ApplyConfig(cfg, false)
130-
}
131-
}()
128+
updater.RegisterGeoUpdater(onSuccess)
132129
}
130+
133131
defer executor.Shutdown()
134132

135133
termSign := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)