mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	优化IP名单相关代码/关闭、重启进程时自动关闭IP名单本地缓存数据库
This commit is contained in:
		@@ -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(),
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user