mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
Improve: lazy load mmdb
This commit is contained in:
parent
969f30cfc4
commit
1e7b5abbe2
|
@ -1,21 +1,18 @@
|
|||
package rules
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var mmdb *geoip2.Reader
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
mmdb, err = geoip2.Open(C.MMDBPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Can't load mmdb: %s", err.Error())
|
||||
}
|
||||
}
|
||||
var (
|
||||
mmdb *geoip2.Reader
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
type GEOIP struct {
|
||||
country string
|
||||
|
@ -43,6 +40,13 @@ func (g *GEOIP) Payload() string {
|
|||
}
|
||||
|
||||
func NewGEOIP(country string, adapter string) *GEOIP {
|
||||
once.Do(func() {
|
||||
var err error
|
||||
mmdb, err = geoip2.Open(C.MMDBPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Can't load mmdb: %s", err.Error())
|
||||
}
|
||||
})
|
||||
return &GEOIP{
|
||||
country: country,
|
||||
adapter: adapter,
|
||||
|
|
Loading…
Reference in New Issue
Block a user