优化代码

This commit is contained in:
刘祥超
2023-08-08 10:07:24 +08:00
parent cdfc37ac14
commit ae74114fca
8 changed files with 18 additions and 12 deletions

View File

@@ -157,6 +157,7 @@ func (this *FileStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)
IsFull: false, IsFull: false,
}) })
} }
this.subDirs = subDirs
this.checkDiskSpace() this.checkDiskSpace()
err = newOptions.Init() err = newOptions.Init()
@@ -482,7 +483,7 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea
stat, err := os.Stat(cachePath) stat, err := os.Stat(cachePath)
// 检查两次写入缓存的时间是否过于相近,分片内容不受此限制 // 检查两次写入缓存的时间是否过于相近,分片内容不受此限制
if err == nil && !isPartial && time.Now().Sub(stat.ModTime()) <= 1*time.Second { if err == nil && !isPartial && time.Since(stat.ModTime()) <= 1*time.Second {
// 防止并发连续写入 // 防止并发连续写入
return nil, ErrFileIsWriting return nil, ErrFileIsWriting
} }

View File

@@ -1,6 +1,7 @@
package nodes package nodes
import ( import (
"context"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"runtime" "runtime"
"testing" "testing"
@@ -43,7 +44,7 @@ func TestHTTPClientPool_cleanClients(t *testing.T) {
Version: 2, Version: 2,
Addr: &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"}, Addr: &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"},
} }
err := origin.Init(nil) err := origin.Init(context.Background())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -65,7 +66,7 @@ func BenchmarkHTTPClientPool_Client(b *testing.B) {
Version: 2, Version: 2,
Addr: &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"}, Addr: &serverconfigs.NetworkAddressConfig{Host: "127.0.0.1", PortRange: "1234"},
} }
err := origin.Init(nil) err := origin.Init(context.Background())
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }

View File

@@ -95,7 +95,7 @@ func (this *HTTPRequest) doRoot() (isBreak bool) {
} }
var filename = strings.Replace(requestPath, "/", Tea.DS, -1) var filename = strings.Replace(requestPath, "/", Tea.DS, -1)
var filePath = "" var filePath string
if len(filename) > 0 && filename[0:1] == Tea.DS { if len(filename) > 0 && filename[0:1] == Tea.DS {
filePath = rootDir + filename filePath = rootDir + filename
} else { } else {

View File

@@ -2,6 +2,7 @@ package nodes
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
@@ -194,7 +195,7 @@ func (this *Node) Start() {
} }
teaconst.NodeId = nodeConfig.Id teaconst.NodeId = nodeConfig.Id
teaconst.NodeIdString = types.String(teaconst.NodeId) teaconst.NodeIdString = types.String(teaconst.NodeId)
err, serverErrors := nodeConfig.Init(nil) err, serverErrors := nodeConfig.Init(context.Background())
if err != nil { if err != nil {
remotelogs.Error("NODE", "init node config failed: "+err.Error()) remotelogs.Error("NODE", "init node config failed: "+err.Error())
return return
@@ -391,7 +392,7 @@ func (this *Node) syncConfig(taskVersion int64) error {
return err return err
} }
err, serverErrors := nodeConfig.Init(nil) err, serverErrors := nodeConfig.Init(context.Background())
if err != nil { if err != nil {
return err return err
} }
@@ -1034,7 +1035,7 @@ func (this *Node) reloadServer() {
remotelogs.Debug("NODE", "reload "+types.String(countUpdatingServers)+" servers") remotelogs.Debug("NODE", "reload "+types.String(countUpdatingServers)+" servers")
} }
err, serverErrors := newNodeConfig.Init(nil) err, serverErrors := newNodeConfig.Init(context.Background())
if err != nil { if err != nil {
remotelogs.Error("NODE", "apply server config error: "+err.Error()) remotelogs.Error("NODE", "apply server config error: "+err.Error())
return return

View File

@@ -1,10 +1,9 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build arm64 //go:build arm64
// +build arm64
package nodes package nodes
// 处理异常 // 处理异常
func (this *Node) handlePanic() { func (this *Node) handlePanic() {
return
} }

View File

@@ -122,7 +122,7 @@ func TestList_ManyItems(t *testing.T) {
}) })
list.GC(time.Now().Unix() + 1) list.GC(time.Now().Unix() + 1)
t.Log("gc", count, "items") t.Log("gc", count, "items")
t.Log(time.Now().Sub(now)) t.Log(time.Since(now))
} }
func TestList_Map_Performance(t *testing.T) { func TestList_Map_Performance(t *testing.T) {
@@ -140,7 +140,7 @@ func TestList_Map_Performance(t *testing.T) {
for i := 0; i < 100_000; i++ { for i := 0; i < 100_000; i++ {
delete(m, int64(i)) delete(m, int64(i))
} }
t.Log(time.Now().Sub(now)) t.Log(time.Since(now))
} }
{ {

View File

@@ -88,3 +88,7 @@ func (this *TestRequest) DisableAccessLog() {
func (this *TestRequest) ProcessResponseHeaders(headers http.Header, status int) { func (this *TestRequest) ProcessResponseHeaders(headers http.Header, status int) {
} }
func (this *TestRequest) WAFMaxRequestSize() int64 {
return 1 << 20
}

View File

@@ -34,7 +34,7 @@ func (this *IPRange) Contains(netIP net.IP) bool {
} }
case IPRangeTypeSingeIP: case IPRangeTypeSingeIP:
if this.IPFrom != nil { if this.IPFrom != nil {
return bytes.Equal(this.IPFrom, netIP) return this.IPFrom.Equal(netIP)
} }
case IPRangeTypeRange: case IPRangeTypeRange:
return bytes.Compare(this.IPFrom, netIP) <= 0 && bytes.Compare(this.IPTo, netIP) >= 0 return bytes.Compare(this.IPFrom, netIP) <= 0 && bytes.Compare(this.IPTo, netIP) >= 0