util/ringbuffer: rename to ringlog

I need a ringbuffer in the more traditional sense, one that has a notion
of item removal as well as tail loss on overrun. This implementation is
really a clearable log window, and is used as such where it is used.

Updates #cleanup
Updates tailscale/corp#31762

Signed-off-by: James Tucker <james@tailscale.com>
This commit is contained in:
James Tucker
2025-08-28 12:24:21 -07:00
committed by James Tucker
parent 4b9a1a0087
commit d42f0b6a21
8 changed files with 27 additions and 28 deletions
+78
View File
@@ -0,0 +1,78 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
// Package ringlog contains a limited-size concurrency-safe generic ring log.
package ringlog
import "sync"
// New creates a new [RingLog] containing at most max items.
func New[T any](max int) *RingLog[T] {
return &RingLog[T]{
max: max,
}
}
// RingLog is a concurrency-safe fixed size log window containing entries of [T].
type RingLog[T any] struct {
mu sync.Mutex
pos int
buf []T
max int
}
// Add appends a new item to the [RingLog], possibly overwriting the oldest
// item in the log if it is already full.
//
// It does nothing if rb is nil.
func (rb *RingLog[T]) Add(t T) {
if rb == nil {
return
}
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 log in the order they
// were added.
//
// It returns nil if rb is nil.
func (rb *RingLog[T]) GetAll() []T {
if rb == nil {
return nil
}
rb.mu.Lock()
defer rb.mu.Unlock()
out := make([]T, len(rb.buf))
for i := range len(rb.buf) {
x := (rb.pos + i) % rb.max
out[i] = rb.buf[x]
}
return out
}
// Len returns the number of elements in the ring log. Note that this value
// could change immediately after being returned if a concurrent caller
// modifies the log.
func (rb *RingLog[T]) Len() int {
if rb == nil {
return 0
}
rb.mu.Lock()
defer rb.mu.Unlock()
return len(rb.buf)
}
// Clear will empty the ring log.
func (rb *RingLog[T]) Clear() {
rb.mu.Lock()
defer rb.mu.Unlock()
rb.pos = 0
rb.buf = nil
}
+55
View File
@@ -0,0 +1,55 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package ringlog
import (
"reflect"
"testing"
)
func TestRingLog(t *testing.T) {
const numItems = 10
rb := New[int](numItems)
for i := range numItems - 1 {
rb.Add(i)
}
t.Run("NotFull", func(t *testing.T) {
if ll := rb.Len(); ll != numItems-1 {
t.Fatalf("got len %d; want %d", ll, numItems-1)
}
all := rb.GetAll()
want := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
if !reflect.DeepEqual(all, want) {
t.Fatalf("items mismatch\ngot: %v\nwant %v", all, want)
}
})
t.Run("Full", func(t *testing.T) {
// Append items to evict something
rb.Add(98)
rb.Add(99)
if ll := rb.Len(); ll != numItems {
t.Fatalf("got len %d; want %d", ll, numItems)
}
all := rb.GetAll()
want := []int{1, 2, 3, 4, 5, 6, 7, 8, 98, 99}
if !reflect.DeepEqual(all, want) {
t.Fatalf("items mismatch\ngot: %v\nwant %v", all, want)
}
})
t.Run("Clear", func(t *testing.T) {
rb.Clear()
if ll := rb.Len(); ll != 0 {
t.Fatalf("got len %d; want 0", ll)
}
all := rb.GetAll()
if len(all) != 0 {
t.Fatalf("got non-empty list; want empty")
}
})
}