|
|
@@ -2,6 +2,7 @@ package geodata
|
|
|
|
|
|
import (
|
|
|
"io/ioutil"
|
|
|
+ "runtime"
|
|
|
"strings"
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
@@ -32,7 +33,7 @@ func (g GeoIPCache) Set(key string, value *router.GeoIP) {
|
|
|
|
|
|
func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
|
|
|
asset := platform.GetAssetLocation(filename)
|
|
|
- idx := strings.ToUpper(asset + "_" + code)
|
|
|
+ idx := strings.ToUpper(asset + "|" + code)
|
|
|
if g.Has(idx) {
|
|
|
return g.Get(idx), nil
|
|
|
}
|
|
|
@@ -47,11 +48,11 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
|
|
|
g.Set(idx, &geoip)
|
|
|
return &geoip, nil
|
|
|
|
|
|
- case ErrCodeNotFound:
|
|
|
+ case errCodeNotFound:
|
|
|
return nil, newError(code, " not found in ", filename)
|
|
|
|
|
|
- case ErrFailedToReadBytes, ErrFailedToReadExpectedLenBytes,
|
|
|
- ErrInvalidGeodataFile, ErrInvalidGeodataVarintLength:
|
|
|
+ case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
|
|
|
+ errInvalidGeodataFile, errInvalidGeodataVarintLength:
|
|
|
newError("failed to decode geodata file: ", filename, ". Fallback to the original ReadFile method.").AtWarning().WriteToLog()
|
|
|
geoipBytes, err = ioutil.ReadFile(asset)
|
|
|
if err != nil {
|
|
|
@@ -61,11 +62,13 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
|
|
|
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ runtime.GC()
|
|
|
for _, geoip := range geoipList.GetEntry() {
|
|
|
if strings.EqualFold(code, geoip.GetCountryCode()) {
|
|
|
g.Set(idx, geoip)
|
|
|
return geoip, nil
|
|
|
}
|
|
|
+ runtime.GC()
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
@@ -97,7 +100,7 @@ func (g GeoSiteCache) Set(key string, value *router.GeoSite) {
|
|
|
|
|
|
func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error) {
|
|
|
asset := platform.GetAssetLocation(filename)
|
|
|
- idx := strings.ToUpper(asset + "_" + code)
|
|
|
+ idx := strings.ToUpper(asset + "|" + code)
|
|
|
if g.Has(idx) {
|
|
|
return g.Get(idx), nil
|
|
|
}
|
|
|
@@ -112,11 +115,11 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
|
|
|
g.Set(idx, &geosite)
|
|
|
return &geosite, nil
|
|
|
|
|
|
- case ErrCodeNotFound:
|
|
|
+ case errCodeNotFound:
|
|
|
return nil, newError(code, " not found in ", filename)
|
|
|
|
|
|
- case ErrFailedToReadBytes, ErrFailedToReadExpectedLenBytes,
|
|
|
- ErrInvalidGeodataFile, ErrInvalidGeodataVarintLength:
|
|
|
+ case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
|
|
|
+ errInvalidGeodataFile, errInvalidGeodataVarintLength:
|
|
|
newError("failed to decode geodata file: ", filename, ". Fallback to the original ReadFile method.").AtWarning().WriteToLog()
|
|
|
geositeBytes, err = ioutil.ReadFile(asset)
|
|
|
if err != nil {
|
|
|
@@ -126,11 +129,13 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
|
|
|
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ runtime.GC()
|
|
|
for _, geosite := range geositeList.GetEntry() {
|
|
|
if strings.EqualFold(code, geosite.GetCountryCode()) {
|
|
|
g.Set(idx, geosite)
|
|
|
return geosite, nil
|
|
|
}
|
|
|
+ runtime.GC()
|
|
|
}
|
|
|
|
|
|
default:
|