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
- Variables
- func Alloc() *[]byte
- func AllocRegion(size int) *[]byte
- func Close(cs ...io.Closer)
- func CloseConn(cs ...MinConn)
- func CloseFD(fd int)
- func CloseFile(f *os.File)
- func CloseOp(c io.Closer, op CloserOp)
- func CloseTCP(c *net.TCPConn)
- func CloseTCPRead(r TCPConn)
- func CloseTCPWrite(w TCPConn)
- func CloseUDP(c *net.UDPConn)
- func Echo(pc net.PacketConn, pkt []byte, dst net.Addr, v4 bool) (reply []byte, from net.Addr)
- func Every(id string, pctx context.Context, d time.Duration, f func()) context.Context
- func FmtBytes(val uint64) string
- func FmtTimeNs(ns uint64) string
- func FmtTimeSecs(ns uint64) int64
- func Gg(who string, f func(), cb func())
- func Gif(cond bool, who string, f func())
- func Go(who string, f func())
- func Go1[T any](who string, f func(T), arg T)
- func Go2[T0 any, T1 any](who string, f func(T0, T1), a0 T0, a1 T1)
- func Grx[T any](who string, f WorkCtx[T], d time.Duration) (zz T, completed bool)
- func Gx(who string, f func())
- func IsNil(x any) bool
- func IsNotNil(x any) bool
- func IsZero(x any) bool
- func LOB() *[]byte
- func Loc(x any) uintptr
- func NewConnMap() *cm
- func NewP2QuantileEstimator(ctx context.Context, samples int64, probability float64) *p2
- func NewP50Estimator(ctx context.Context) *p2
- func OneErr(errs ...error) error
- func Ping(pc net.PacketConn, ipp netip.AddrPort) (ok bool, rtt time.Duration, err error)
- func Pipe(dst io.Writer, src io.Reader) (int64, error)
- func Race[T any](who string, timeout time.Duration, fs ...WorkCtx[T]) (zz T, fidx int, errs error)
- func RandomIPFromPrefix(cidr netip.Prefix) (netip.Addr, error)
- func Recover(code ExitCode, aux any) (didpanic bool)
- func RecoverFn(aux string, fn Finally) (didpanic bool)
- func Recycle(b *[]byte) bool
- func SetKeepAliveConfig(c MinConn) bool
- func SetKeepAliveConfigSockOpt(c MinConn, args ...int) (ok bool)
- func TlsSessionCache() tls.ClientSessionCache
- func TrimmedClone(pkt *stack.PacketBuffer) *stack.PacketBuffer
- func TypeEq(a, b any) bool
- func Version() string
- type Barrier
- type CloserOp
- type ConnMapper
- type ConnPool
- type DuplexConn
- type ExitCode
- type ExpMap
- func (m *ExpMap[P, Q]) Alive(key P) bool
- func (m *ExpMap[P, Q]) Clear() int
- func (m *ExpMap[P, Q]) Delete(key P)
- func (m *ExpMap[P, Q]) Get(key P) uint32
- func (m *ExpMap[P, Q]) K(key P, value Q, expiry time.Duration) uint32
- func (m *ExpMap[P, Q]) Len() int
- func (m *ExpMap[P, Q]) Set(key P, expiry time.Duration) uint32
- func (m *ExpMap[P, Q]) V(key P) (zz Q, fresh bool)
- type Finally
- type Hangover
- type ICMPConn
- type MinConn
- type MultConnPool
- type P2QuantileEstimator
- type PcapHeader
- type PcapPacket
- type PoolableConn
- type Sieve
- type Sieve2K
- type TCPConn
- type UDPConn
- type V
- type Volatile
- type Work
- type Work1
- type WorkCtx
Constants ¶
const ( // Anew is the value returned by Barrier.Do when the function was // executed and its results are stored in the Barrier. Anew = iota // results are already stored in the Barrier. Shared )
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 )
const ( UNKNOWN_UID = -1 UNKNOWN_UID_STR = "-1" UNSUPPORTED_NETWORK = -1 )
const Nobody = uintptr(0) // nobody
Variables ¶
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 AllocRegion ¶
AllocRegion returns a truncated byte slice at least size big
func FmtTimeSecs ¶
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 IsNil ¶
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 NewConnMap ¶
func NewConnMap() *cm
func NewP2QuantileEstimator ¶
NewP90Estimator returns a new estimator with percentile p.
func NewP50Estimator ¶
NewP50Estimator returns a new P50 (median) estimator.
func Pipe ¶
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 ¶
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 ¶
RandomIPFromPrefix returns a random IP from the provided CIDR prefix. Supports IPv4 and IPv6. Does not support mapped inputs.
func Recover ¶
Recover must be called as a defered function, and must be the first defer called at the start of a new goroutine.
func RecoverFn ¶
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 SetKeepAliveConfig ¶
func SetKeepAliveConfigSockOpt ¶
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.
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 ¶
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 ¶
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.
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.
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 (*ExpMap[P, Q]) K ¶
Set sets the (value, expiry) for the given key and returns the number of hits. expiry must be greater than the minimum lifetime.
type Hangover ¶
type Hangover struct {
// contains filtered or unexported fields
}
func NewHangover ¶
func NewHangover() *Hangover
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.
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 PcapPacket ¶
type PcapPacket struct { Timestamp time.Time Packet *stack.PacketBuffer MaxCaptureLen int }
func (*PcapPacket) MarshalBinary ¶
func (p *PcapPacket) MarshalBinary() ([]byte, error)
type Sieve ¶
type Sieve[K comparable, V any] struct { // contains filtered or unexported fields }
Sieve is a thread-safe map with expiring keys.
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 ¶
Get returns the value associated with the given key, and a boolean indicating whether the key was found.
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]) 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 ¶
Get returns the value associated with the given key, and a boolean indicating whether the key was found.
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
type Volatile ¶
Volatile is a non-panicking, non-atomic atomic.Value.
func NewVolatile ¶
NewVolatile returns a new Volatile with the value t. Panics if t is nil.
func NewZeroVolatile ¶
NewVolatile returns a new uninitialized Volatile.
func (*Volatile[T]) Cas ¶
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.