utils

package
v3.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 23 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 (
	VarIntLen1 uint64 = 1 << ((iota + 1) * 7)
	VarIntLen2
	VarIntLen3
	VarIntLen4
	VarIntLen5
	VarIntLen6
	VarIntLen7
	VarIntLen8
	VarIntLen9
)
View Source
const IFA_FLAGS = 0x8

Variables

View Source
var GlobalLogLevel = LogLevelError | LogLevelInfo
View Source
var LogFile bool
View Source
var LogFunc bool

Functions

func ContextTick

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

func Debugf

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

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 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 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](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 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