From d3030f7e3f9c7f41ac07806bae69394c37ef2f4c Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Mon, 25 Jul 2022 20:23:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96IP=E5=90=8D=E5=8D=95=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81/=E5=85=B3=E9=97=AD=E3=80=81?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E8=BF=9B=E7=A8=8B=E6=97=B6=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=85=B3=E9=97=ADIP=E5=90=8D=E5=8D=95=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/iplibrary/ip_item_test.go | 2 +- internal/iplibrary/ip_list_db.go | 27 +++++++++++++++++++++++++++ internal/iplibrary/ip_list_db_test.go | 5 +++++ internal/iplibrary/ip_list_test.go | 10 +++++----- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/internal/iplibrary/ip_item_test.go b/internal/iplibrary/ip_item_test.go index 15e3b1f..1bdf114 100644 --- a/internal/iplibrary/ip_item_test.go +++ b/internal/iplibrary/ip_item_test.go @@ -79,7 +79,7 @@ func TestIPItem_Memory(t *testing.T) { for i := 0; i < 2_000_000; i ++ { list.Add(&IPItem{ Type: "ip", - Id: int64(i), + Id: uint64(i), IPFrom: utils.IP2Long("192.168.1.1"), IPTo: 0, ExpiredAt: time.Now().Unix(), diff --git a/internal/iplibrary/ip_list_db.go b/internal/iplibrary/ip_list_db.go index c90096f..dc29296 100644 --- a/internal/iplibrary/ip_list_db.go +++ b/internal/iplibrary/ip_list_db.go @@ -28,6 +28,8 @@ type IPListDB struct { cleanTicker *time.Ticker dir string + + isClosed bool } func NewIPListDB() (*IPListDB, error) { @@ -56,6 +58,12 @@ func (this *IPListDB) init() error { return err } db.SetMaxOpenConns(1) + + //_, err = db.Exec("VACUUM") + //if err != nil { + // return err + //} + this.db = db // 初始化数据库 @@ -117,6 +125,7 @@ ON "` + this.itemTableName + `" ( goman.New(func() { events.On(events.EventQuit, func() { + _ = this.Close() this.cleanTicker.Stop() }) @@ -133,11 +142,19 @@ ON "` + this.itemTableName + `" ( // DeleteExpiredItems 删除过期的条目 func (this *IPListDB) DeleteExpiredItems() error { + if this.isClosed { + return nil + } + _, err := this.deleteExpiredItemsStmt.Exec(time.Now().Unix() - 7*86400) return err } func (this *IPListDB) AddItem(item *pb.IPItem) error { + if this.isClosed { + return nil + } + _, err := this.deleteItemStmt.Exec(item.Id) if err != nil { return err @@ -147,6 +164,10 @@ func (this *IPListDB) AddItem(item *pb.IPItem) error { } func (this *IPListDB) ReadItems(offset int64, size int64) (items []*pb.IPItem, err error) { + if this.isClosed { + return + } + rows, err := this.selectItemsStmt.Query(offset, size) if err != nil { return nil, err @@ -169,6 +190,10 @@ func (this *IPListDB) ReadItems(offset int64, size int64) (items []*pb.IPItem, e // ReadMaxVersion 读取当前最大版本号 func (this *IPListDB) ReadMaxVersion() int64 { + if this.isClosed { + return 0 + } + row := this.selectMaxVersionStmt.QueryRow() if row == nil { return 0 @@ -182,6 +207,8 @@ func (this *IPListDB) ReadMaxVersion() int64 { } func (this *IPListDB) Close() error { + this.isClosed = true + if this.db != nil { _ = this.deleteExpiredItemsStmt.Close() _ = this.deleteItemStmt.Close() diff --git a/internal/iplibrary/ip_list_db_test.go b/internal/iplibrary/ip_list_db_test.go index 9e28bb5..fab894c 100644 --- a/internal/iplibrary/ip_list_db_test.go +++ b/internal/iplibrary/ip_list_db_test.go @@ -53,6 +53,11 @@ func TestIPListDB_ReadItems(t *testing.T) { if err != nil { t.Fatal(err) } + + defer func() { + _ = db.Close() + }() + items, err := db.ReadItems(0, 2) if err != nil { t.Fatal(err) diff --git a/internal/iplibrary/ip_list_test.go b/internal/iplibrary/ip_list_test.go index 5be4c61..6c50234 100644 --- a/internal/iplibrary/ip_list_test.go +++ b/internal/iplibrary/ip_list_test.go @@ -144,7 +144,7 @@ func TestIPList_Contains(t *testing.T) { list := NewIPList() for i := 0; i < 255; i++ { list.AddDelay(&IPItem{ - Id: int64(i), + Id: uint64(i), IPFrom: utils.IP2Long(strconv.Itoa(i) + ".168.0.1"), IPTo: utils.IP2Long(strconv.Itoa(i) + ".168.255.1"), ExpiredAt: 0, @@ -152,7 +152,7 @@ func TestIPList_Contains(t *testing.T) { } for i := 0; i < 255; i++ { list.AddDelay(&IPItem{ - Id: int64(1000 + i), + Id: uint64(1000 + i), IPFrom: utils.IP2Long("192.167.2." + strconv.Itoa(i)), }) } @@ -172,7 +172,7 @@ func TestIPList_Contains_Many(t *testing.T) { list := NewIPList() for i := 0; i < 1_000_000; i++ { list.AddDelay(&IPItem{ - Id: int64(i), + Id: uint64(i), IPFrom: utils.IP2Long(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))), IPTo: utils.IP2Long(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))), ExpiredAt: 0, @@ -217,7 +217,7 @@ func TestIPList_ContainsIPStrings(t *testing.T) { list := NewIPList() for i := 0; i < 255; i++ { list.Add(&IPItem{ - Id: int64(i), + Id: uint64(i), IPFrom: utils.IP2Long(strconv.Itoa(i) + ".168.0.1"), IPTo: utils.IP2Long(strconv.Itoa(i) + ".168.255.1"), ExpiredAt: 0, @@ -305,7 +305,7 @@ func BenchmarkIPList_Contains(b *testing.B) { var list = NewIPList() for i := 1; i < 200_000; i++ { list.AddDelay(&IPItem{ - Id: int64(i), + Id: uint64(i), IPFrom: utils.IP2Long(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"), IPTo: utils.IP2Long(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"), ExpiredAt: time.Now().Unix() + 60,