mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
- Replace manual increment methods with atomic store operations in PathMetrics - Enhance GetStats method to handle different metric types and convert to value slice - Update RequestLog JSON tags for consistent naming - Add SafePathMetrics conversion for mixed pointer and value slices - Improve type safety and consistency in metrics tracking
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:"BytesSent"`
|
|
ClientIP string `json:"ClientIP"`
|
|
}
|
|
|
|
// 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
|
|
}
|