mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
- Refactor Collector struct to use more efficient atomic counters and data structures - Implement robust bandwidth tracking with sliding window mechanism - Enhance path metrics with atomic operations and improved type safety - Add data consistency checker for metrics integrity - Optimize request logging with a more memory-efficient queue - Simplify metrics collection and reporting logic - Improve frontend metrics display with more detailed status code visualization
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package models
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// RequestLog 请求日志
|
|
type RequestLog struct {
|
|
Time time.Time `json:"time"`
|
|
Path string `json:"path"`
|
|
Status int `json:"status"`
|
|
Latency int64 `json:"latency"`
|
|
BytesSent int64 `json:"bytes_sent"`
|
|
ClientIP string `json:"client_ip"`
|
|
}
|
|
|
|
// RequestQueue 请求队列
|
|
type RequestQueue struct {
|
|
sync.RWMutex
|
|
items []RequestLog
|
|
size int
|
|
cursor int
|
|
}
|
|
|
|
// NewRequestQueue 创建新的请求队列
|
|
func NewRequestQueue(size int) *RequestQueue {
|
|
return &RequestQueue{
|
|
items: make([]RequestLog, size),
|
|
size: size,
|
|
}
|
|
}
|
|
|
|
// Push 添加请求日志
|
|
func (q *RequestQueue) Push(log RequestLog) {
|
|
q.Lock()
|
|
defer q.Unlock()
|
|
q.items[q.cursor] = log
|
|
q.cursor = (q.cursor + 1) % q.size
|
|
}
|
|
|
|
// GetAll 获取所有请求日志
|
|
func (q *RequestQueue) GetAll() []RequestLog {
|
|
q.RLock()
|
|
defer q.RUnlock()
|
|
result := make([]RequestLog, 0, q.size)
|
|
for i := 0; i < q.size; i++ {
|
|
idx := (q.cursor - i - 1 + q.size) % q.size
|
|
if !q.items[idx].Time.IsZero() {
|
|
result = append(result, q.items[idx])
|
|
}
|
|
}
|
|
return result
|
|
}
|