mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-08 03:00:27 +08:00
访问日志简化requestId生成方法
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ type HTTPAccessLogQueue struct {
|
|||||||
func NewHTTPAccessLogQueue() *HTTPAccessLogQueue {
|
func NewHTTPAccessLogQueue() *HTTPAccessLogQueue {
|
||||||
// 队列中最大的值,超出此数量的访问日志会被丢弃
|
// 队列中最大的值,超出此数量的访问日志会被丢弃
|
||||||
// TODO 需要可以在界面中设置
|
// TODO 需要可以在界面中设置
|
||||||
maxSize := 10000
|
maxSize := 20000
|
||||||
queue := &HTTPAccessLogQueue{
|
queue := &HTTPAccessLogQueue{
|
||||||
queue: make(chan *pb.HTTPAccessLog, maxSize),
|
queue: make(chan *pb.HTTPAccessLog, maxSize),
|
||||||
}
|
}
|
||||||
@@ -49,17 +50,30 @@ func (this *HTTPAccessLogQueue) Push(accessLog *pb.HTTPAccessLog) {
|
|||||||
|
|
||||||
// 上传访问日志
|
// 上传访问日志
|
||||||
func (this *HTTPAccessLogQueue) loop() error {
|
func (this *HTTPAccessLogQueue) loop() error {
|
||||||
accessLogs := []*pb.HTTPAccessLog{}
|
var accessLogs = []*pb.HTTPAccessLog{}
|
||||||
count := 0
|
var count = 0
|
||||||
|
var timestamp int64
|
||||||
|
var requestId = 1_000_000
|
||||||
|
|
||||||
Loop:
|
Loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case accessLog := <-this.queue:
|
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)
|
accessLogs = append(accessLogs, accessLog)
|
||||||
count++
|
count++
|
||||||
|
|
||||||
// 每次只提交 N 条访问日志,防止网络拥堵
|
// 每次只提交 N 条访问日志,防止网络拥堵
|
||||||
if count > 1000 {
|
if count > 2000 {
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -3,14 +3,10 @@ package nodes
|
|||||||
import (
|
import (
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var requestId int64 = 1_0000_0000_0000_0000
|
|
||||||
|
|
||||||
// 日志
|
// 日志
|
||||||
func (this *HTTPRequest) log() {
|
func (this *HTTPRequest) log() {
|
||||||
if this.disableLog {
|
if this.disableLog {
|
||||||
@@ -85,7 +81,7 @@ func (this *HTTPRequest) log() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
accessLog := &pb.HTTPAccessLog{
|
accessLog := &pb.HTTPAccessLog{
|
||||||
RequestId: strconv.FormatInt(this.requestFromTime.UnixNano(), 10) + strconv.FormatInt(atomic.AddInt64(&requestId, 1), 10) + sharedNodeConfig.PaddedId(),
|
RequestId: "",
|
||||||
NodeId: sharedNodeConfig.Id,
|
NodeId: sharedNodeConfig.Id,
|
||||||
ServerId: this.Server.Id,
|
ServerId: this.Server.Id,
|
||||||
RemoteAddr: this.requestRemoteAddr(true),
|
RemoteAddr: this.requestRemoteAddr(true),
|
||||||
|
|||||||
Reference in New Issue
Block a user