当使用quit退出进程时,同时也禁用缓存策略

This commit is contained in:
GoEdgeLab
2021-12-19 14:15:17 +08:00
parent 3ab0f64b18
commit 8d4c2b0fe0
3 changed files with 59 additions and 10 deletions

View File

@@ -47,6 +47,7 @@ type FileList struct {
hitsTableName string hitsTableName string
isClosed bool isClosed bool
isReady bool
memoryCache *ttlcache.Cache memoryCache *ttlcache.Cache
} }
@@ -77,11 +78,30 @@ func (this *FileList) Init() error {
// 防止sqlite提示authority错误 // 防止sqlite提示authority错误
dir = "" dir = ""
} }
db, err := sql.Open("sqlite3", "file:"+dir+"/index.db?cache=shared&mode=rwc&_journal_mode=WAL") var dbPath = dir + "/index.db"
remotelogs.Println("CACHE", "loading database '"+dbPath+"'")
db, err := sql.Open("sqlite3", "file:"+dbPath+"?cache=shared&mode=rwc&_journal_mode=WAL")
if err != nil { if err != nil {
return err return err
} }
// 检查数据库
_, err = db.Exec(`SELECT * FROM "` + this.itemsTableName + `" LIMIT 1`)
if err != nil {
// 删除重建
remotelogs.Println("CACHE", "rebuilding database '"+dbPath+"'")
_ = db.Close()
this.isClosed = false
_ = os.Remove(dbPath)
db, err = sql.Open("sqlite3", "file:"+dbPath+"?cache=shared&mode=rwc&_journal_mode=WAL")
if err != nil {
return err
}
}
db.SetMaxOpenConns(1) db.SetMaxOpenConns(1)
this.db = db this.db = db
// 清除旧表 // 清除旧表
@@ -176,6 +196,8 @@ func (this *FileList) Init() error {
return err return err
} }
this.isReady = true
return nil return nil
} }
@@ -185,7 +207,7 @@ func (this *FileList) Reset() error {
} }
func (this *FileList) Add(hash string, item *Item) error { func (this *FileList) Add(hash string, item *Item) error {
if this.isClosed { if !this.isReady {
return nil return nil
} }
@@ -212,7 +234,7 @@ func (this *FileList) Add(hash string, item *Item) error {
} }
func (this *FileList) Exist(hash string) (bool, error) { func (this *FileList) Exist(hash string) (bool, error) {
if this.isClosed { if !this.isReady {
return false, nil return false, nil
} }
@@ -242,7 +264,7 @@ func (this *FileList) Exist(hash string) (bool, error) {
// CleanPrefix 清理某个前缀的缓存数据 // CleanPrefix 清理某个前缀的缓存数据
func (this *FileList) CleanPrefix(prefix string) error { func (this *FileList) CleanPrefix(prefix string) error {
if this.isClosed { if !this.isReady {
return nil return nil
} }
@@ -272,7 +294,7 @@ func (this *FileList) CleanPrefix(prefix string) error {
} }
func (this *FileList) Remove(hash string) error { func (this *FileList) Remove(hash string) error {
if this.isClosed { if !this.isReady {
return nil return nil
} }
@@ -316,7 +338,7 @@ func (this *FileList) Remove(hash string) error {
// count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间 // count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间
// callback 每次发现过期key的调用 // callback 每次发现过期key的调用
func (this *FileList) Purge(count int, callback func(hash string) error) (int, error) { func (this *FileList) Purge(count int, callback func(hash string) error) (int, error) {
if this.isClosed { if !this.isReady {
return 0, nil return 0, nil
} }
@@ -360,7 +382,7 @@ func (this *FileList) Purge(count int, callback func(hash string) error) (int, e
} }
func (this *FileList) PurgeLFU(count int, callback func(hash string) error) error { func (this *FileList) PurgeLFU(count int, callback func(hash string) error) error {
if this.isClosed { if !this.isReady {
return nil return nil
} }
@@ -403,7 +425,7 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro
} }
func (this *FileList) CleanAll() error { func (this *FileList) CleanAll() error {
if this.isClosed { if !this.isReady {
return nil return nil
} }
@@ -418,7 +440,7 @@ func (this *FileList) CleanAll() error {
} }
func (this *FileList) Stat(check func(hash string) bool) (*Stat, error) { func (this *FileList) Stat(check func(hash string) bool) (*Stat, error) {
if this.isClosed { if !this.isReady {
return &Stat{}, nil return &Stat{}, nil
} }
@@ -461,6 +483,7 @@ func (this *FileList) OnRemove(f func(item *Item)) {
func (this *FileList) Close() error { func (this *FileList) Close() error {
this.isClosed = true this.isClosed = true
this.isReady = false
this.memoryCache.Destroy() this.memoryCache.Destroy()

View File

@@ -3,8 +3,10 @@ package caches
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"strconv" "strconv"
"sync" "sync"
@@ -12,6 +14,13 @@ import (
var SharedManager = NewManager() var SharedManager = NewManager()
func init() {
events.On(events.EventQuit, func() {
logs.Println("CACHE", "quiting cache manager")
SharedManager.UpdatePolicies([]*serverconfigs.HTTPCachePolicy{})
})
}
// Manager 缓存策略管理器 // Manager 缓存策略管理器
type Manager struct { type Manager struct {
// 全局配置 // 全局配置
@@ -25,10 +34,12 @@ type Manager struct {
// NewManager 获取管理器对象 // NewManager 获取管理器对象
func NewManager() *Manager { func NewManager() *Manager {
return &Manager{ var m = &Manager{
policyMap: map[int64]*serverconfigs.HTTPCachePolicy{}, policyMap: map[int64]*serverconfigs.HTTPCachePolicy{},
storageMap: map[int64]StorageInterface{}, storageMap: map[int64]StorageInterface{},
} }
return m
} }
// UpdatePolicies 重新设置策略 // UpdatePolicies 重新设置策略

View File

@@ -3,6 +3,7 @@ package nodes
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/assert" "github.com/iwind/TeaGo/assert"
"runtime"
"testing" "testing"
) )
@@ -33,3 +34,17 @@ func TestHTTPRequest_RedirectToHTTPS(t *testing.T) {
a.IsBool(req.web.RedirectToHttps.IsOn == true) a.IsBool(req.web.RedirectToHttps.IsOn == true)
} }
} }
func TestHTTPRequest_Memory(t *testing.T) {
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
var requests = []*HTTPRequest{}
for i := 0; i < 1_000_000; i++ {
requests = append(requests, &HTTPRequest{})
}
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,")
}