mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-27 07:26:35 +08:00
IP名单同步时在本地记录上一次同步的位置,以便于下次启动的时候不再重复执行
This commit is contained in:
@@ -8,6 +8,9 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/waf"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -21,6 +24,8 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
var versionCacheFile = "ip_list_version.cache"
|
||||
|
||||
// IPListManager IP名单管理
|
||||
type IPListManager struct {
|
||||
// 缓存文件
|
||||
@@ -37,7 +42,7 @@ type IPListManager struct {
|
||||
func NewIPListManager() *IPListManager {
|
||||
return &IPListManager{
|
||||
cacheFile: Tea.Root + "/configs/ip_list.cache",
|
||||
pageSize: 1000,
|
||||
pageSize: 500,
|
||||
listMap: map[int64]*IPList{},
|
||||
}
|
||||
}
|
||||
@@ -45,6 +50,9 @@ func NewIPListManager() *IPListManager {
|
||||
func (this *IPListManager) Start() {
|
||||
// TODO 从缓存当中读取数据
|
||||
|
||||
// 从缓存中读取位置
|
||||
this.version = this.readLocalVersion()
|
||||
|
||||
// 第一次读取
|
||||
err := this.loop()
|
||||
if err != nil {
|
||||
@@ -89,10 +97,9 @@ func (this *IPListManager) loop() error {
|
||||
if !hasNext {
|
||||
break
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
||||
// TODO 写入到缓存当中
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -163,6 +170,9 @@ func (this *IPListManager) fetch() (hasNext bool, err error) {
|
||||
this.locker.Unlock()
|
||||
this.version = items[len(items)-1].Version
|
||||
|
||||
// 写入版本号到缓存当中
|
||||
this.updateLocalVersion(this.version)
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -172,3 +182,25 @@ func (this *IPListManager) FindList(listId int64) *IPList {
|
||||
this.locker.Unlock()
|
||||
return list
|
||||
}
|
||||
|
||||
func (this *IPListManager) readLocalVersion() int64 {
|
||||
data, err := ioutil.ReadFile(Tea.ConfigFile(versionCacheFile))
|
||||
if err != nil || len(data) == 0 {
|
||||
return 0
|
||||
}
|
||||
return types.Int64(string(data))
|
||||
}
|
||||
|
||||
func (this *IPListManager) updateLocalVersion(version int64) {
|
||||
fp, err := os.OpenFile(Tea.ConfigFile(versionCacheFile), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
remotelogs.Warn("IP_LIST", "write local version cache failed: "+err.Error())
|
||||
return
|
||||
}
|
||||
_, err = fp.WriteString(types.String(version))
|
||||
if err != nil {
|
||||
remotelogs.Warn("IP_LIST", "write local version cache failed: "+err.Error())
|
||||
return
|
||||
}
|
||||
_ = fp.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user