From 1e7b5abbe2768418f0f2eac26d2788443a716bdf Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 1 Aug 2018 00:18:29 +0800 Subject: [PATCH] Improve: lazy load mmdb --- rules/geoip.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/rules/geoip.go b/rules/geoip.go index 9ee8691e..f79f26f0 100644 --- a/rules/geoip.go +++ b/rules/geoip.go @@ -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,