proxybudget

package
v0.134.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package proxybudget meters proxy socket bytes against a durable, bounded account. It is independent of provider selection, authentication, destination allowlists and the fleet token ledger; callers must enforce those separately. The accounting and reservations are extracted from go-search-duck-go's lane.

Adapted from go-search-duck-go/internal/lane/ledger.go; existing lane state remains owned by its service.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfig      = errors.New("proxybudget: invalid bounded configuration")
	ErrUnavailable = errors.New("proxybudget: ledger unavailable")
	ErrClosed      = errors.New("proxybudget: closed")
	ErrDisabled    = errors.New("proxybudget: disabled")
	ErrBusy        = errors.New("proxybudget: in-flight limit reached")
	ErrBudget      = errors.New("proxybudget: total budget exhausted")
	ErrDaily       = errors.New("proxybudget: daily budget exhausted")
	ErrRequest     = errors.New("proxybudget: request byte cap reached")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	StateDir         string        `json:"state_dir"`
	TotalBudgetBytes int64         `json:"total_budget_bytes"`
	DailyCapBytes    int64         `json:"daily_cap_bytes"`
	MaxRequestBytes  int64         `json:"max_request_bytes"`
	MaxInflight      int           `json:"max_inflight"`
	InitialUsedBytes int64         `json:"initial_used_bytes,omitempty"`
	FlushInterval    time.Duration `json:"flush_interval,omitempty"`
}

Config requires positive total, daily, per-request and concurrency limits. StateDir must be a private, persistent absolute path unique to this account allocation. Never open another process's existing lane ledger here.

type Manager

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

Manager owns a locked durable byte ledger and the live request tickets.

func Open

func Open(cfg Config) (*Manager, error)

Open validates limits and fails closed on a missing permission, corrupt ledger or an already-owned state directory. Existing usage is never reset.

func (*Manager) Admit

func (m *Manager) Admit(ctx context.Context) (*Ticket, error)

Admit reserves the full per-request cap before any upstream socket exists. Cancellation closes the ticket's sockets and releases unused reservations.

func (*Manager) Close

func (m *Manager) Close() error

Close rejects admissions, drains every owned socket, then flushes and unlocks the ledger. This ordering prevents post-flush TLS bytes being lost on shutdown.

func (*Manager) Status

func (m *Manager) Status() Status

type PoolConfig

type PoolConfig struct {
	ProviderID   string   `json:"provider_id"`
	PoolID       string   `json:"pool_id"`
	AllowedHosts []string `json:"allowed_hosts"`
	Config
}

PoolConfig binds one provider/pool allocation to its byte budget and allowed destination hosts. It contains no proxy URL or credentials. Callers enforce AllowsHost on every target, including redirects and browser subresources, in addition to their normal SSRF checks.

func LoadPoolConfigs

func LoadPoolConfigs(path string) ([]PoolConfig, error)

LoadPoolConfigs reads the strict {"pools":[...]} config without opening any ledgers. Duplicate pool identities or state directories, unknown fields, missing/invalid limits, empty host policies and oversized files fail closed.

func (PoolConfig) AllowsHost

func (c PoolConfig) AllowsHost(host string) bool

AllowsHost performs an exact normalized host match, never wildcard expansion.

type Status

type Status struct {
	TotalUsedBytes   int64 `json:"total_used_bytes"`
	DailyUsedBytes   int64 `json:"daily_used_bytes"`
	ReservedBytes    int64 `json:"reserved_bytes"`
	TotalBudgetBytes int64 `json:"total_budget_bytes"`
	DailyCapBytes    int64 `json:"daily_cap_bytes"`
	MaxRequestBytes  int64 `json:"max_request_bytes"`
	Inflight         int   `json:"inflight"`
	Disabled         bool  `json:"disabled"`
	Closed           bool  `json:"closed"`
}

Status contains only byte counts and limits, never upstream credentials.

type Ticket

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

Ticket is a single admitted request across all its proxy connections. It is safe to wrap multiple browser subresource sockets in the same ticket.

func (*Ticket) Close

func (t *Ticket) Close() error

Close is idempotent. No new I/O can start after the close barrier; outstanding reads/writes are unblocked by closing their sockets, then fully accounted.

func (*Ticket) Usage

func (t *Ticket) Usage() Usage

func (*Ticket) WrapConn

func (t *Ticket) WrapConn(conn net.Conn) (net.Conn, error)

WrapConn takes ownership of a raw upstream TCP socket before proxy TLS or CONNECT. It never dials or chooses an address, so the caller's SSRF guards remain in force. A refused wrapper closes the supplied connection.

type Usage

type Usage struct {
	InBytes    int64 `json:"in_bytes"`
	OutBytes   int64 `json:"out_bytes"`
	TotalBytes int64 `json:"total_bytes"`
}

Usage is the actual socket usage recorded so far, in both directions.

Jump to

Keyboard shortcuts

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