From ac069fd7f3d7d346ee94e4677aedde8730f9d19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 21 Nov 2021 10:27:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=97=A5=E5=BF=97=E7=AE=80?= =?UTF-8?q?=E5=8C=96requestId=E7=94=9F=E6=88=90=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_access_log_queue.go | 22 ++++++++++++++++++---- internal/nodes/http_request_log.go | 6 +----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/internal/nodes/http_access_log_queue.go b/internal/nodes/http_access_log_queue.go index 9eb9e82..68994d9 100644 --- a/internal/nodes/http_access_log_queue.go +++ b/internal/nodes/http_access_log_queue.go @@ -4,6 +4,7 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/rpc" + "strconv" "time" ) @@ -18,7 +19,7 @@ type HTTPAccessLogQueue struct { func NewHTTPAccessLogQueue() *HTTPAccessLogQueue { // 队列中最大的值,超出此数量的访问日志会被丢弃 // TODO 需要可以在界面中设置 - maxSize := 10000 + maxSize := 20000 queue := &HTTPAccessLogQueue{ queue: make(chan *pb.HTTPAccessLog, maxSize), } @@ -49,17 +50,30 @@ func (this *HTTPAccessLogQueue) Push(accessLog *pb.HTTPAccessLog) { // 上传访问日志 func (this *HTTPAccessLogQueue) loop() error { - accessLogs := []*pb.HTTPAccessLog{} - count := 0 + var accessLogs = []*pb.HTTPAccessLog{} + var count = 0 + var timestamp int64 + var requestId = 1_000_000 + Loop: for { select { case accessLog := <-this.queue: + if accessLog.Timestamp > timestamp { + requestId = 10_000_000 + timestamp = accessLog.Timestamp + } else { + requestId++ + } + + // timestamp + requestId + nodeId + accessLog.RequestId = strconv.FormatInt(accessLog.Timestamp, 10) + strconv.Itoa(requestId) + strconv.FormatInt(accessLog.NodeId, 10) + accessLogs = append(accessLogs, accessLog) count++ // 每次只提交 N 条访问日志,防止网络拥堵 - if count > 1000 { + if count > 2000 { break Loop } default: diff --git a/internal/nodes/http_request_log.go b/internal/nodes/http_request_log.go index 6c0e603..a8f197f 100644 --- a/internal/nodes/http_request_log.go +++ b/internal/nodes/http_request_log.go @@ -3,14 +3,10 @@ package nodes import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" - "strconv" "strings" - "sync/atomic" "time" ) -var requestId int64 = 1_0000_0000_0000_0000 - // 日志 func (this *HTTPRequest) log() { if this.disableLog { @@ -85,7 +81,7 @@ func (this *HTTPRequest) log() { } accessLog := &pb.HTTPAccessLog{ - RequestId: strconv.FormatInt(this.requestFromTime.UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + sharedNodeConfig.PaddedId(), + RequestId: "", NodeId: sharedNodeConfig.Id, ServerId: this.Server.Id, RemoteAddr: this.requestRemoteAddr(true),