上传访问日志
()
| 68 | |
| 69 | // 上传访问日志 |
| 70 | func (this *HTTPAccessLogQueue) loop() error { |
| 71 | const maxLen = 2000 |
| 72 | var accessLogs = make([]*pb.HTTPAccessLog, 0, maxLen) |
| 73 | var count = 0 |
| 74 | |
| 75 | Loop: |
| 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 |