counter

package
v1.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 13

Documentation

Overview

Package counter provides generic reference counter objects

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counter

type Counter[T comparable] map[T]int

Counter tracks references for comparable .

No thread safety is provided within this structure, the user is expected to handle concurrent access to this structure if it is used from multiple threads.

func (Counter[T]) Add

func (c Counter[T]) Add(key T) bool

Add increments the reference count for the specified key.

func (Counter[T]) DeepCopy

func (c Counter[T]) DeepCopy() Counter[T]

DeepCopy makes a new copy of the received Counter.

func (Counter[T]) Delete

func (c Counter[T]) Delete(key T) bool

Delete decrements the reference count for the specified key.

func (Counter[T]) Has added in v1.17.0

func (c Counter[T]) Has(key T) bool

Has returns true if the given key has a non-zero refcount.

type IntCounter

type IntCounter Counter[int]

IntCounter tracks references for integers with an optional limiter.

No threadsafety is provided within this structure, the user is expected to handle concurrent access to this structure if it is used from multiple threads.

func (IntCounter) Add

func (i IntCounter) Add(key int) (changed bool)

Add increments the reference count for the specified integer key.

func (IntCounter) DeepCopy

func (i IntCounter) DeepCopy() IntCounter

DeepCopy makes a new copy of the received IntCounter.

func (IntCounter) Delete

func (i IntCounter) Delete(key int) bool

Delete decrements the reference count for the specified integer key.

func (IntCounter) ToBPFData

func (i IntCounter) ToBPFData() []int

ToBPFData returns the keys as a slice, sorted from high to low.

type IntervalRangeCounter added in v1.19.0

type IntervalRangeCounter struct {
	RangeCounter
	// contains filtered or unexported fields
}

IntervalRangeCounter is a specialized RangeCounter that provides a IsElapsed() method to check if the time interval has elapsed since the first increment.

func NewIntervalRangeCounter added in v1.19.0

func NewIntervalRangeCounter(interval time.Duration) *IntervalRangeCounter

NewIntervalRangeCounter creates a new IntervalRangeCounter with the specified interval.

func (*IntervalRangeCounter) IsElapsed added in v1.19.0

func (c *IntervalRangeCounter) IsElapsed(now time.Time) bool

IsElapsed checks if the duration since the first increment until now exceeds the configured interval. It always returns false when the counter is empty as a "start time" is required as base to compute the interval.

type PrefixLengthCounter

type PrefixLengthCounter struct {
	lock.RWMutex
	// contains filtered or unexported fields
}

PrefixLengthCounter tracks references to prefix lengths, limited by the maxUniquePrefixes count. Neither of the IPv4 or IPv6 counters nested within may contain more keys than the specified maximum number of unique prefixes.

func DefaultPrefixLengthCounter

func DefaultPrefixLengthCounter() *PrefixLengthCounter

DefaultPrefixLengthCounter creates a default prefix length counter that already counts the minimum and maximum prefix lengths for IP hosts and default routes (ie, /32 and /0). As with NewPrefixLengthCounter, insertions are limited to the specified maximum number of unique prefix lengths.

func NewPrefixLengthCounter

func NewPrefixLengthCounter(maxUniquePrefixes6, maxUniquePrefixes4 int) *PrefixLengthCounter

NewPrefixLengthCounter returns a new PrefixLengthCounter which limits insertions to the specified maximum number of unique prefix lengths.

func (*PrefixLengthCounter) Add

func (p *PrefixLengthCounter) Add(prefixes []netip.Prefix) (bool, error)

Add increments references to prefix lengths for the specified IPNets to the counter. If the maximum number of unique prefix lengths would be exceeded, returns an error.

Returns true if adding these prefixes results in an increase in the total number of unique prefix lengths in the counter.

func (*PrefixLengthCounter) Delete

func (p *PrefixLengthCounter) Delete(prefixes []netip.Prefix) (changed bool)

Delete reduces references to prefix lengths in the specified IPNets from the counter. Returns true if removing references to these prefix lengths would result in a decrese in the total number of unique prefix lengths in the counter.

func (*PrefixLengthCounter) ToBPFData

func (p *PrefixLengthCounter) ToBPFData() (s6, s4 []int)

ToBPFData converts the counter into a set of prefix lengths that the BPF datapath can use for LPM lookup.

type RangeCount added in v1.19.0

type RangeCount struct {
	Count uint64
	First time.Time
	Last  time.Time
}

RangeCount represents a monotonically increasing count along with the first and last time it was incremented.

type RangeCounter added in v1.19.0

type RangeCounter struct {
	// contains filtered or unexported fields
}

RangeCounter is a simple counter that tracks a count and a time interval.

func NewRangeCounter added in v1.19.0

func NewRangeCounter() *RangeCounter

NewRangeCounter creates a new RangeCounter.

func (*RangeCounter) Clear added in v1.19.0

func (c *RangeCounter) Clear() RangeCount

Clear clears the counter and returns the existing count.

func (*RangeCounter) Increment added in v1.19.0

func (c *RangeCounter) Increment(now time.Time)

Increment increments the counter and updates the time range.

func (*RangeCounter) Peek added in v1.19.0

func (c *RangeCounter) Peek() RangeCount

Peek returns the current count.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL