Documentation ¶
Index ¶
- Variables
- type AddFunc
- type AllowedIPsRefCounter
- type Counter
- func (rm *Counter[Key, I, O]) Clear()
- func (rm *Counter[Key, I, O]) Decrement(key Key) (Ref[O], error)
- func (rm *Counter[Key, I, O]) DecrementWithID(id string) error
- func (rm *Counter[Key, I, O]) Flush() error
- func (rm *Counter[Key, I, O]) Get(key Key) (Ref[O], bool)
- func (rm *Counter[Key, I, O]) Increment(key Key, in I) (Ref[O], error)
- func (rm *Counter[Key, I, O]) IncrementWithID(id string, key Key, in I) (Ref[O], error)
- type Ref
- type RemoveFunc
- type RouteRefCounter
Constants ¶
This section is empty.
Variables ¶
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 ¶
AddFunc is the function type for adding a new key. Key is the type of the key (e.g., netip.Prefix).
type AllowedIPsRefCounter ¶
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 ¶
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 ¶
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]) Get ¶ added in v0.30.0
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.
type RemoveFunc ¶
RemoveFunc is the function type for removing a key.
type RouteRefCounter ¶
RouteRefCounter is a Counter for Route, it doesn't take any input on Increment and doesn't use any output on Decrement