package eventbus import "sync" type RingBuffer[T any] struct { mu sync.Mutex buf []T head int tail int count int cap int } func NewRingBuffer[T any](capacity int) *RingBuffer[T] { return &RingBuffer[T]{ buf: make([]T, capacity), cap: capacity, } } func (r *RingBuffer[T]) Push(item T) { r.mu.Lock() defer r.mu.Unlock() r.buf[r.head] = item r.head = (r.head + 1) % r.cap if r.count == r.cap { r.tail = (r.tail + 1) % r.cap } else { r.count++ } } func (r *RingBuffer[T]) Pop() (T, bool) { r.mu.Lock() defer r.mu.Unlock() var zero T if r.count == 0 { return zero, false } item := r.buf[r.tail] r.buf[r.tail] = zero r.tail = (r.tail + 1) % r.cap r.count-- return item, true } func (r *RingBuffer[T]) Len() int { r.mu.Lock() defer r.mu.Unlock() return r.count }