在节点重新实现缓存策略和WAF策略

This commit is contained in:
刘祥超
2020-12-17 17:36:10 +08:00
parent 0e312f6707
commit 1ea63ab8c6
26 changed files with 151 additions and 129 deletions

View File

@@ -2,7 +2,7 @@ package caches
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeNode/internal/logs"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/iwind/TeaGo/lists"
"strconv"
"sync"
@@ -36,7 +36,7 @@ func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy
// 停止旧有的
for _, oldPolicy := range this.policyMap {
if !lists.ContainsInt64(newPolicyIds, oldPolicy.Id) {
logs.Error("CACHE", "remove policy "+strconv.FormatInt(oldPolicy.Id, 10))
remotelogs.Error("CACHE", "remove policy "+strconv.FormatInt(oldPolicy.Id, 10))
delete(this.policyMap, oldPolicy.Id)
storage, ok := this.storageMap[oldPolicy.Id]
if ok {
@@ -50,13 +50,13 @@ func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy
for _, newPolicy := range newPolicies {
_, ok := this.policyMap[newPolicy.Id]
if !ok {
logs.Println("CACHE", "add policy "+strconv.FormatInt(newPolicy.Id, 10))
remotelogs.Println("CACHE", "add policy "+strconv.FormatInt(newPolicy.Id, 10))
}
// 初始化
err := newPolicy.Init()
if err != nil {
logs.Error("CACHE", "UpdatePolicies: init policy error: "+err.Error())
remotelogs.Error("CACHE", "UpdatePolicies: init policy error: "+err.Error())
continue
}
this.policyMap[newPolicy.Id] = newPolicy
@@ -68,19 +68,19 @@ func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy
if !ok {
storage := this.NewStorageWithPolicy(policy)
if storage == nil {
logs.Error("CACHE", "can not find storage type '"+policy.Type+"'")
remotelogs.Error("CACHE", "can not find storage type '"+policy.Type+"'")
continue
}
err := storage.Init()
if err != nil {
logs.Error("CACHE", "UpdatePolicies: init storage failed: "+err.Error())
remotelogs.Error("CACHE", "UpdatePolicies: init storage failed: "+err.Error())
continue
}
this.storageMap[policy.Id] = storage
} else {
// 检查policy是否有变化
if !storage.Policy().IsSame(policy) {
logs.Println("CACHE", "policy "+strconv.FormatInt(policy.Id, 10)+" changed")
remotelogs.Println("CACHE", "policy "+strconv.FormatInt(policy.Id, 10)+" changed")
// 停止老的
storage.Stop()
@@ -89,12 +89,12 @@ func (this *Manager) UpdatePolicies(newPolicies []*serverconfigs.HTTPCachePolicy
// 启动新的
storage := this.NewStorageWithPolicy(policy)
if storage == nil {
logs.Error("CACHE", "can not find storage type '"+policy.Type+"'")
remotelogs.Error("CACHE", "can not find storage type '"+policy.Type+"'")
continue
}
err := storage.Init()
if err != nil {
logs.Error("CACHE", "UpdatePolicies: init storage failed: "+err.Error())
remotelogs.Error("CACHE", "UpdatePolicies: init storage failed: "+err.Error())
continue
}
this.storageMap[policy.Id] = storage

View File

@@ -7,7 +7,7 @@ import (
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/logs"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/types"
@@ -82,7 +82,7 @@ func (this *FileStorage) Init() error {
}
cost := time.Since(before).Seconds() * 1000
logs.Println("CACHE", "init policy "+strconv.FormatInt(this.policy.Id, 10)+", cost: "+fmt.Sprintf("%.2f", cost)+" ms, count: "+strconv.Itoa(count)+", size: "+fmt.Sprintf("%.3f", float64(size)/1024/1024)+" M")
remotelogs.Println("CACHE", "init policy "+strconv.FormatInt(this.policy.Id, 10)+", cost: "+fmt.Sprintf("%.2f", cost)+" ms, count: "+strconv.Itoa(count)+", size: "+fmt.Sprintf("%.3f", float64(size)/1024/1024)+" M")
}()
// 配置
@@ -546,7 +546,7 @@ func (this *FileStorage) initList() error {
item, err := this.decodeFile(path)
if err != nil {
if err != ErrNotFound {
logs.Error("CACHE", "decode path '"+path+"': "+err.Error())
remotelogs.Error("CACHE", "decode path '"+path+"': "+err.Error())
}
continue
}
@@ -559,7 +559,7 @@ func (this *FileStorage) initList() error {
// 启动定时清理任务
this.ticker = utils.NewTicker(30 * time.Second)
events.On(events.EventQuit, func() {
logs.Println("CACHE", "quit clean timer")
remotelogs.Println("CACHE", "quit clean timer")
var ticker = this.ticker
if ticker != nil {
ticker.Stop()
@@ -642,7 +642,7 @@ func (this *FileStorage) purgeLoop() {
path := this.hashPath(hash)
err := os.Remove(path)
if err != nil && !os.IsNotExist(err) {
logs.Error("CACHE", "purge '"+path+"' error: "+err.Error())
remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error())
}
})
}