core

package
v0.0.0-...-99b537f Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: MPL-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Copyright (c) 2024 RethinkDNS and its authors.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

Index

Constants

View Source
const (
	// Anew is the value returned by Barrier.Do when the function was
	// executed and its results are stored in the Barrier.
	Anew = iota
	// Shared is the value returned by Barrier.Do when the function's
	// results are already stored in the Barrier.
	Shared
)
View Source
const (
	// B524288 is slab of size 512k
	B524288 = 512 * 1024
	// B65536 is slab of size 64k
	B65536 = 64 * 1024
	// B32768 is slab of size 32k
	B32768 = 32 * 1024
	// B16384 is slab of size 16k
	B16384 = 16 * 1024
	// B8192 is slab of size 8k
	B8192 = 8 * 1024
	// B4096 is slab of size 4k
	B4096 = 4 * 1024
	// B2048 is slab of size 2k; also the min
	B2048 = 2 * 1024
	// BMAX is the largest pooled slab size
	BMAX = B524288
)
View Source
const (
	UNKNOWN_UID         = -1
	UNKNOWN_UID_STR     = "-1"
	UNSUPPORTED_NETWORK = -1
)
View Source
const Nobody = uintptr(0) // nobody

Variables

View Source
var (
	// Commit set at link time by git rev-parse --short HEAD
	Commit string
	// Date set at link time by date -u +'%Y%m%d%H%M%S'
	Date string
)

Functions

func Alloc

func Alloc() *[]byte

Alloc returns a truncated byte slice of size 2048

func AllocRegion

func AllocRegion(size int) *[]byte

AllocRegion returns a truncated byte slice at least size big

func Close

func Close(cs ...io.Closer)

Close closes cs.

func CloseConn

func CloseConn(cs ...MinConn)

CloseConn closes cs.

func CloseFD

func CloseFD(fd int)

func CloseFile

func CloseFile(f *os.File)

func CloseOp

func CloseOp(c io.Closer, op CloserOp)

CloseOp closes op on c.

func CloseTCP

func CloseTCP(c *net.TCPConn)

CloseTCP closes c.

func CloseTCPRead

func CloseTCPRead(r TCPConn)

CloseTCPRead closes the read end of r.

func CloseTCPWrite

func CloseTCPWrite(w TCPConn)

CloseTCPWrite closes the write end of w.

func CloseUDP

func CloseUDP(c *net.UDPConn)

CloseUDP closes c.

func Echo

func Echo(pc net.PacketConn, pkt []byte, dst net.Addr, v4 bool) (reply []byte, from net.Addr)

func Every

func Every(id string, pctx context.Context, d time.Duration, f func()) context.Context

func FmtBytes

func FmtBytes(val uint64) string

from: github.com/google/gops/blob/35c854fb84/agent/agent.go

func FmtTimeNs

func FmtTimeNs(ns uint64) string

func FmtTimeSecs

func FmtTimeSecs(ns uint64) int64

func Gg

func Gg(who string, f func(), cb func())

Gg runs f in a goroutine, recovers from any panics if any; then calls cb in a separate goroutine, and recovers from any panics.

func Gif

func Gif(cond bool, who string, f func())

func Go

func Go(who string, f func())

Go runs f in a goroutine and recovers from any panics.

func Go1

func Go1[T any](who string, f func(T), arg T)

Go1 runs f(arg) in a goroutine and recovers from any panics.

func Go2

func Go2[T0 any, T1 any](who string, f func(T0, T1), a0 T0, a1 T1)

Go2 runs f(arg0,arg1) in a goroutine and recovers from any panics.

func Grx

func Grx[T any](who string, f WorkCtx[T], d time.Duration) (zz T, completed bool)

func Gx

func Gx(who string, f func())

Gx runs f in a goroutine and exits the process if f panics.

func IsNil

func IsNil(x any) bool

IsNil reports whether x is nil if its Chan, Func, Map, Pointer, UnsafePointer, Interface, and Slice; may panic or return false if x is not addressable

func IsNotNil

func IsNotNil(x any) bool

may panic or return false if x is not addressable

func IsZero

func IsZero(x any) bool

func LOB

func LOB() *[]byte

LOB returns a truncated byte slice of size 524288

func Loc

func Loc(x any) uintptr

func NewConnMap

func NewConnMap() *cm

func NewP2QuantileEstimator

func NewP2QuantileEstimator(ctx context.Context, samples int64, probability float64) *p2

NewP90Estimator returns a new estimator with percentile p.

func NewP50Estimator

func NewP50Estimator(ctx context.Context) *p2

NewP50Estimator returns a new P50 (median) estimator.

func OneErr

func OneErr(errs ...error) error

func Ping

func Ping(pc net.PacketConn, ipp netip.AddrPort) (ok bool, rtt time.Duration, err error)

from: github.com/go-ping/ping/blob/caaf2b72ea5/ping.go

func Pipe

func Pipe(dst io.Writer, src io.Reader) (int64, error)

Pipe copies data from src to dst, and returns the number of bytes copied. Prefers src.WriteTo(dst) and dst.ReadFrom(src) if available. Otherwise, uses io.CopyBuffer, recycling buffers from global pool.

func Race

func Race[T any](who string, timeout time.Duration, fs ...WorkCtx[T]) (zz T, fidx int, errs error)

Race runs all the functions in fs concurrently and returns the first non-error result. Returned values are the result, the index of the function that returned the result, and any errors. If all functions return an error, the accumulation of it is returned. Panicking functions are considered as returning an error. If the timeout is reached, errTimeout is returned. Note that, zero value result could be returned if at least one function returns that without any error.

func RandomIPFromPrefix

func RandomIPFromPrefix(cidr netip.Prefix) (netip.Addr, error)

RandomIPFromPrefix returns a random IP from the provided CIDR prefix. Supports IPv4 and IPv6. Does not support mapped inputs.

func Recover

func Recover(code ExitCode, aux any) (didpanic bool)

Recover must be called as a defered function, and must be the first defer called at the start of a new goroutine.

func RecoverFn

func RecoverFn(aux string, fn Finally) (didpanic bool)

fn is called in a separate goroutine, if a panic is recovered. RecoverFn must be called as a defered function, and must be the first defer called at the start of a new goroutine.

func Recycle

func Recycle(b *[]byte) bool

Recycle returns the byte slices to the pool

func SetKeepAliveConfig

func SetKeepAliveConfig(c MinConn) bool

func SetKeepAliveConfigSockOpt

func SetKeepAliveConfigSockOpt(c MinConn, args ...int) (ok bool)

SetKeepAliveConfigSockOpt sets for a TCP connection, SO_KEEPALIVE, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT, TCP_USER_TIMEOUT. args is optional, and should be in the order of idle, interval, count.

func TlsSessionCache

func TlsSessionCache() tls.ClientSessionCache

func TrimmedClone

func TrimmedClone(pkt *stack.PacketBuffer) *stack.PacketBuffer

trimmedClone clones the packet buffer to not modify the original. It trims anything before the network header.

func TypeEq

func TypeEq(a, b any) bool

func Version

func Version() string

Types

type Barrier

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

Barrier represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.

func NewBarrier

func NewBarrier[T any](ttl time.Duration) *Barrier[T, string]

NewBarrier returns a new Barrier with the given time-to-live for completed Vs.

func NewBarrier2

func NewBarrier2[T any, K comparable](ttl, neg time.Duration) *Barrier[T, K]

NewBarrier2 returns a new Barrier with the time-to-lives for completed Vs (ttl) and errored Vs (neg).

func NewKeyedBarrier

func NewKeyedBarrier[T any, K comparable](ttl time.Duration) *Barrier[T, K]

func (*Barrier[T, K]) Do

func (ba *Barrier[T, K]) Do(k K, once Work[T]) (*V[T, K], int)

Do executes and returns the results of the given function, making sure that only one execution is in-flight for a given key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

func (*Barrier[T, K]) Do1

func (ba *Barrier[T, K]) Do1(k K, once Work1[T], arg T) (*V[T, K], int)

Do1 is like Do but for Work1 with one arg.

func (*Barrier[T, K]) DoIt

func (ba *Barrier[T, K]) DoIt(k K, once Work[T]) (zz T, err error)

DoIt is like Do but returns from once as-is.

func (*Barrier[T, K]) Go

func (ba *Barrier[T, K]) Go(k K, once Work[T]) <-chan *V[T, K]

untested

type CloserOp

type CloserOp int
const (
	CopR   CloserOp = iota // close read
	CopW                   // close write
	CopRW                  // close read write
	CopAny                 // close
)

type ConnMapper

type ConnMapper interface {
	// Clear untracks all conns.
	Clear() []string
	// Track maps x[] to cid.
	Track(cid string, x ...MinConn) int
	// Get returns a conn mapped to connection id, cid.
	Get(cid string) []MinConn
	// Untrack closes all conns with connection id, cid.
	Untrack(cid string) int
	// UntrackBatch untracks one cid at a time.
	UntrackBatch(cids []string) []string
	// Len returns the number of tracked conns.
	Len() int
	// String returns a string repr of all tracked conns.
	String() string
}

type ConnPool

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

github.com/redis/go-redis/blob/d9eeed13/internal/pool/pool.go

func NewConnPool

func NewConnPool[T comparable](ctx context.Context, id T) *ConnPool[T]

NewConnPool creates a new conn pool with preset capacity and ttl.

func (*ConnPool[T]) Get

func (c *ConnPool[T]) Get() (zz net.Conn)

Get returns a conn from the pool, if available, within 3 seconds.

func (*ConnPool[T]) Put

func (c *ConnPool[T]) Put(conn net.Conn) (ok bool)

Put puts conn back in the pool. Put takes ownership of the conn regardless of the return value.

type DuplexConn

type DuplexConn interface {
	TCPConn
	PoolableConn
	io.ReaderFrom
}

DuplexConn represents a bidirectional stream socket.

type ExitCode

type ExitCode int
const DontExit ExitCode = 0

DontExit is a special code that can be passed to Recover to indicate that the process should not exit after recovering from a panic.

const Exit11 ExitCode = 11

An exit code of 11 keeps us out of the way of the detailed exitcodes from plan, and also happens to be the same code as SIGSEGV which is roughly the same type of condition that causes most panics.

type ExpMap

type ExpMap[P comparable, Q any] struct {
	sync.Mutex // guards ExpMap.
	// contains filtered or unexported fields
}

ExpMap holds expiring keys and read hits.

func NewExpiringMap

func NewExpiringMap[P comparable, Q any](ctx context.Context) *ExpMap[P, Q]

NewExpiringMap returns a new ExpMap with min lifetime of 0.

func NewExpiringMapLifetime

func NewExpiringMapLifetime[P comparable, Q any](ctx context.Context, min time.Duration) *ExpMap[P, Q]

func (*ExpMap[P, Q]) Alive

func (m *ExpMap[P, Q]) Alive(key P) bool

func (*ExpMap[P, Q]) Clear

func (m *ExpMap[P, Q]) Clear() int

Clear deletes all keys and returns the number of keys deleted.

func (*ExpMap[P, Q]) Delete

func (m *ExpMap[P, Q]) Delete(key P)

Delete deletes the given key.

func (*ExpMap[P, Q]) Get

func (m *ExpMap[P, Q]) Get(key P) uint32

Get returns the number of hits for the given key.

func (*ExpMap[P, Q]) K

func (m *ExpMap[P, Q]) K(key P, value Q, expiry time.Duration) uint32

Set sets the (value, expiry) for the given key and returns the number of hits. expiry must be greater than the minimum lifetime.

func (*ExpMap[P, Q]) Len

func (m *ExpMap[P, Q]) Len() int

Len returns the number of keys, which may or may not have expired.

func (*ExpMap[P, Q]) Set

func (m *ExpMap[P, Q]) Set(key P, expiry time.Duration) uint32

Set sets the expiry for the given key and returns the number of hits. expiry must be greater than the minimum lifetime.

func (*ExpMap[P, Q]) V

func (m *ExpMap[P, Q]) V(key P) (zz Q, fresh bool)

type Finally

type Finally func()

type Hangover

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

func NewHangover

func NewHangover() *Hangover

func (*Hangover) Break

func (h *Hangover) Break()

func (*Hangover) Exceeds

func (h *Hangover) Exceeds(d time.Duration) bool

func (*Hangover) Note

func (h *Hangover) Note()

func (*Hangover) Within

func (h *Hangover) Within(d time.Duration) bool

type ICMPConn

type ICMPConn interface {
	net.PacketConn
}

type MinConn

type MinConn interface {
	LocalAddr() net.Addr
	SetDeadline(time.Time) error
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
	Close() error
}

MinConn is a minimal connection interface that is a subset of both net.Conn and net.PacketConn.

type MultConnPool

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

func NewMultConnPool

func NewMultConnPool[T comparable](ctx context.Context) *MultConnPool[T]

NewMultConnPool creates a new multi connection-pool.

func (*MultConnPool[T]) Get

func (m *MultConnPool[T]) Get(id T) net.Conn

Get returns a conn from the pool[id], if available.

func (*MultConnPool[T]) Put

func (m *MultConnPool[T]) Put(id T, conn net.Conn) (ok bool)

Put puts conn back in the pool[id].

type P2QuantileEstimator

type P2QuantileEstimator interface {
	// Add a sample to the estimator.
	Add(float64)
	// Get the estimation for p.
	Get() int64
	// Get the percentile, p.
	P() float64
}

P2QuantileEstimator is an interface for the P2 quantile estimator.

type PcapHeader

type PcapHeader struct {
	MagicNumber  uint32
	VersionMajor uint16
	VersionMinor uint16
	Thiszone     int32
	Sigfigs      uint32
	Snaplen      uint32
	Network      uint32
}

type PcapPacket

type PcapPacket struct {
	Timestamp     time.Time
	Packet        *stack.PacketBuffer
	MaxCaptureLen int
}

func (*PcapPacket) MarshalBinary

func (p *PcapPacket) MarshalBinary() ([]byte, error)

type PoolableConn

type PoolableConn syscall.Conn

so it can be pooled by ConnPool.

type Sieve

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

Sieve is a thread-safe map with expiring keys.

func NewSieve

func NewSieve[K comparable, V any](ctx context.Context, dur time.Duration) *Sieve[K, V]

NewSieve returns a new Sieve with keys expiring after lifetime.

func (*Sieve[K, V]) Clear

func (s *Sieve[K, V]) Clear() int

Clear removes all elements from the sieve.

func (*Sieve[K, V]) Del

func (s *Sieve[K, V]) Del(k K)

Del removes the element with the given key from the sieve.

func (*Sieve[K, V]) Get

func (s *Sieve[K, V]) Get(k K) (V, bool)

Get returns the value associated with the given key, and a boolean indicating whether the key was found.

func (*Sieve[K, V]) Len

func (s *Sieve[K, V]) Len() int

Len returns the number of elements in the sieve.

func (*Sieve[K, V]) Put

func (s *Sieve[K, V]) Put(k K, v V) (replaced bool)

Put adds an element to the sieve with the given key and value.

type Sieve2K

type Sieve2K[K1, K2 comparable, V any] struct {
	// contains filtered or unexported fields
}

Sieve2K is a map of expiring maps. The outer map is keyed to K1, while the inner expiring maps are keyed to K2.

func NewSieve2K

func NewSieve2K[K1, K2 comparable, V any](ctx context.Context, dur time.Duration) *Sieve2K[K1, K2, V]

NewSieve2K returns a new Sieve2K with keys expiring after lifetime.

func (*Sieve2K[K1, K2, V]) Clear

func (s *Sieve2K[K1, K2, V]) Clear() (n int)

Clear removes all elements from the sieve.

func (*Sieve2K[K1, K2, V]) Del

func (s *Sieve2K[K1, K2, V]) Del(k1 K1, k2 K2)

Del removes the element with the given key from the sieve.

func (*Sieve2K[K1, K2, V]) Get

func (s *Sieve2K[K1, K2, V]) Get(k1 K1, k2 K2) (zz V, ok bool)

Get returns the value associated with the given key, and a boolean indicating whether the key was found.

func (*Sieve2K[K1, K2, V]) Len

func (s *Sieve2K[K1, K2, V]) Len() (n int)

Len returns the number of elements in the sieve.

func (*Sieve2K[K1, K2, V]) Put

func (s *Sieve2K[K1, K2, V]) Put(k1 K1, k2 K2, v V) (replaced bool)

Put adds an element to the sieve with the given key and value.

type TCPConn

type TCPConn interface {
	// RemoteAddr returns the destination network address.
	RemoteAddr() net.Addr
	// LocalAddr returns the local client network address.
	LocalAddr() net.Addr

	// confirms to protect.Conn
	Write([]byte) (int, error)
	Read([]byte) (int, error)

	Close() error
	// CloseWrite closes the writing side by sending a FIN
	// segment to local peer. That means we can write no further
	// data to TUN.
	CloseWrite() error
	// CloseRead closes the reading side. That means we can no longer
	// read more from TUN.
	CloseRead() error

	SetDeadline(time.Time) error
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
}

TCPConn abstracts a TCP connection coming from TUN. This connection should be handled by a registered TCP proxy handler.

type UDPConn

type UDPConn interface {
	// LocalAddr returns the local client network address.
	LocalAddr() net.Addr
	RemoteAddr() net.Addr

	// confirms to protect.Conn
	Write([]byte) (int, error)
	Read([]byte) (int, error)

	// confirms to net.PacketConn
	WriteTo([]byte, net.Addr) (int, error)
	ReadFrom([]byte) (int, net.Addr, error)

	// Close closes the connection.
	Close() error

	// Implements net.Conn and net.PacketConn
	SetDeadline(time.Time) error
	SetReadDeadline(time.Time) error
	SetWriteDeadline(time.Time) error
}

UDPConn abstracts a UDP connection coming from TUN. This connection should be handled by a registered UDP proxy handler.

type V

type V[T any, K comparable] struct {
	Val T
	Err error
	N   atomic.Uint32
	// contains filtered or unexported fields
}

V is an in-flight or completed Barrier.Do V

func (*V[t, k]) E

func (v *V[t, k]) E() string

func (*V[t, k]) String

func (v *V[t, k]) String() string

type Volatile

type Volatile[T any] atomic.Value

Volatile is a non-panicking, non-atomic atomic.Value.

func NewVolatile

func NewVolatile[T any](t T) *Volatile[T]

NewVolatile returns a new Volatile with the value t. Panics if t is nil.

func NewZeroVolatile

func NewZeroVolatile[T any]() *Volatile[T]

NewVolatile returns a new uninitialized Volatile.

func (*Volatile[T]) Cas

func (a *Volatile[T]) Cas(old, new T) (ok bool)

Cas compares and swaps the value of a with new, returns true if the value was swapped. If new is nil, returns true; and sets a to NewZeroVolatile[T] non-atomically. If a is nil or old & new are not of same concrete type, returns false.

func (*Volatile[T]) Load

func (a *Volatile[T]) Load() (t T)

Load returns the value of a. May return zero value. This func is atomic.

func (*Volatile[T]) Store

func (a *Volatile[T]) Store(t T)

Store stores the value t; creates a new Volatile[T] if t is nil. If a is nil, does nothing. This func is not atomic.

func (*Volatile[T]) Swap

func (a *Volatile[T]) Swap(new T) (old T)

Swap assigns new and returns the old value, atomically. If a is nil, it returns zero value. If new is nil, returns old value; and sets a to NewZeroVolatile[T]. If old & new are not of the same concrete type, it panics.

func (*Volatile[T]) Tango

func (a *Volatile[T]) Tango(new T) (old T)

Tango retrieves old value and loads in new non-atomically. If a is nil, returns zero value. If new is nil, returns zero value; and sets a to NewZeroVolatile[T]. old & new need not be the same concrete type. This func is not atomic.

type Work

type Work[T any] func() (T, error)

Work is the type of the function to memoize.

type Work1

type Work1[T any] func(T) (T, error)

type WorkCtx

type WorkCtx[T any] func(context.Context) (T, error)

Directories

Path Synopsis
Package blindrsa implements the RSA Blind Signature Protocol as defined in [RFC9474].
Package blindrsa implements the RSA Blind Signature Protocol as defined in [RFC9474].

Jump to

Keyboard shortcuts

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