utils

package
v4.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: MIT Imports: 25 Imported by: 6

Documentation

Index

Constants

View Source
const (
	LogLevelError = LogLevel(1 << iota)
	LogLevelInfo
	LogLevelNotice
	LogLevelDebug
)
View Source
const (
	IFA_F_SECONDARY = 0x01
	IFA_F_TEMPORARY = IFA_F_SECONDARY

	IFA_F_NODAD          = 0x02
	IFA_F_OPTIMISTIC     = 0x04
	IFA_F_DADFAILED      = 0x08
	IFA_F_HOMEADDRESS    = 0x10
	IFA_F_DEPRECATED     = 0x20
	IFA_F_TENTATIVE      = 0x40
	IFA_F_PERMANENT      = 0x80
	IFA_F_MANAGETEMPADDR = 0x100
	IFA_F_NOPREFIXROUTE  = 0x200
	IFA_F_MCAUTOJOIN     = 0x400
)
View Source
const IFA_FLAGS = 0x8

Variables

View Source
var ErrNonCanonicalEncoding = errors.New("binary: varint has non canonical encoding")
View Source
var GOMAXPROCS = min(runtime.GOMAXPROCS(0), runtime.NumCPU())
View Source
var GlobalLogLevel = LogLevelError | LogLevelInfo
View Source
var LogFile bool
View Source
var LogFunc bool

Functions

func CanonicalUvarint added in v4.1.0

func CanonicalUvarint(buf []byte) (uint64, int)

CanonicalUvarint decodes a uint64 from buf and returns that value and the number of bytes read (> 0). If an error occurred, the value is 0 and the number of bytes n is <= 0 meaning:

  • n == 0: buf too small;
  • n < 0: value larger than 64 bits (overflow) and -n is the number of bytes read.

The function errors if non-canonical bytes were read.

func ContextTick

func ContextTick(ctx context.Context, d time.Duration) <-chan time.Time

func Debugf

func Debugf(prefix, format string, v ...any)

func Div128

func Div128(hi, lo, y uint64) (hiQuo, loQuo uint64)

func Error

func Error(v ...any)

func Errorf

func Errorf(prefix, format string, v ...any)

func Fatalf

func Fatalf(format string, v ...any)

func GetOutboundIPv6

func GetOutboundIPv6() ([]netip.Addr, error)

func IsLogLevelDebug added in v4.1.0

func IsLogLevelDebug() bool

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

func Logf

func Logf(prefix, format string, v ...any)

func MarshalJSON

func MarshalJSON(val any) ([]byte, error)

func MarshalJSONIndent

func MarshalJSONIndent(val any, indent string) ([]byte, error)

func NetIPGetEUI48Information

func NetIPGetEUI48Information(addr netip.Addr)

func NetIPIsCGNAT

func NetIPIsCGNAT(addr netip.Addr) bool

func NewJSONDecoder

func NewJSONDecoder(reader io.Reader) *gojson.Decoder

func NewJSONEncoder

func NewJSONEncoder(writer io.Writer) *gojson.Encoder

func Noticef

func Noticef(prefix, format string, v ...any)

func NthElementSlice

func NthElementSlice[S ~[]E, E cmp.Ordered](s S, k int)

NthElementSlice QuickSelect implementation k is the desired index value, where array[k] is the k+1 smallest element values between s[0, k-1] are guaranteed <= to s[k] values between s[k+1, len(s)] are guaranteed >= to s[k]

func NthElementSliceFunc

func NthElementSliceFunc[S ~[]E, E any](s S, cmp func(a, b E) int, k int)

NthElementSliceFunc QuickSelect implementation k is the desired index value, where array[k] is the k+1 smallest element values between s[0, k-1] are guaranteed <= to s[k] values between s[k+1, len(s)] are guaranteed >= to s[k]

func Panic

func Panic(v ...any)

func Panicf

func Panicf(format string, v ...any)

func ParseUint64

func ParseUint64(s []byte) (uint64, error)

ParseUint64 parses uint64 from s.

It is equivalent to strconv.ParseUint(s, 10, 64), but is faster.

From https://github.com/valyala/fastjson

func PreviousPowerOfTwo

func PreviousPowerOfTwo(x uint64) int

func Print

func Print(v ...any)

func ReadCanonicalUvarint added in v4.1.0

func ReadCanonicalUvarint(r io.ByteReader) (uint64, error)

ReadCanonicalUvarint reads an encoded unsigned integer from r and returns it as a uint64. The error is ErrNonCanonicalEncoding if non-canonical bytes were read. The error is io.EOF only if no bytes were read. If an io.EOF happens after reading some but not all the bytes, ReadCanonicalUvarint returns io.ErrUnexpectedEOF.

func ReadFullProgressive added in v4.7.0

func ReadFullProgressive[T ~[]byte](r io.Reader, dst *T, size int) (n int, err error)

ReadFullProgressive Reads into buf up to size bytes by doubling size each time

func SiUnits

func SiUnits(number float64, decimals int) string

func SliceCount

func SliceCount[S ~[]E, E any](s S, f func(E) bool) (count int)

func SplitWork

func SplitWork(routines int, workSize uint64, do func(workIndex uint64, routineIndex int) error, init func(routines, routineIndex int) error) error

func UVarInt64Size

func UVarInt64Size[T uint64 | int | uint8](v T) (n int)

func UVarInt64SliceSize

func UVarInt64SliceSize[T uint64 | int](v []T) (n int)

func UnmarshalJSON

func UnmarshalJSON(data []byte, val any) error

func XMRUnits

func XMRUnits(v uint64) string

func XorShift64Star

func XorShift64Star(x uint64) uint64

XorShift64Star Implementation of xorshift* https://en.wikipedia.org/wiki/Xorshift#xorshift* x must be initialized to a non-zero value

Types

type Cache

type Cache[K comparable, T any] interface {
	Get(key K) (value T, ok bool)
	Set(key K, value T)
	Delete(key K)
	Clear()
	Stats() (hits, misses uint64)
}

type CircularBuffer

type CircularBuffer[T comparable] struct {
	// contains filtered or unexported fields
}

func NewCircularBuffer

func NewCircularBuffer[T comparable](size int) *CircularBuffer[T]

func (*CircularBuffer[T]) Current

func (b *CircularBuffer[T]) Current() T

func (*CircularBuffer[T]) Exists

func (b *CircularBuffer[T]) Exists(value T) bool

func (*CircularBuffer[T]) Get

func (b *CircularBuffer[T]) Get(index uint32) T

func (*CircularBuffer[T]) Push

func (b *CircularBuffer[T]) Push(value T)

func (*CircularBuffer[T]) PushUnique

func (b *CircularBuffer[T]) PushUnique(value T) bool

func (*CircularBuffer[T]) Replace

func (b *CircularBuffer[T]) Replace(value, replace T)

func (*CircularBuffer[T]) Reverse

func (b *CircularBuffer[T]) Reverse()

func (*CircularBuffer[T]) Slice

func (b *CircularBuffer[T]) Slice() []T

type ExtendedIPFlags

type ExtendedIPFlags uint
const (
	FlagTemporary ExtendedIPFlags = 1 << iota
	FlagPermanent
	FlagNoPrefixRoute
	FlagManageTempAddress
	FlagStablePrivacy
	FlagDeprecated
)

type ExtendedIPNet

type ExtendedIPNet struct {
	IP    net.IP     // network number
	Mask  net.IPMask // network mask
	Flags ExtendedIPFlags
}

func InterfaceAddrs

func InterfaceAddrs(ifi *net.Interface) ([]*ExtendedIPNet, error)

InterfaceAddrs returns a list of unicast interface addresses for a specific interface.

func (*ExtendedIPNet) String

func (n *ExtendedIPNet) String() string

type LRUCache

type LRUCache[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewLRUCache

func NewLRUCache[K comparable, T any](size int) *LRUCache[K, T]

func (*LRUCache[K, T]) Clear

func (c *LRUCache[K, T]) Clear()

func (*LRUCache[K, T]) Delete

func (c *LRUCache[K, T]) Delete(key K)

func (*LRUCache[K, T]) Get

func (c *LRUCache[K, T]) Get(key K) (value T, ok bool)

func (*LRUCache[K, T]) Set

func (c *LRUCache[K, T]) Set(key K, value T)

func (*LRUCache[K, T]) Stats

func (c *LRUCache[K, T]) Stats() (hits, misses uint64)

type LimitedByteReader

type LimitedByteReader struct {
	R ReaderAndByteReader // underlying reader
	N int64               // max bytes remaining
}

A LimitedByteReader reads from R but limits the amount of data returned to just N bytes. Each call to Read updates N to reflect the new amount remaining. Read returns EOF when N <= 0 or when the underlying R returns EOF.

func LimitByteReader

func LimitByteReader(r ReaderAndByteReader, n int64) *LimitedByteReader

LimitByteReader returns a Reader that reads from r but stops with EOF after n bytes. The underlying implementation is a *LimitedReader.

func (*LimitedByteReader) Left

func (l *LimitedByteReader) Left() int64

func (*LimitedByteReader) Read

func (l *LimitedByteReader) Read(p []byte) (n int, err error)

func (*LimitedByteReader) ReadByte

func (l *LimitedByteReader) ReadByte() (v byte, err error)

type LogLevel

type LogLevel int

type MapCache

type MapCache[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewMapCache

func NewMapCache[K comparable, T any](preAllocateSize int) *MapCache[K, T]

func (*MapCache[K, T]) Clear

func (m *MapCache[K, T]) Clear()

func (*MapCache[K, T]) Delete

func (m *MapCache[K, T]) Delete(key K)

func (*MapCache[K, T]) Get

func (m *MapCache[K, T]) Get(key K) (value T, ok bool)

func (*MapCache[K, T]) Set

func (m *MapCache[K, T]) Set(key K, value T)

func (*MapCache[K, T]) Stats

func (m *MapCache[K, T]) Stats() (hits, misses uint64)

type NilCache added in v4.1.0

type NilCache[K comparable, T any] struct {
}

func NewNilCache added in v4.1.0

func NewNilCache[K comparable, T any]() NilCache[K, T]

func (NilCache[K, T]) Clear added in v4.1.0

func (m NilCache[K, T]) Clear()

func (NilCache[K, T]) Delete added in v4.1.0

func (m NilCache[K, T]) Delete(key K)

func (NilCache[K, T]) Get added in v4.1.0

func (m NilCache[K, T]) Get(key K) (value T, ok bool)

func (NilCache[K, T]) Set added in v4.1.0

func (m NilCache[K, T]) Set(key K, value T)

func (NilCache[K, T]) Stats added in v4.1.0

func (m NilCache[K, T]) Stats() (hits, misses uint64)

type ReaderAndByteReader

type ReaderAndByteReader interface {
	io.Reader
	io.ByteReader
}

Jump to

Keyboard shortcuts

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