mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 16:00:25 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -3,7 +3,6 @@ package apps
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/goman"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/utils"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/sizes"
 | 
					 | 
				
			||||||
	"github.com/iwind/TeaGo/Tea"
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
	"github.com/iwind/TeaGo/files"
 | 
						"github.com/iwind/TeaGo/files"
 | 
				
			||||||
	timeutil "github.com/iwind/TeaGo/utils/time"
 | 
						timeutil "github.com/iwind/TeaGo/utils/time"
 | 
				
			||||||
@@ -41,7 +40,7 @@ func (this *LogWriter) Init() {
 | 
				
			|||||||
	this.c = make(chan string, 1024)
 | 
						this.c = make(chan string, 1024)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 异步写入文件
 | 
						// 异步写入文件
 | 
				
			||||||
	var maxFileSize = 128 * sizes.M // 文件最大尺寸,超出此尺寸则清空
 | 
						var maxFileSize int64 = 128 << 20 // 文件最大尺寸,超出此尺寸则清空
 | 
				
			||||||
	if fp != nil {
 | 
						if fp != nil {
 | 
				
			||||||
		goman.New(func() {
 | 
							goman.New(func() {
 | 
				
			||||||
			var totalSize int64 = 0
 | 
								var totalSize int64 = 0
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,7 +18,6 @@ import (
 | 
				
			|||||||
	fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
 | 
						fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
 | 
				
			||||||
	memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
 | 
						memutils "github.com/TeaOSLab/EdgeNode/internal/utils/mem"
 | 
				
			||||||
	setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
 | 
						setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets"
 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/sizes"
 | 
					 | 
				
			||||||
	"github.com/TeaOSLab/EdgeNode/internal/zero"
 | 
						"github.com/TeaOSLab/EdgeNode/internal/zero"
 | 
				
			||||||
	"github.com/iwind/TeaGo/Tea"
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
	"github.com/iwind/TeaGo/rands"
 | 
						"github.com/iwind/TeaGo/rands"
 | 
				
			||||||
@@ -312,12 +311,12 @@ func (this *FileStorage) Init() error {
 | 
				
			|||||||
		var totalSize = this.TotalDiskSize()
 | 
							var totalSize = this.TotalDiskSize()
 | 
				
			||||||
		var cost = time.Since(before).Seconds() * 1000
 | 
							var cost = time.Since(before).Seconds() * 1000
 | 
				
			||||||
		var sizeMB = types.String(totalSize) + " Bytes"
 | 
							var sizeMB = types.String(totalSize) + " Bytes"
 | 
				
			||||||
		if totalSize > 1*sizes.G {
 | 
							if totalSize > (1 << 30) {
 | 
				
			||||||
			sizeMB = fmt.Sprintf("%.3f G", float64(totalSize)/float64(sizes.G))
 | 
								sizeMB = fmt.Sprintf("%.3f GiB", float64(totalSize)/(1<<30))
 | 
				
			||||||
		} else if totalSize > 1*sizes.M {
 | 
							} else if totalSize > (1 << 20) {
 | 
				
			||||||
			sizeMB = fmt.Sprintf("%.3f M", float64(totalSize)/float64(sizes.M))
 | 
								sizeMB = fmt.Sprintf("%.3f MiB", float64(totalSize)/(1<<20))
 | 
				
			||||||
		} else if totalSize > 1*sizes.K {
 | 
							} else if totalSize > (1 << 10) {
 | 
				
			||||||
			sizeMB = fmt.Sprintf("%.3f K", float64(totalSize)/float64(sizes.K))
 | 
								sizeMB = fmt.Sprintf("%.3f KiB", float64(totalSize)/(1<<10))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		var mmapTag = "disabled"
 | 
							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()
 | 
								this.hotMapLocker.Lock()
 | 
				
			||||||
			hotItem, ok := this.hotMap[key]
 | 
								hotItem, ok := this.hotMap[key]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -664,7 +664,7 @@ func TestFileStorage_ScanGarbageCaches(t *testing.T) {
 | 
				
			|||||||
func BenchmarkFileStorage_Read(b *testing.B) {
 | 
					func BenchmarkFileStorage_Read(b *testing.B) {
 | 
				
			||||||
	runtime.GOMAXPROCS(1)
 | 
						runtime.GOMAXPROCS(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_ = utils.SetRLimit(1024 * 1024)
 | 
						_ = utils.SetRLimit(1 << 20)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{
 | 
						var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{
 | 
				
			||||||
		Id:   1,
 | 
							Id:   1,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -282,14 +282,14 @@ func (this *APIStream) handleStatCache(message *pb.NodeStreamMessage) error {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sizeFormat := ""
 | 
						sizeFormat := ""
 | 
				
			||||||
	if stat.Size < 1024 {
 | 
						if stat.Size < (1 << 10) {
 | 
				
			||||||
		sizeFormat = strconv.FormatInt(stat.Size, 10) + " Bytes"
 | 
							sizeFormat = strconv.FormatInt(stat.Size, 10) + " Bytes"
 | 
				
			||||||
	} else if stat.Size < 1024*1024 {
 | 
						} else if stat.Size < (1 << 20) {
 | 
				
			||||||
		sizeFormat = fmt.Sprintf("%.2f KB", float64(stat.Size)/1024)
 | 
							sizeFormat = fmt.Sprintf("%.2f KiB", float64(stat.Size)/(1<<10))
 | 
				
			||||||
	} else if stat.Size < 1024*1024*1024 {
 | 
						} else if stat.Size < (1 << 30) {
 | 
				
			||||||
		sizeFormat = fmt.Sprintf("%.2f MB", float64(stat.Size)/1024/1024)
 | 
							sizeFormat = fmt.Sprintf("%.2f MiB", float64(stat.Size)/(1<<20))
 | 
				
			||||||
	} else {
 | 
						} 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))
 | 
						this.replyOk(message.RequestId, "size:"+sizeFormat+", count:"+strconv.Itoa(stat.Count))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	// AccessLogMaxRequestBodySize 访问日志存储的请求内容最大尺寸 TODO 此值应该可以在访问日志页设置
 | 
						// AccessLogMaxRequestBodySize 访问日志存储的请求内容最大尺寸 TODO 此值应该可以在访问日志页设置
 | 
				
			||||||
	AccessLogMaxRequestBodySize = 2 * 1024 * 1024
 | 
						AccessLogMaxRequestBodySize = 2 << 20
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 日志
 | 
					// 日志
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -213,7 +213,7 @@ func (this *Node) Start() {
 | 
				
			|||||||
	events.Notify(events.EventLoaded)
 | 
						events.Notify(events.EventLoaded)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 设置rlimit
 | 
						// 设置rlimit
 | 
				
			||||||
	_ = utils.SetRLimit(1024 * 1024)
 | 
						_ = utils.SetRLimit(1 << 20)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 连接API
 | 
						// 连接API
 | 
				
			||||||
	goman.New(func() {
 | 
						goman.New(func() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
@@ -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)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user