Improve: lazy load mmdb

This commit is contained in:
Dreamacro 2018-08-01 00:18:29 +08:00
parent 295d649624
commit a1a58c31ae

View File

@ -1,21 +1,18 @@
package rules package rules
import ( import (
"sync"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var mmdb *geoip2.Reader var (
mmdb *geoip2.Reader
func init() { once sync.Once
var err error )
mmdb, err = geoip2.Open(C.MMDBPath)
if err != nil {
log.Fatalf("Can't load mmdb: %s", err.Error())
}
}
type GEOIP struct { type GEOIP struct {
country string country string
@ -43,6 +40,13 @@ func (g *GEOIP) Payload() string {
} }
func NewGEOIP(country string, adapter string) *GEOIP { 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{ return &GEOIP{
country: country, country: country,
adapter: adapter, adapter: adapter,