protector

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 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 protector provides a set of protectors that stop the query services when the resource usage exceeds the limit.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrQueryTooLarge indicates that a request exceeds the configured hard limit.
	ErrQueryTooLarge = errors.New("query memory request exceeds per-query limit")
	// ErrQueryResourceExhausted indicates exhaustion of a query allowance or the shared pool.
	ErrQueryResourceExhausted = errors.New("query memory pool exhausted")
)

Functions

func SetHostMemoryForTest added in v0.11.2

func SetHostMemoryForTest(total func() uint64) (restore func())

SetHostMemoryForTest replaces the host memory reader used to size the default query pool and returns a function restoring the previous reader.

func WaitWhileHigh added in v0.11.0

func WaitWhileHigh(ctx context.Context, pm Memory, maxWait time.Duration, l *logger.Logger, stage string) error

WaitWhileHigh blocks while memory pressure is high, bounded by maxWait. It only creates a timeout context when it actually has to wait. It returns the timeout/cancel error if ctx or maxWait elapses before recovery.

Types

type Memory

type Memory interface {
	AvailableBytes() int64
	GetLimit() uint64
	AcquireResource(ctx context.Context, size uint64) error
	// ShouldCache returns true if the file size is smaller than the threshold.
	ShouldCache(fileSize int64) bool
	run.PreRunner
	run.Config
	run.Service
	// State returns the current memory pressure state.
	State() State
}

Memory is an interface for monitoring and limiting memory usage to prevent OOM.

func NewMemory

func NewMemory(omr observability.MetricsRegistry) Memory

NewMemory creates a new Memory protector.

type Nop added in v0.9.0

type Nop struct{}

Nop is a no-op implementation of Memory interface for testing.

func (Nop) AcquireResource added in v0.9.0

func (Nop) AcquireResource(_ context.Context, _ uint64) error

AcquireResource always succeeds.

func (Nop) AvailableBytes added in v0.9.0

func (Nop) AvailableBytes() int64

AvailableBytes always returns -1 (unlimited).

func (Nop) FlagSet added in v0.9.0

func (Nop) FlagSet() *run.FlagSet

FlagSet returns an empty flag set.

func (Nop) GetLimit added in v0.9.0

func (Nop) GetLimit() uint64

GetLimit always returns 0 (no limit).

func (Nop) GracefulStop added in v0.9.0

func (Nop) GracefulStop()

GracefulStop does nothing.

func (Nop) Name added in v0.9.0

func (Nop) Name() string

Name returns the protector name.

func (Nop) PreRun added in v0.9.0

func (Nop) PreRun(context.Context) error

PreRun does nothing.

func (Nop) Serve added in v0.9.0

func (Nop) Serve() run.StopNotify

Serve returns a closed channel.

func (Nop) ShouldCache added in v0.9.0

func (Nop) ShouldCache(int64) bool

ShouldCache always returns false.

func (Nop) State added in v0.9.0

func (n Nop) State() State

State implements Memory.

func (Nop) Validate added in v0.9.0

func (Nop) Validate() error

Validate always succeeds.

type QueryBudget added in v0.11.1

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

QueryBudget provides an atomic, finite reservation pool for one service. Reservations are estimates and must remain held until response materialization is complete. It intentionally does not modify Memory.AcquireResource semantics.

func NewQueryBudget added in v0.11.1

func NewQueryBudget(pm Memory) *QueryBudget

NewQueryBudget derives a bounded query pool from the protector. A disabled or unavailable protector uses a finite fallback pool rather than becoming unlimited.

func QueryBudgetFor added in v0.11.1

func QueryBudgetFor(pm Memory) *QueryBudget

QueryBudgetFor returns the process-wide budget associated with a protector. It avoids independent pools for the liaison's public services.

func (*QueryBudget) Admit added in v0.11.1

func (b *QueryBudget) Admit(ctx context.Context, limit, offset, defaultLimit uint32) (func(), error)

Admit reserves the complete dynamic assignment for a query after deriving a result-window ceiling from current sampled memory and outstanding reservations.

func (*QueryBudget) AdmitContext added in v0.11.1

func (b *QueryBudget) AdmitContext(ctx context.Context, limit, offset, defaultLimit uint32) (context.Context, func(), error)

AdmitContext admits a query and attaches its chargeable reservation to ctx.

func (*QueryBudget) AdmitRequestContext added in v0.11.1

func (b *QueryBudget) AdmitRequestContext(ctx context.Context, limit, offset, defaultLimit uint32) (context.Context, func(), error)

AdmitRequestContext admits a query window, treating MaxUint32 with zero offset as an unbounded scan.

func (*QueryBudget) AdmitScanContext added in v0.11.1

func (b *QueryBudget) AdmitScanContext(ctx context.Context) (context.Context, func(), error)

AdmitScanContext admits an incremental scan without reserving from its requested limit. Callers must charge every retained result with query.ChargeResult before allocation.

func (*QueryBudget) Limit added in v0.11.1

func (b *QueryBudget) Limit() uint64

Limit returns the finite pool size available to all admitted queries.

func (*QueryBudget) MaxQuery added in v0.11.1

func (b *QueryBudget) MaxQuery() uint64

MaxQuery returns the hard per-query reservation limit.

func (*QueryBudget) Reserve added in v0.11.1

func (b *QueryBudget) Reserve(ctx context.Context, bytes uint64) (func(), error)

Reserve atomically reserves bytes, returning a release function. It rejects oversized requests and pressure, and honors cancellation before admission.

func (*QueryBudget) Reserved added in v0.11.1

func (b *QueryBudget) Reserved() uint64

Reserved returns the current total of outstanding query reservations.

type QueryReservation added in v0.11.1

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

QueryReservation tracks charged allocations and shared ownership of an admitted query.

func (*QueryReservation) Charge added in v0.11.1

func (r *QueryReservation) Charge(bytes uint64) error

Charge accounts bytes before allocation, rejecting charges beyond the assignment.

func (*QueryReservation) ChargeResult added in v0.11.1

func (r *QueryReservation) ChargeResult(bytes uint64) error

ChargeResult accounts one retained result and its bytes before allocation. Exhausting either allowance rejects the result without changing either counter.

func (*QueryReservation) Limit added in v0.11.1

func (r *QueryReservation) Limit() uint64

Limit returns the reservation's assigned byte budget.

func (*QueryReservation) Owner added in v0.11.1

func (r *QueryReservation) Owner() any

Owner identifies the shared query pool owning this reservation.

func (*QueryReservation) Refund added in v0.11.2

func (r *QueryReservation) Refund(bytes uint64)

Refund returns bytes previously charged to the reservation, never taking its usage below zero. It does not affect the retained-result counter.

func (*QueryReservation) Used added in v0.11.1

func (r *QueryReservation) Used() uint64

Used returns the bytes currently charged to the reservation (charges minus refunds).

type State added in v0.9.0

type State int

State represents memory pressure levels for load shedding.

const (
	// StateLow indicates normal memory usage, accept all requests.
	StateLow State = iota
	// StateHigh indicates high memory pressure, reject new requests.
	StateHigh
)

Jump to

Keyboard shortcuts

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