|
| 1 | +package hedge |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// LatencySnapshot contains latency quantiles. |
| 10 | +type LatencySnapshot struct { |
| 11 | + P50 time.Duration |
| 12 | + P90 time.Duration |
| 13 | + P95 time.Duration |
| 14 | + P99 time.Duration |
| 15 | +} |
| 16 | + |
| 17 | +// LatencyTracker tracks recent latency samples and calculates quantiles. |
| 18 | +type LatencyTracker interface { |
| 19 | + // Observe records a duration sample. |
| 20 | + Observe(d time.Duration) |
| 21 | + // Snapshot returns the current latency snapshot. |
| 22 | + Snapshot() LatencySnapshot |
| 23 | +} |
| 24 | + |
| 25 | +// RingBufferTracker implements LatencyTracker using a fixed-size ring buffer. |
| 26 | +// It is safe for concurrent use. |
| 27 | +type RingBufferTracker struct { |
| 28 | + mu sync.RWMutex |
| 29 | + samples []time.Duration |
| 30 | + idx int |
| 31 | + full bool |
| 32 | +} |
| 33 | + |
| 34 | +// NewRingBufferTracker creates a new tracker with the specified size. |
| 35 | +// Size must be greater than 0. |
| 36 | +func NewRingBufferTracker(size int) *RingBufferTracker { |
| 37 | + if size <= 0 { |
| 38 | + size = 256 // Default safe size |
| 39 | + } |
| 40 | + return &RingBufferTracker{ |
| 41 | + samples: make([]time.Duration, size), |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Observe records a duration sample. |
| 46 | +func (t *RingBufferTracker) Observe(d time.Duration) { |
| 47 | + t.mu.Lock() |
| 48 | + defer t.mu.Unlock() |
| 49 | + |
| 50 | + t.samples[t.idx] = d |
| 51 | + t.idx++ |
| 52 | + if t.idx >= len(t.samples) { |
| 53 | + t.idx = 0 |
| 54 | + t.full = true |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Snapshot returns the current latency snapshot. |
| 59 | +func (t *RingBufferTracker) Snapshot() LatencySnapshot { |
| 60 | + t.mu.RLock() |
| 61 | + defer t.mu.RUnlock() |
| 62 | + |
| 63 | + count := t.idx |
| 64 | + if t.full { |
| 65 | + count = len(t.samples) |
| 66 | + } |
| 67 | + |
| 68 | + if count == 0 { |
| 69 | + return LatencySnapshot{} |
| 70 | + } |
| 71 | + |
| 72 | + // Copy samples to avoid holding lock during sort |
| 73 | + // We only copy valid samples |
| 74 | + sorted := make([]time.Duration, count) |
| 75 | + if t.full { |
| 76 | + copy(sorted, t.samples) |
| 77 | + } else { |
| 78 | + copy(sorted, t.samples[:count]) |
| 79 | + } |
| 80 | + |
| 81 | + sort.Slice(sorted, func(i, j int) bool { |
| 82 | + return sorted[i] < sorted[j] |
| 83 | + }) |
| 84 | + |
| 85 | + return LatencySnapshot{ |
| 86 | + P50: quantile(sorted, 0.50), |
| 87 | + P90: quantile(sorted, 0.90), |
| 88 | + P95: quantile(sorted, 0.95), |
| 89 | + P99: quantile(sorted, 0.99), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func quantile(sorted []time.Duration, q float64) time.Duration { |
| 94 | + if len(sorted) == 0 { |
| 95 | + return 0 |
| 96 | + } |
| 97 | + // Use (N-1)*q to interpret index in 0-based array. |
| 98 | + // For N=100 (indices 0-99): |
| 99 | + // q=0.5 -> 49.5 -> 49 (Value 50) |
| 100 | + // q=0.99 -> 98.01 -> 98 (Value 99) |
| 101 | + idx := int(float64(len(sorted)-1) * q) |
| 102 | + if idx >= len(sorted) { |
| 103 | + idx = len(sorted) - 1 |
| 104 | + } |
| 105 | + if idx < 0 { |
| 106 | + idx = 0 |
| 107 | + } |
| 108 | + return sorted[idx] |
| 109 | +} |
0 commit comments