|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "sync/atomic" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/getsentry/sentry-go/internal/ratelimit" |
| 9 | +) |
| 10 | + |
| 11 | +const defaultCapacity = 100 |
| 12 | + |
| 13 | +// Buffer is a thread-safe ring buffer with overflow policies. |
| 14 | +type Buffer[T any] struct { |
| 15 | + mu sync.RWMutex |
| 16 | + items []T |
| 17 | + head int |
| 18 | + tail int |
| 19 | + size int |
| 20 | + capacity int |
| 21 | + |
| 22 | + category ratelimit.Category |
| 23 | + priority ratelimit.Priority |
| 24 | + overflowPolicy OverflowPolicy |
| 25 | + |
| 26 | + offered int64 |
| 27 | + dropped int64 |
| 28 | + onDropped func(item T, reason string) |
| 29 | +} |
| 30 | + |
| 31 | +func NewBuffer[T any](category ratelimit.Category, capacity int, overflowPolicy OverflowPolicy) *Buffer[T] { |
| 32 | + if capacity <= 0 { |
| 33 | + capacity = defaultCapacity |
| 34 | + } |
| 35 | + |
| 36 | + return &Buffer[T]{ |
| 37 | + items: make([]T, capacity), |
| 38 | + capacity: capacity, |
| 39 | + category: category, |
| 40 | + priority: category.GetPriority(), |
| 41 | + overflowPolicy: overflowPolicy, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (b *Buffer[T]) SetDroppedCallback(callback func(item T, reason string)) { |
| 46 | + b.mu.Lock() |
| 47 | + defer b.mu.Unlock() |
| 48 | + b.onDropped = callback |
| 49 | +} |
| 50 | + |
| 51 | +// Offer adds an item to the buffer, returns false if dropped due to overflow. |
| 52 | +func (b *Buffer[T]) Offer(item T) bool { |
| 53 | + atomic.AddInt64(&b.offered, 1) |
| 54 | + |
| 55 | + b.mu.Lock() |
| 56 | + defer b.mu.Unlock() |
| 57 | + |
| 58 | + if b.size < b.capacity { |
| 59 | + b.items[b.tail] = item |
| 60 | + b.tail = (b.tail + 1) % b.capacity |
| 61 | + b.size++ |
| 62 | + return true |
| 63 | + } |
| 64 | + |
| 65 | + switch b.overflowPolicy { |
| 66 | + case OverflowPolicyDropOldest: |
| 67 | + oldItem := b.items[b.head] |
| 68 | + b.items[b.head] = item |
| 69 | + b.head = (b.head + 1) % b.capacity |
| 70 | + b.tail = (b.tail + 1) % b.capacity |
| 71 | + |
| 72 | + atomic.AddInt64(&b.dropped, 1) |
| 73 | + if b.onDropped != nil { |
| 74 | + b.onDropped(oldItem, "buffer_full_drop_oldest") |
| 75 | + } |
| 76 | + return true |
| 77 | + |
| 78 | + case OverflowPolicyDropNewest: |
| 79 | + atomic.AddInt64(&b.dropped, 1) |
| 80 | + if b.onDropped != nil { |
| 81 | + b.onDropped(item, "buffer_full_drop_newest") |
| 82 | + } |
| 83 | + return false |
| 84 | + |
| 85 | + default: |
| 86 | + atomic.AddInt64(&b.dropped, 1) |
| 87 | + if b.onDropped != nil { |
| 88 | + b.onDropped(item, "unknown_overflow_policy") |
| 89 | + } |
| 90 | + return false |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Poll removes and returns the oldest item, false if empty. |
| 95 | +func (b *Buffer[T]) Poll() (T, bool) { |
| 96 | + b.mu.Lock() |
| 97 | + defer b.mu.Unlock() |
| 98 | + |
| 99 | + var zero T |
| 100 | + if b.size == 0 { |
| 101 | + return zero, false |
| 102 | + } |
| 103 | + |
| 104 | + item := b.items[b.head] |
| 105 | + b.items[b.head] = zero |
| 106 | + b.head = (b.head + 1) % b.capacity |
| 107 | + b.size-- |
| 108 | + |
| 109 | + return item, true |
| 110 | +} |
| 111 | + |
| 112 | +// PollBatch removes and returns up to maxItems. |
| 113 | +func (b *Buffer[T]) PollBatch(maxItems int) []T { |
| 114 | + if maxItems <= 0 { |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + b.mu.Lock() |
| 119 | + defer b.mu.Unlock() |
| 120 | + |
| 121 | + if b.size == 0 { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + itemCount := maxItems |
| 126 | + if itemCount > b.size { |
| 127 | + itemCount = b.size |
| 128 | + } |
| 129 | + |
| 130 | + result := make([]T, itemCount) |
| 131 | + var zero T |
| 132 | + |
| 133 | + for i := 0; i < itemCount; i++ { |
| 134 | + result[i] = b.items[b.head] |
| 135 | + b.items[b.head] = zero |
| 136 | + b.head = (b.head + 1) % b.capacity |
| 137 | + b.size-- |
| 138 | + } |
| 139 | + |
| 140 | + return result |
| 141 | +} |
| 142 | + |
| 143 | +// Drain removes and returns all items. |
| 144 | +func (b *Buffer[T]) Drain() []T { |
| 145 | + b.mu.Lock() |
| 146 | + defer b.mu.Unlock() |
| 147 | + |
| 148 | + if b.size == 0 { |
| 149 | + return nil |
| 150 | + } |
| 151 | + |
| 152 | + result := make([]T, b.size) |
| 153 | + index := 0 |
| 154 | + var zero T |
| 155 | + |
| 156 | + for i := 0; i < b.size; i++ { |
| 157 | + pos := (b.head + i) % b.capacity |
| 158 | + result[index] = b.items[pos] |
| 159 | + b.items[pos] = zero |
| 160 | + index++ |
| 161 | + } |
| 162 | + |
| 163 | + b.head = 0 |
| 164 | + b.tail = 0 |
| 165 | + b.size = 0 |
| 166 | + |
| 167 | + return result |
| 168 | +} |
| 169 | + |
| 170 | +// Peek returns the oldest item without removing it, false if empty. |
| 171 | +func (b *Buffer[T]) Peek() (T, bool) { |
| 172 | + b.mu.RLock() |
| 173 | + defer b.mu.RUnlock() |
| 174 | + |
| 175 | + var zero T |
| 176 | + if b.size == 0 { |
| 177 | + return zero, false |
| 178 | + } |
| 179 | + |
| 180 | + return b.items[b.head], true |
| 181 | +} |
| 182 | + |
| 183 | +func (b *Buffer[T]) Size() int { |
| 184 | + b.mu.RLock() |
| 185 | + defer b.mu.RUnlock() |
| 186 | + return b.size |
| 187 | +} |
| 188 | + |
| 189 | +func (b *Buffer[T]) Capacity() int { |
| 190 | + return b.capacity |
| 191 | +} |
| 192 | + |
| 193 | +func (b *Buffer[T]) Category() ratelimit.Category { |
| 194 | + return b.category |
| 195 | +} |
| 196 | + |
| 197 | +func (b *Buffer[T]) Priority() ratelimit.Priority { |
| 198 | + return b.priority |
| 199 | +} |
| 200 | + |
| 201 | +func (b *Buffer[T]) IsEmpty() bool { |
| 202 | + b.mu.RLock() |
| 203 | + defer b.mu.RUnlock() |
| 204 | + return b.size == 0 |
| 205 | +} |
| 206 | + |
| 207 | +func (b *Buffer[T]) IsFull() bool { |
| 208 | + b.mu.RLock() |
| 209 | + defer b.mu.RUnlock() |
| 210 | + return b.size == b.capacity |
| 211 | +} |
| 212 | + |
| 213 | +func (b *Buffer[T]) Utilization() float64 { |
| 214 | + b.mu.RLock() |
| 215 | + defer b.mu.RUnlock() |
| 216 | + return float64(b.size) / float64(b.capacity) |
| 217 | +} |
| 218 | + |
| 219 | +func (b *Buffer[T]) OfferedCount() int64 { |
| 220 | + return atomic.LoadInt64(&b.offered) |
| 221 | +} |
| 222 | + |
| 223 | +func (b *Buffer[T]) DroppedCount() int64 { |
| 224 | + return atomic.LoadInt64(&b.dropped) |
| 225 | +} |
| 226 | + |
| 227 | +func (b *Buffer[T]) AcceptedCount() int64 { |
| 228 | + return b.OfferedCount() - b.DroppedCount() |
| 229 | +} |
| 230 | + |
| 231 | +func (b *Buffer[T]) DropRate() float64 { |
| 232 | + offered := b.OfferedCount() |
| 233 | + if offered == 0 { |
| 234 | + return 0.0 |
| 235 | + } |
| 236 | + return float64(b.DroppedCount()) / float64(offered) |
| 237 | +} |
| 238 | + |
| 239 | +func (b *Buffer[T]) Clear() { |
| 240 | + b.mu.Lock() |
| 241 | + defer b.mu.Unlock() |
| 242 | + |
| 243 | + var zero T |
| 244 | + for i := 0; i < b.capacity; i++ { |
| 245 | + b.items[i] = zero |
| 246 | + } |
| 247 | + |
| 248 | + b.head = 0 |
| 249 | + b.tail = 0 |
| 250 | + b.size = 0 |
| 251 | +} |
| 252 | + |
| 253 | +func (b *Buffer[T]) GetMetrics() BufferMetrics { |
| 254 | + b.mu.RLock() |
| 255 | + size := b.size |
| 256 | + util := float64(b.size) / float64(b.capacity) |
| 257 | + b.mu.RUnlock() |
| 258 | + |
| 259 | + return BufferMetrics{ |
| 260 | + Category: b.category, |
| 261 | + Priority: b.priority, |
| 262 | + Capacity: b.capacity, |
| 263 | + Size: size, |
| 264 | + Utilization: util, |
| 265 | + OfferedCount: b.OfferedCount(), |
| 266 | + DroppedCount: b.DroppedCount(), |
| 267 | + AcceptedCount: b.AcceptedCount(), |
| 268 | + DropRate: b.DropRate(), |
| 269 | + LastUpdated: time.Now(), |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +type BufferMetrics struct { |
| 274 | + Category ratelimit.Category `json:"category"` |
| 275 | + Priority ratelimit.Priority `json:"priority"` |
| 276 | + Capacity int `json:"capacity"` |
| 277 | + Size int `json:"size"` |
| 278 | + Utilization float64 `json:"utilization"` |
| 279 | + OfferedCount int64 `json:"offered_count"` |
| 280 | + DroppedCount int64 `json:"dropped_count"` |
| 281 | + AcceptedCount int64 `json:"accepted_count"` |
| 282 | + DropRate float64 `json:"drop_rate"` |
| 283 | + LastUpdated time.Time `json:"last_updated"` |
| 284 | +} |
0 commit comments