mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-16 23:01:31 +01:00
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:
parent
4b9a1a0087
commit
d42f0b6a21
@ -947,7 +947,7 @@ tailscale.com/cmd/k8s-operator dependencies: (generated by github.com/tailscale/
|
|||||||
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
|
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
|
||||||
tailscale.com/util/ringbuffer from tailscale.com/wgengine/magicsock
|
tailscale.com/util/ringlog from tailscale.com/wgengine/magicsock
|
||||||
tailscale.com/util/set from tailscale.com/cmd/k8s-operator+
|
tailscale.com/util/set from tailscale.com/cmd/k8s-operator+
|
||||||
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/slicesx from tailscale.com/appc+
|
tailscale.com/util/slicesx from tailscale.com/appc+
|
||||||
|
|||||||
@ -424,7 +424,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
|
|||||||
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
|
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
|
||||||
tailscale.com/util/ringbuffer from tailscale.com/wgengine/magicsock
|
tailscale.com/util/ringlog from tailscale.com/wgengine/magicsock
|
||||||
tailscale.com/util/set from tailscale.com/derp+
|
tailscale.com/util/set from tailscale.com/derp+
|
||||||
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/slicesx from tailscale.com/net/dns/recursive+
|
tailscale.com/util/slicesx from tailscale.com/net/dns/recursive+
|
||||||
|
|||||||
@ -376,7 +376,7 @@ tailscale.com/cmd/tsidp dependencies: (generated by github.com/tailscale/depawar
|
|||||||
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
tailscale.com/util/rands from tailscale.com/cmd/tsidp+
|
tailscale.com/util/rands from tailscale.com/cmd/tsidp+
|
||||||
tailscale.com/util/ringbuffer from tailscale.com/wgengine/magicsock
|
tailscale.com/util/ringlog from tailscale.com/wgengine/magicsock
|
||||||
tailscale.com/util/set from tailscale.com/control/controlclient+
|
tailscale.com/util/set from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/slicesx from tailscale.com/appc+
|
tailscale.com/util/slicesx from tailscale.com/appc+
|
||||||
|
|||||||
@ -371,7 +371,7 @@ tailscale.com/tsnet dependencies: (generated by github.com/tailscale/depaware)
|
|||||||
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
tailscale.com/util/race from tailscale.com/net/dns/resolver
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
|
tailscale.com/util/rands from tailscale.com/ipn/ipnlocal+
|
||||||
tailscale.com/util/ringbuffer from tailscale.com/wgengine/magicsock
|
tailscale.com/util/ringlog from tailscale.com/wgengine/magicsock
|
||||||
tailscale.com/util/set from tailscale.com/control/controlclient+
|
tailscale.com/util/set from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
tailscale.com/util/singleflight from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/slicesx from tailscale.com/appc+
|
tailscale.com/util/slicesx from tailscale.com/appc+
|
||||||
|
|||||||
@ -1,32 +1,31 @@
|
|||||||
// Copyright (c) Tailscale Inc & AUTHORS
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
// SPDX-License-Identifier: BSD-3-Clause
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
// Package ringbuffer contains a fixed-size concurrency-safe generic ring
|
// Package ringlog contains a limited-size concurrency-safe generic ring log.
|
||||||
// buffer.
|
package ringlog
|
||||||
package ringbuffer
|
|
||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
// New creates a new RingBuffer containing at most max items.
|
// New creates a new [RingLog] containing at most max items.
|
||||||
func New[T any](max int) *RingBuffer[T] {
|
func New[T any](max int) *RingLog[T] {
|
||||||
return &RingBuffer[T]{
|
return &RingLog[T]{
|
||||||
max: max,
|
max: max,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RingBuffer is a concurrency-safe ring buffer.
|
// RingLog is a concurrency-safe fixed size log window containing entries of [T].
|
||||||
type RingBuffer[T any] struct {
|
type RingLog[T any] struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
pos int
|
pos int
|
||||||
buf []T
|
buf []T
|
||||||
max int
|
max int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add appends a new item to the RingBuffer, possibly overwriting the oldest
|
// Add appends a new item to the [RingLog], possibly overwriting the oldest
|
||||||
// item in the buffer if it is already full.
|
// item in the log if it is already full.
|
||||||
//
|
//
|
||||||
// It does nothing if rb is nil.
|
// It does nothing if rb is nil.
|
||||||
func (rb *RingBuffer[T]) Add(t T) {
|
func (rb *RingLog[T]) Add(t T) {
|
||||||
if rb == nil {
|
if rb == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -40,11 +39,11 @@ func (rb *RingBuffer[T]) Add(t T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll returns a copy of all the entries in the ring buffer in the order they
|
// GetAll returns a copy of all the entries in the ring log in the order they
|
||||||
// were added.
|
// were added.
|
||||||
//
|
//
|
||||||
// It returns nil if rb is nil.
|
// It returns nil if rb is nil.
|
||||||
func (rb *RingBuffer[T]) GetAll() []T {
|
func (rb *RingLog[T]) GetAll() []T {
|
||||||
if rb == nil {
|
if rb == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -58,10 +57,10 @@ func (rb *RingBuffer[T]) GetAll() []T {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the number of elements in the ring buffer. Note that this value
|
// Len returns the number of elements in the ring log. Note that this value
|
||||||
// could change immediately after being returned if a concurrent caller
|
// could change immediately after being returned if a concurrent caller
|
||||||
// modifies the buffer.
|
// modifies the log.
|
||||||
func (rb *RingBuffer[T]) Len() int {
|
func (rb *RingLog[T]) Len() int {
|
||||||
if rb == nil {
|
if rb == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -70,8 +69,8 @@ func (rb *RingBuffer[T]) Len() int {
|
|||||||
return len(rb.buf)
|
return len(rb.buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear will empty the ring buffer.
|
// Clear will empty the ring log.
|
||||||
func (rb *RingBuffer[T]) Clear() {
|
func (rb *RingLog[T]) Clear() {
|
||||||
rb.mu.Lock()
|
rb.mu.Lock()
|
||||||
defer rb.mu.Unlock()
|
defer rb.mu.Unlock()
|
||||||
rb.pos = 0
|
rb.pos = 0
|
||||||
@ -1,14 +1,14 @@
|
|||||||
// Copyright (c) Tailscale Inc & AUTHORS
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||||||
// SPDX-License-Identifier: BSD-3-Clause
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
package ringbuffer
|
package ringlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRingBuffer(t *testing.T) {
|
func TestRingLog(t *testing.T) {
|
||||||
const numItems = 10
|
const numItems = 10
|
||||||
rb := New[int](numItems)
|
rb := New[int](numItems)
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ import (
|
|||||||
"tailscale.com/types/key"
|
"tailscale.com/types/key"
|
||||||
"tailscale.com/types/logger"
|
"tailscale.com/types/logger"
|
||||||
"tailscale.com/util/mak"
|
"tailscale.com/util/mak"
|
||||||
"tailscale.com/util/ringbuffer"
|
"tailscale.com/util/ringlog"
|
||||||
"tailscale.com/util/slicesx"
|
"tailscale.com/util/slicesx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ type endpoint struct {
|
|||||||
lastRecvWG mono.Time // last time there were incoming packets from this peer destined for wireguard-go (e.g. not disco)
|
lastRecvWG mono.Time // last time there were incoming packets from this peer destined for wireguard-go (e.g. not disco)
|
||||||
lastRecvUDPAny mono.Time // last time there were incoming UDP packets from this peer of any kind
|
lastRecvUDPAny mono.Time // last time there were incoming UDP packets from this peer of any kind
|
||||||
numStopAndResetAtomic int64
|
numStopAndResetAtomic int64
|
||||||
debugUpdates *ringbuffer.RingBuffer[EndpointChange]
|
debugUpdates *ringlog.RingLog[EndpointChange]
|
||||||
|
|
||||||
// These fields are initialized once and never modified.
|
// These fields are initialized once and never modified.
|
||||||
c *Conn
|
c *Conn
|
||||||
|
|||||||
@ -62,7 +62,7 @@ import (
|
|||||||
"tailscale.com/util/clientmetric"
|
"tailscale.com/util/clientmetric"
|
||||||
"tailscale.com/util/eventbus"
|
"tailscale.com/util/eventbus"
|
||||||
"tailscale.com/util/mak"
|
"tailscale.com/util/mak"
|
||||||
"tailscale.com/util/ringbuffer"
|
"tailscale.com/util/ringlog"
|
||||||
"tailscale.com/util/set"
|
"tailscale.com/util/set"
|
||||||
"tailscale.com/util/testenv"
|
"tailscale.com/util/testenv"
|
||||||
"tailscale.com/util/usermetric"
|
"tailscale.com/util/usermetric"
|
||||||
@ -3112,7 +3112,7 @@ func (c *Conn) updateNodes(update NodeViewsUpdate) (peersChanged bool) {
|
|||||||
// ~1MB on mobile but we never used the data so the memory was just
|
// ~1MB on mobile but we never used the data so the memory was just
|
||||||
// wasted.
|
// wasted.
|
||||||
default:
|
default:
|
||||||
ep.debugUpdates = ringbuffer.New[EndpointChange](entriesPerBuffer)
|
ep.debugUpdates = ringlog.New[EndpointChange](entriesPerBuffer)
|
||||||
}
|
}
|
||||||
if n.Addresses().Len() > 0 {
|
if n.Addresses().Len() > 0 {
|
||||||
ep.nodeAddr = n.Addresses().At(0).Addr()
|
ep.nodeAddr = n.Addresses().At(0).Addr()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user