MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / loop

Method loop

internal/nodes/http_access_log_queue.go:70–141  ·  view source on GitHub ↗

上传访问日志

()

Source from the content-addressed store, hash-verified

68
69// 上传访问日志
70func (this *HTTPAccessLogQueue) loop() error {
71 const maxLen = 2000
72 var accessLogs = make([]*pb.HTTPAccessLog, 0, maxLen)
73 var count = 0
74
75Loop:
76 for {
77 select {
78 case accessLog := <-this.queue:
79 accessLogs = append(accessLogs, accessLog)
80 count++
81
82 // 每次只提交 N 条访问日志,防止网络拥堵
83 if count >= maxLen {
84 break Loop
85 }
86 default:
87 break Loop
88 }
89 }
90
91 if len(accessLogs) == 0 {
92 return nil
93 }
94
95 // 发送到本地
96 if sharedHTTPAccessLogViewer.HasConns() {
97 for _, accessLog := range accessLogs {
98 sharedHTTPAccessLogViewer.Send(accessLog)
99 }
100 }
101
102 // 发送到API
103 if this.rpcClient == nil {
104 client, err := rpc.SharedRPC()
105 if err != nil {
106 return err
107 }
108 this.rpcClient = client
109 }
110
111 _, err := this.rpcClient.HTTPAccessLogRPC.CreateHTTPAccessLogs(this.rpcClient.Context(), &pb.CreateHTTPAccessLogsRequest{HttpAccessLogs: accessLogs})
112 if err != nil {
113 // 是否包含了invalid UTF-8
114 if strings.Contains(err.Error(), "string field contains invalid UTF-8") {
115 for _, accessLog := range accessLogs {
116 this.ToValidUTF8(accessLog)
117 }
118
119 // 重新提交
120 _, err = this.rpcClient.HTTPAccessLogRPC.CreateHTTPAccessLogs(this.rpcClient.Context(), &pb.CreateHTTPAccessLogsRequest{HttpAccessLogs: accessLogs})
121 return err
122 }
123
124 // 是否请求内容过大
125 statusCode, ok := status.FromError(err)
126 if ok && statusCode.Code() == codes.ResourceExhausted {
127 // 去除Body

Callers 1

StartMethod · 0.95

Calls 8

ToValidUTF8Method · 0.95
SharedRPCFunction · 0.92
HasConnsMethod · 0.80
ContextMethod · 0.80
CodeMethod · 0.65
SendMethod · 0.45
ContainsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected