Skip to content

Commit

Permalink
fix: update mmdb fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Larvan2 committed Feb 21, 2024
1 parent 3d833ef commit f8295a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion component/mmdb/mmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Verify() bool {
func Instance() Reader {
once.Do(func() {
mmdbPath := C.Path.MMDB()
log.Debugln("Load MMDB file: %s", mmdbPath)
log.Infoln("Load MMDB file: %s", mmdbPath)
mmdb, err := maxminddb.Open(mmdbPath)
if err != nil {
log.Fatalln("Can't load MMDB: %s", err.Error())
Expand Down Expand Up @@ -94,3 +94,7 @@ func DownloadMMDB(path string) (err error) {

return err
}

func Reload() {
once = sync.Once{}
}
4 changes: 4 additions & 0 deletions config/update_geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/metacubex/mihomo/component/geodata"
_ "github.com/metacubex/mihomo/component/geodata/standard"
"github.com/metacubex/mihomo/component/mmdb"
C "github.com/metacubex/mihomo/constant"

"github.com/oschwald/maxminddb-golang"
Expand Down Expand Up @@ -33,6 +34,7 @@ func UpdateGeoDatabases() error {
}

} else {
defer mmdb.Reload()
data, err := downloadForBytes(C.MmdbUrl)
if err != nil {
return fmt.Errorf("can't download MMDB database file: %w", err)
Expand All @@ -43,6 +45,8 @@ func UpdateGeoDatabases() error {
return fmt.Errorf("invalid MMDB database file: %s", err)
}
_ = instance.Close()

mmdb.Instance().Reader.Close() // mmdb is loaded with mmap, so it needs to be closed before overwriting the file
if err = saveFile(data, C.Path.MMDB()); err != nil {
return fmt.Errorf("can't save MMDB database file: %w", err)
}
Expand Down

0 comments on commit f8295a0

Please sign in to comment.