From 4f66f55152fb3d4e68442056f9ae21535b1b1c7f Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Mon, 15 Apr 2024 19:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/apps/log_writer.go | 3 +-- internal/caches/storage_file.go | 15 +++++++-------- internal/caches/storage_file_test.go | 2 +- internal/nodes/api_stream.go | 12 ++++++------ internal/nodes/http_request_log.go | 2 +- internal/nodes/node.go | 2 +- internal/utils/sizes/sizes.go | 10 ---------- internal/utils/sizes/sizes_test.go | 17 ----------------- 8 files changed, 17 insertions(+), 46 deletions(-) delete mode 100644 internal/utils/sizes/sizes.go delete mode 100644 internal/utils/sizes/sizes_test.go diff --git a/internal/apps/log_writer.go b/internal/apps/log_writer.go index 07dc004..4949d2b 100644 --- a/internal/apps/log_writer.go +++ b/internal/apps/log_writer.go @@ -3,7 +3,6 @@ package apps import ( "github.com/TeaOSLab/EdgeNode/internal/goman" "github.com/TeaOSLab/EdgeNode/internal/utils" - "github.com/TeaOSLab/EdgeNode/internal/utils/sizes" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/files" timeutil "github.com/iwind/TeaGo/utils/time" @@ -41,7 +40,7 @@ func (this *LogWriter) Init() { this.c = make(chan string, 1024) // 异步写入文件 - var maxFileSize = 128 * sizes.M // 文件最大尺寸,超出此尺寸则清空 + var maxFileSize int64 = 128 << 20 // 文件最大尺寸,超出此尺寸则清空 if fp != nil { goman.New(func() { var totalSize int64 = 0 diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index ab31607..6b708b4 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -18,7 +18,6 @@ import ( fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs" memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem" setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets" - "github.com/TeaOSLab/EdgeNode/internal/utils/sizes" "github.com/TeaOSLab/EdgeNode/internal/zero" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/rands" @@ -312,12 +311,12 @@ func (this *FileStorage) Init() error { var totalSize = this.TotalDiskSize() var cost = time.Since(before).Seconds() * 1000 var sizeMB = types.String(totalSize) + " Bytes" - if totalSize > 1*sizes.G { - sizeMB = fmt.Sprintf("%.3f G", float64(totalSize)/float64(sizes.G)) - } else if totalSize > 1*sizes.M { - sizeMB = fmt.Sprintf("%.3f M", float64(totalSize)/float64(sizes.M)) - } else if totalSize > 1*sizes.K { - sizeMB = fmt.Sprintf("%.3f K", float64(totalSize)/float64(sizes.K)) + if totalSize > (1 << 30) { + sizeMB = fmt.Sprintf("%.3f GiB", float64(totalSize)/(1<<30)) + } else if totalSize > (1 << 20) { + sizeMB = fmt.Sprintf("%.3f MiB", float64(totalSize)/(1<<20)) + } else if totalSize > (1 << 10) { + sizeMB = fmt.Sprintf("%.3f KiB", float64(totalSize)/(1<<10)) } var mmapTag = "disabled" @@ -1441,7 +1440,7 @@ func (this *FileStorage) increaseHit(key string, hash string, reader Reader) { // 增加到热点 // 这里不收录缓存尺寸过大的文件 - if memoryStorage != nil && reader.BodySize() > 0 && reader.BodySize() < 128*sizes.M { + if memoryStorage != nil && reader.BodySize() > 0 && reader.BodySize() < (128<<20) { this.hotMapLocker.Lock() hotItem, ok := this.hotMap[key] diff --git a/internal/caches/storage_file_test.go b/internal/caches/storage_file_test.go index 905480d..b74b6a8 100644 --- a/internal/caches/storage_file_test.go +++ b/internal/caches/storage_file_test.go @@ -664,7 +664,7 @@ func TestFileStorage_ScanGarbageCaches(t *testing.T) { func BenchmarkFileStorage_Read(b *testing.B) { runtime.GOMAXPROCS(1) - _ = utils.SetRLimit(1024 * 1024) + _ = utils.SetRLimit(1 << 20) var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, diff --git a/internal/nodes/api_stream.go b/internal/nodes/api_stream.go index 56b1779..a76ad99 100644 --- a/internal/nodes/api_stream.go +++ b/internal/nodes/api_stream.go @@ -282,14 +282,14 @@ func (this *APIStream) handleStatCache(message *pb.NodeStreamMessage) error { } sizeFormat := "" - if stat.Size < 1024 { + if stat.Size < (1 << 10) { sizeFormat = strconv.FormatInt(stat.Size, 10) + " Bytes" - } else if stat.Size < 1024*1024 { - sizeFormat = fmt.Sprintf("%.2f KB", float64(stat.Size)/1024) - } else if stat.Size < 1024*1024*1024 { - sizeFormat = fmt.Sprintf("%.2f MB", float64(stat.Size)/1024/1024) + } else if stat.Size < (1 << 20) { + sizeFormat = fmt.Sprintf("%.2f KiB", float64(stat.Size)/(1<<10)) + } else if stat.Size < (1 << 30) { + sizeFormat = fmt.Sprintf("%.2f MiB", float64(stat.Size)/(1<<20)) } else { - sizeFormat = fmt.Sprintf("%.2f GB", float64(stat.Size)/1024/1024/1024) + sizeFormat = fmt.Sprintf("%.2f GiB", float64(stat.Size)/(1<<30)) } this.replyOk(message.RequestId, "size:"+sizeFormat+", count:"+strconv.Itoa(stat.Count)) diff --git a/internal/nodes/http_request_log.go b/internal/nodes/http_request_log.go index 457bfc8..8cc32db 100644 --- a/internal/nodes/http_request_log.go +++ b/internal/nodes/http_request_log.go @@ -9,7 +9,7 @@ import ( const ( // AccessLogMaxRequestBodySize 访问日志存储的请求内容最大尺寸 TODO 此值应该可以在访问日志页设置 - AccessLogMaxRequestBodySize = 2 * 1024 * 1024 + AccessLogMaxRequestBodySize = 2 << 20 ) // 日志 diff --git a/internal/nodes/node.go b/internal/nodes/node.go index 1e49c6c..5acaace 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -213,7 +213,7 @@ func (this *Node) Start() { events.Notify(events.EventLoaded) // 设置rlimit - _ = utils.SetRLimit(1024 * 1024) + _ = utils.SetRLimit(1 << 20) // 连接API goman.New(func() { diff --git a/internal/utils/sizes/sizes.go b/internal/utils/sizes/sizes.go deleted file mode 100644 index c0bb3ff..0000000 --- a/internal/utils/sizes/sizes.go +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. - -package sizes - -const ( - K int64 = 1024 - M = 1024 * K - G = 1024 * M - T = 1024 * G -) diff --git a/internal/utils/sizes/sizes_test.go b/internal/utils/sizes/sizes_test.go deleted file mode 100644 index 35334f5..0000000 --- a/internal/utils/sizes/sizes_test.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. - -package sizes_test - -import ( - "github.com/TeaOSLab/EdgeNode/internal/utils/sizes" - "github.com/iwind/TeaGo/assert" - "testing" -) - -func TestSizes(t *testing.T) { - var a = assert.NewAssertion(t) - a.IsTrue(sizes.K == 1024) - a.IsTrue(sizes.M == 1024*1024) - a.IsTrue(sizes.G == 1024*1024*1024) - a.IsTrue(sizes.T == 1024*1024*1024*1024) -}