diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index 26c0d0b..8f9d87c 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -321,14 +321,14 @@ func (this *HTTPRequest) doEnd() { if this.isCached { countCached = 1 - cachedBytes = this.writer.SentBodyBytes() + cachedBytes = this.writer.SentBodyBytes() + this.writer.SentHeaderBytes() } if this.isAttack { countAttacks = 1 attackBytes = this.CalculateSize() } - stats.SharedTrafficStatManager.Add(this.ReqServer.Id, this.ReqHost, this.writer.SentBodyBytes(), cachedBytes, 1, countCached, countAttacks, attackBytes, this.ReqServer.ShouldCheckTrafficLimit(), this.ReqServer.PlanId()) + stats.SharedTrafficStatManager.Add(this.ReqServer.Id, this.ReqHost, this.writer.SentBodyBytes()+this.writer.SentHeaderBytes(), cachedBytes, 1, countCached, countAttacks, attackBytes, this.ReqServer.ShouldCheckTrafficLimit(), this.ReqServer.PlanId()) } // 指标 @@ -1292,7 +1292,7 @@ func (this *HTTPRequest) CalculateSize() (size int64) { size += int64(len(this.RawReq.Proto)) + 1 for k, v := range this.RawReq.Header { for _, v1 := range v { - size += int64(len(k) + 2 /** : **/ + len(v1)) + size += int64(len(k) + 2 /** : **/ + len(v1) + 1) } } diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index dfa909e..a40338c 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -304,6 +304,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) { // 读取Header var headerBuf = []byte{} + this.writer.SetSentHeaderBytes(reader.HeaderSize()) err = reader.ReadHeader(buf, func(n int) (goNext bool, err error) { headerBuf = append(headerBuf, buf[:n]...) for { diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index 960b843..be9627a 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -58,8 +58,9 @@ type HTTPWriter struct { size int64 - statusCode int - sentBodyBytes int64 + statusCode int + sentBodyBytes int64 + sentHeaderBytes int64 isOk bool // 是否完全成功 isFinished bool // 是否已完成 @@ -692,6 +693,23 @@ func (this *HTTPWriter) SentBodyBytes() int64 { return this.sentBodyBytes } +// SentHeaderBytes 计算发送的Header字节数 +func (this *HTTPWriter) SentHeaderBytes() int64 { + if this.sentHeaderBytes > 0 { + return this.sentHeaderBytes + } + for k, v := range this.Header() { + for _, v1 := range v { + this.sentHeaderBytes += int64(len(k) + 2 + len(v1) + 1) + } + } + return this.sentHeaderBytes +} + +func (this *HTTPWriter) SetSentHeaderBytes(sentHeaderBytes int64) { + this.sentHeaderBytes = sentHeaderBytes +} + // WriteHeader 写入状态码 func (this *HTTPWriter) WriteHeader(statusCode int) { if this.rawWriter != nil { diff --git a/internal/stats/traffic_stat_manager.go b/internal/stats/traffic_stat_manager.go index d691e83..25fb01b 100644 --- a/internal/stats/traffic_stat_manager.go +++ b/internal/stats/traffic_stat_manager.go @@ -101,7 +101,7 @@ func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64, this.totalRequests++ - timestamp := utils.FloorUnixTime(300) + var timestamp = utils.FloorUnixTime(300) key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10) this.locker.Lock()