mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 00:33:27 +08:00
chore: apply config when geo update
This commit is contained in:
parent
df69a31e62
commit
c3ee921d30
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/metacubex/mihomo/common/atomic"
|
||||
@ -19,8 +18,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
updateGeoMux sync.Mutex
|
||||
UpdatingGeo atomic.Bool
|
||||
UpdatingGeo atomic.Bool
|
||||
)
|
||||
|
||||
func updateGeoDatabases() error {
|
||||
@ -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")
|
||||
|
||||
@ -124,6 +115,7 @@ func UpdateGeoDatabases() error {
|
||||
return err
|
||||
}
|
||||
|
||||
updateNotification <- struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
err := updater.UpdateGeoDatabases()
|
||||
if err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, newError(err.Error()))
|
||||
return
|
||||
}
|
||||
updateNotification := make(chan struct{})
|
||||
errorChannel := make(chan error, 1)
|
||||
done := make(chan struct{})
|
||||
defer func() {
|
||||
close(updateNotification)
|
||||
close(errorChannel)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
cfg, err := executor.ParseWithPath(C.Path.Config())
|
||||
if err != nil {
|
||||
log.Errorln("[REST-API] update GEO databases failed: %v", err)
|
||||
return
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
log.Warnln("[REST-API] update GEO databases successful, apply config...")
|
||||
|
||||
executor.ApplyConfig(cfg, false)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
err := updater.UpdateGeoDatabases(updateNotification)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
}
|
||||
}()
|
||||
|
||||
<-done
|
||||
|
||||
render.NoContent(w, r)
|
||||
}
|
||||
|
18
main.go
18
main.go
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user