优化代码

This commit is contained in:
刘祥超
2022-11-13 10:32:12 +08:00
parent dbddf8a91a
commit 5f76be2cfd
3 changed files with 86 additions and 1 deletions

View File

@@ -27,6 +27,9 @@ type RPCClient struct {
apiConfig *configs.APIConfig apiConfig *configs.APIConfig
conns []*grpc.ClientConn conns []*grpc.ClientConn
ctx context.Context
ctxUpdatedAt int64
locker sync.RWMutex locker sync.RWMutex
NodeRPC pb.NodeServiceClient NodeRPC pb.NodeServiceClient
@@ -94,7 +97,12 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
// Context 节点上下文信息 // Context 节点上下文信息
func (this *RPCClient) Context() context.Context { func (this *RPCClient) Context() context.Context {
var ctx = context.Background() var currentTime = time.Now().Unix()
if this.ctx != nil && this.ctxUpdatedAt > currentTime-5 {
return this.ctx
}
var m = maps.Map{ var m = maps.Map{
"timestamp": time.Now().Unix(), "timestamp": time.Now().Unix(),
"type": "node", "type": "node",
@@ -111,7 +119,13 @@ func (this *RPCClient) Context() context.Context {
return context.Background() return context.Background()
} }
var token = base64.StdEncoding.EncodeToString(data) var token = base64.StdEncoding.EncodeToString(data)
var ctx = context.Background()
ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", this.apiConfig.NodeId, "token", token) ctx = metadata.AppendToOutgoingContext(ctx, "nodeId", this.apiConfig.NodeId, "token", token)
this.ctxUpdatedAt = currentTime
this.ctx = ctx
return ctx return ctx
} }

62
internal/rpc/rpc_test.go Normal file
View File

@@ -0,0 +1,62 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package rpc_test
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeNode/internal/rpc"
_ "github.com/iwind/TeaGo/bootstrap"
timeutil "github.com/iwind/TeaGo/utils/time"
"sync"
"testing"
"time"
)
func TestRPCConcurrentCall(t *testing.T) {
rpcClient, err := rpc.SharedRPC()
if err != nil {
t.Fatal(err)
}
var before = time.Now()
defer func() {
t.Log("cost:", time.Since(before).Seconds()*1000, "ms")
}()
var concurrent = 3
var wg = sync.WaitGroup{}
wg.Add(concurrent)
for i := 0; i < concurrent; i++ {
go func() {
defer wg.Done()
_, err = rpcClient.NodeRPC.FindCurrentNodeConfig(rpcClient.Context(), &pb.FindCurrentNodeConfigRequest{})
if err != nil {
t.Log(err)
}
}()
}
wg.Wait()
}
func TestRPC_Retry(t *testing.T) {
rpcClient, err := rpc.SharedRPC()
if err != nil {
t.Fatal(err)
}
var ticker = time.NewTicker(1 * time.Second)
for range ticker.C {
go func() {
_, err = rpcClient.NodeRPC.FindCurrentNodeConfig(rpcClient.Context(), &pb.FindCurrentNodeConfigRequest{})
if err != nil {
t.Log(timeutil.Format("H:i:s"), err)
} else {
t.Log(timeutil.Format("H:i:s"), "success")
}
}()
}
}

View File

@@ -73,6 +73,15 @@ func (this *UserAgentParser) Parse(userAgent string) (result UserAgentParserResu
result.BrowserName, result.BrowserVersion = this.parser.Browser() result.BrowserName, result.BrowserVersion = this.parser.Browser()
result.IsMobile = this.parser.Mobile() result.IsMobile = this.parser.Mobile()
// 忽略特殊字符
if len(result.BrowserName) > 0 {
for _, r := range result.BrowserName {
if r == '$' || r == '"' || r == '\'' || r == '<' || r == '>' || r == ')' {
return
}
}
}
if this.cacheCursor == 0 { if this.cacheCursor == 0 {
this.cacheMap1[userAgent] = result this.cacheMap1[userAgent] = result
if len(this.cacheMap1) >= this.maxCacheItems { if len(this.cacheMap1) >= this.maxCacheItems {