Skip to content

Commit

Permalink
chore: apply config when geo update
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed May 19, 2024
1 parent df69a31 commit c3ee921
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
23 changes: 7 additions & 16 deletions component/updater/update_geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"runtime"
"sync"
"time"

"github.com/metacubex/mihomo/common/atomic"
Expand All @@ -19,8 +18,7 @@ import (
)

var (
updateGeoMux sync.Mutex
UpdatingGeo atomic.Bool
UpdatingGeo atomic.Bool
)

func updateGeoDatabases() error {
Expand Down Expand Up @@ -100,22 +98,15 @@ func updateGeoDatabases() error {
return nil
}

func UpdateGeoDatabases() error {
func UpdateGeoDatabases(updateNotification chan struct{}) error {
log.Infoln("[GEO] Start updating GEO database")

updateGeoMux.Lock()

if UpdatingGeo.Load() {
updateGeoMux.Unlock()
return errors.New("GEO database is updating, skip")
}

UpdatingGeo.Store(true)
updateGeoMux.Unlock()

defer func() {
UpdatingGeo.Store(false)
}()
defer UpdatingGeo.Store(false)

log.Infoln("[GEO] Updating GEO database")

Expand All @@ -124,6 +115,7 @@ func UpdateGeoDatabases() error {
return err
}

updateNotification <- struct{}{}
return nil
}

Expand All @@ -144,7 +136,7 @@ func getUpdateTime() (err error, time time.Time) {
return nil, fileInfo.ModTime()
}

func RegisterGeoUpdater() {
func RegisterGeoUpdater(updateNotification chan struct{}) {
if C.GeoUpdateInterval <= 0 {
log.Errorln("[GEO] Invalid update interval: %d", C.GeoUpdateInterval)
return
Expand All @@ -164,16 +156,15 @@ func RegisterGeoUpdater() {
log.Infoln("[GEO] last update time %s", lastUpdate)
if lastUpdate.Add(time.Duration(C.GeoUpdateInterval) * time.Hour).Before(time.Now()) {
log.Infoln("[GEO] Database has not been updated for %v, update now", time.Duration(C.GeoUpdateInterval)*time.Hour)
if err := UpdateGeoDatabases(); err != nil {
if err := UpdateGeoDatabases(updateNotification); err != nil {
log.Errorln("[GEO] Failed to update GEO database: %s", err.Error())
return
}
}

for range ticker.C {
if err := UpdateGeoDatabases(); err != nil {
if err := UpdateGeoDatabases(updateNotification); err != nil {
log.Errorln("[GEO] Failed to update GEO database: %s", err.Error())
return
}
}
}()
Expand Down
53 changes: 35 additions & 18 deletions hub/route/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,30 +364,47 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
}

func updateGeoDatabases(w http.ResponseWriter, r *http.Request) {
if updater.UpdatingGeo.Load() {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError("updating..."))
return
}
updateNotification := make(chan struct{})
errorChannel := make(chan error, 1)
done := make(chan struct{})
defer func() {
close(updateNotification)
close(errorChannel)
}()

err := updater.UpdateGeoDatabases()
if err != nil {
render.Status(r, http.StatusBadRequest)
render.JSON(w, r, newError(err.Error()))
return
}
go func() {
defer close(done)
for {
select {
case <-updateNotification:
cfg, err := executor.ParseWithPath(C.Path.Config())
if err != nil {
log.Errorln("[REST-API] update GEO databases failed: %v", err)
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, newError("Error parsing configuration"))
return
}

log.Warnln("[REST-API] update GEO databases success, applying config")
executor.ApplyConfig(cfg, false)
return
case err := <-errorChannel:
log.Errorln("[REST-API] update GEO databases failed: %v", err)
render.Status(r, http.StatusInternalServerError)
render.JSON(w, r, err.Error())
return
}
}
}()

go func() {
cfg, err := executor.ParseWithPath(C.Path.Config())
err := updater.UpdateGeoDatabases(updateNotification)
if err != nil {
log.Errorln("[REST-API] update GEO databases failed: %v", err)
return
errorChannel <- err
}

log.Warnln("[REST-API] update GEO databases successful, apply config...")

executor.ApplyConfig(cfg, false)
}()

<-done

render.NoContent(w, r)
}
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,23 @@ func main() {
}

if C.GeoAutoUpdate {
updater.RegisterGeoUpdater()
}
updateNotification := make(chan struct{})
go updater.RegisterGeoUpdater(updateNotification)

go func() {
for range updateNotification {
cfg, err := executor.ParseWithPath(C.Path.Config())
if err != nil {
log.Errorln("[GEO] update GEO databases failed: %v", err)
return
}

log.Warnln("[GEO] update GEO databases success, applying config")

executor.ApplyConfig(cfg, false)
}
}()
}
defer executor.Shutdown()

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

0 comments on commit c3ee921

Please sign in to comment.