Improve: lazy load mmdb

This commit is contained in:
gVisor bot 2018-08-01 00:18:29 +08:00
parent 969f30cfc4
commit 1e7b5abbe2

View File

@ -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,