From 8d4c2b0fe0c9a9fb8b59178b5dbf5262df7f5dc0 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 19 Dec 2021 14:15:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E4=BD=BF=E7=94=A8quit=E9=80=80?= =?UTF-8?q?=E5=87=BA=E8=BF=9B=E7=A8=8B=E6=97=B6=EF=BC=8C=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E4=B9=9F=E7=A6=81=E7=94=A8=E7=BC=93=E5=AD=98=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file.go | 41 ++++++++++++++++++++++------- internal/caches/manager.go | 13 ++++++++- internal/nodes/http_request_test.go | 15 +++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/internal/caches/list_file.go b/internal/caches/list_file.go index 4f42399..f68fb07 100644 --- a/internal/caches/list_file.go +++ b/internal/caches/list_file.go @@ -47,6 +47,7 @@ type FileList struct { hitsTableName string isClosed bool + isReady bool memoryCache *ttlcache.Cache } @@ -77,11 +78,30 @@ func (this *FileList) Init() error { // 防止sqlite提示authority错误 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 { 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) + this.db = db // 清除旧表 @@ -176,6 +196,8 @@ func (this *FileList) Init() error { return err } + this.isReady = true + return nil } @@ -185,7 +207,7 @@ func (this *FileList) Reset() error { } func (this *FileList) Add(hash string, item *Item) error { - if this.isClosed { + if !this.isReady { return nil } @@ -212,7 +234,7 @@ func (this *FileList) Add(hash string, item *Item) error { } func (this *FileList) Exist(hash string) (bool, error) { - if this.isClosed { + if !this.isReady { return false, nil } @@ -242,7 +264,7 @@ func (this *FileList) Exist(hash string) (bool, error) { // CleanPrefix 清理某个前缀的缓存数据 func (this *FileList) CleanPrefix(prefix string) error { - if this.isClosed { + if !this.isReady { return nil } @@ -272,7 +294,7 @@ func (this *FileList) CleanPrefix(prefix string) error { } func (this *FileList) Remove(hash string) error { - if this.isClosed { + if !this.isReady { return nil } @@ -316,7 +338,7 @@ func (this *FileList) Remove(hash string) error { // count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间 // callback 每次发现过期key的调用 func (this *FileList) Purge(count int, callback func(hash string) error) (int, error) { - if this.isClosed { + if !this.isReady { 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 { - if this.isClosed { + if !this.isReady { return nil } @@ -403,7 +425,7 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro } func (this *FileList) CleanAll() error { - if this.isClosed { + if !this.isReady { return nil } @@ -418,7 +440,7 @@ func (this *FileList) CleanAll() error { } func (this *FileList) Stat(check func(hash string) bool) (*Stat, error) { - if this.isClosed { + if !this.isReady { return &Stat{}, nil } @@ -461,6 +483,7 @@ func (this *FileList) OnRemove(f func(item *Item)) { func (this *FileList) Close() error { this.isClosed = true + this.isReady = false this.memoryCache.Destroy() diff --git a/internal/caches/manager.go b/internal/caches/manager.go index 66be659..f0d625a 100644 --- a/internal/caches/manager.go +++ b/internal/caches/manager.go @@ -3,8 +3,10 @@ package caches import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" + "github.com/TeaOSLab/EdgeNode/internal/events" "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/types" "strconv" "sync" @@ -12,6 +14,13 @@ import ( var SharedManager = NewManager() +func init() { + events.On(events.EventQuit, func() { + logs.Println("CACHE", "quiting cache manager") + SharedManager.UpdatePolicies([]*serverconfigs.HTTPCachePolicy{}) + }) +} + // Manager 缓存策略管理器 type Manager struct { // 全局配置 @@ -25,10 +34,12 @@ type Manager struct { // NewManager 获取管理器对象 func NewManager() *Manager { - return &Manager{ + var m = &Manager{ policyMap: map[int64]*serverconfigs.HTTPCachePolicy{}, storageMap: map[int64]StorageInterface{}, } + + return m } // UpdatePolicies 重新设置策略 diff --git a/internal/nodes/http_request_test.go b/internal/nodes/http_request_test.go index e5669a2..dcb110f 100644 --- a/internal/nodes/http_request_test.go +++ b/internal/nodes/http_request_test.go @@ -3,6 +3,7 @@ package nodes import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/assert" + "runtime" "testing" ) @@ -33,3 +34,17 @@ func TestHTTPRequest_RedirectToHTTPS(t *testing.T) { 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,") +}