mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-22 16:23:15 +08:00
Improve: lazy load mmdb
This commit is contained in:
parent
295d649624
commit
a1a58c31ae
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user