优化IP库内存使用

This commit is contained in:
GoEdgeLab
2022-08-21 23:09:25 +08:00
parent e5136e1680
commit 2dbc6e53d5
7 changed files with 147 additions and 86 deletions

View File

@@ -6,9 +6,7 @@ import (
"bytes"
"compress/gzip"
_ "embed"
"github.com/iwind/TeaGo/logs"
"net"
"os"
)
//go:embed internal-ip-library.db
@@ -16,16 +14,8 @@ var ipLibraryData []byte
var library = NewIPLibrary()
func init() {
var args = os.Args
if len(args) > 1 && args[1] == "daemon" {
return
}
err := library.Init()
if err != nil {
logs.Println("IP_LIBRARY", "initialized failed: "+err.Error())
}
func Init() error {
return library.Init()
}
func Lookup(ip net.IP) *QueryResult {
@@ -45,6 +35,9 @@ func NewIPLibrary() *IPLibrary {
}
func (this *IPLibrary) Init() error {
if len(ipLibraryData) == 0 || this.reader != nil {
return nil
}
var reader = bytes.NewReader(ipLibraryData)
gzipReader, err := gzip.NewReader(reader)
if err != nil {
@@ -59,6 +52,7 @@ func (this *IPLibrary) Init() error {
return err
}
this.reader = libReader
return nil
}