refcounter

package
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIgnore = errors.New("ignore")

ErrIgnore can be returned by AddFunc to indicate that the counter should not be incremented for the given key.

Functions

This section is empty.

Types

type AddFunc

type AddFunc[Key, I, O any] func(key Key, in I) (out O, err error)

AddFunc is the function type for adding a new key. Key is the type of the key (e.g., netip.Prefix).

type AllowedIPsRefCounter

type AllowedIPsRefCounter = Counter[netip.Prefix, string, string]

AllowedIPsRefCounter is a Counter for AllowedIPs, it takes a peer key on Increment and passes it back to Decrement

type Counter

type Counter[Key comparable, I, O any] struct {
	// contains filtered or unexported fields
}

Counter is a generic reference counter for managing keys and their associated data. Key: The type of the key (e.g., netip.Prefix, string).

I: The input type for the AddFunc. It is the input type for additional data needed when adding a key, it is passed as the second argument to AddFunc.

O: The output type for the AddFunc and RemoveFunc. This is the output returned by AddFunc. It is stored and passed to RemoveFunc when the reference count reaches 0.

The types can be aliased to a specific type using the following syntax:

type RouteRefCounter = Counter[netip.Prefix, any, any]

func New

func New[Key comparable, I, O any](add AddFunc[Key, I, O], remove RemoveFunc[Key, O]) *Counter[Key, I, O]

New creates a new Counter instance. Usage example:

counter := New[netip.Prefix, string, string](
    func(key netip.Prefix, in string) (out string, err error) { ... },
    func(key netip.Prefix, out string) error { ... },`
)

func (*Counter[Key, I, O]) Clear added in v0.30.0

func (rm *Counter[Key, I, O]) Clear()

Clear removes all references without calling RemoveFunc.

func (*Counter[Key, I, O]) Decrement

func (rm *Counter[Key, I, O]) Decrement(key Key) (Ref[O], error)

Decrement decrements the reference count for the given key. If the reference count reaches 0, the RemoveFunc is called.

func (*Counter[Key, I, O]) DecrementWithID

func (rm *Counter[Key, I, O]) DecrementWithID(id string) error

DecrementWithID decrements the reference count for all keys associated with the given ID. If the reference count reaches 0, the RemoveFunc is called.

func (*Counter[Key, I, O]) Flush

func (rm *Counter[Key, I, O]) Flush() error

Flush removes all references and calls RemoveFunc for each key.

func (*Counter[Key, I, O]) Get added in v0.30.0

func (rm *Counter[Key, I, O]) Get(key Key) (Ref[O], bool)

Get retrieves the current reference count and associated data for a key. If the key doesn't exist, it returns a zero value Ref and false.

func (*Counter[Key, I, O]) Increment

func (rm *Counter[Key, I, O]) Increment(key Key, in I) (Ref[O], error)

Increment increments the reference count for the given key. If this is the first reference to the key, the AddFunc is called.

func (*Counter[Key, I, O]) IncrementWithID

func (rm *Counter[Key, I, O]) IncrementWithID(id string, key Key, in I) (Ref[O], error)

IncrementWithID increments the reference count for the given key and groups it under the given ID. If this is the first reference to the key, the AddFunc is called.

type Ref

type Ref[O any] struct {
	Count int
	Out   O
}

Ref holds the reference count and associated data for a key.

type RemoveFunc

type RemoveFunc[Key, O any] func(key Key, out O) error

RemoveFunc is the function type for removing a key.

type RouteRefCounter

type RouteRefCounter = Counter[netip.Prefix, struct{}, struct{}]

RouteRefCounter is a Counter for Route, it doesn't take any input on Increment and doesn't use any output on Decrement

Jump to

Keyboard shortcuts

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