2023-02-21 19:01:07 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
// Package ringbuffer contains a fixed-size concurrency-safe generic ring
|
|
|
|
// buffer.
|
|
|
|
package ringbuffer
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
// New creates a new RingBuffer containing at most max items.
|
|
|
|
func New[T any](max int) *RingBuffer[T] {
|
|
|
|
return &RingBuffer[T]{
|
|
|
|
max: max,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RingBuffer is a concurrency-safe ring buffer.
|
|
|
|
type RingBuffer[T any] struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
pos int
|
|
|
|
buf []T
|
|
|
|
max int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add appends a new item to the RingBuffer, possibly overwriting the oldest
|
|
|
|
// item in the buffer if it is already full.
|
2024-03-21 04:09:23 +00:00
|
|
|
//
|
|
|
|
// It does nothing if rb is nil.
|
2023-02-21 19:01:07 +00:00
|
|
|
func (rb *RingBuffer[T]) Add(t T) {
|
2024-03-21 04:09:23 +00:00
|
|
|
if rb == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-21 19:01:07 +00:00
|
|
|
rb.mu.Lock()
|
|
|
|
defer rb.mu.Unlock()
|
|
|
|
if len(rb.buf) < rb.max {
|
|
|
|
rb.buf = append(rb.buf, t)
|
|
|
|
} else {
|
|
|
|
rb.buf[rb.pos] = t
|
|
|
|
rb.pos = (rb.pos + 1) % rb.max
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll returns a copy of all the entries in the ring buffer in the order they
|
|
|
|
// were added.
|
2024-03-21 04:09:23 +00:00
|
|
|
//
|
|
|
|
// It returns nil if rb is nil.
|
2023-02-21 19:01:07 +00:00
|
|
|
func (rb *RingBuffer[T]) GetAll() []T {
|
|
|
|
if rb == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rb.mu.Lock()
|
|
|
|
defer rb.mu.Unlock()
|
|
|
|
out := make([]T, len(rb.buf))
|
2024-04-16 20:15:13 +00:00
|
|
|
for i := range len(rb.buf) {
|
2023-02-21 19:01:07 +00:00
|
|
|
x := (rb.pos + i) % rb.max
|
|
|
|
out[i] = rb.buf[x]
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of elements in the ring buffer. Note that this value
|
|
|
|
// could change immediately after being returned if a concurrent caller
|
|
|
|
// modifies the buffer.
|
|
|
|
func (rb *RingBuffer[T]) Len() int {
|
|
|
|
if rb == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
rb.mu.Lock()
|
|
|
|
defer rb.mu.Unlock()
|
|
|
|
return len(rb.buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear will empty the ring buffer.
|
|
|
|
func (rb *RingBuffer[T]) Clear() {
|
|
|
|
rb.mu.Lock()
|
|
|
|
defer rb.mu.Unlock()
|
|
|
|
rb.pos = 0
|
|
|
|
rb.buf = nil
|
|
|
|
}
|