backpressure

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package backpressure 提供通用并发背压机制。

本包用 semaphore 限制同一资源的最大 in-flight 操作数,并可配置等待超时。 Acquire 返回原始业务 context 和 release 函数,等待超时只影响排队过程,不会给 下游业务操作额外加 deadline。

包内事件和快照是机制级模型,业务项目可以在外层 adapter 中把它们映射为自己的 metrics、resilience 事件或治理响应。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Acquirer

type Acquirer interface {
	Acquire(context.Context) (context.Context, func(), error)
}

Acquirer 等待一个执行槽位,并返回 release 函数。

type Event

type Event struct {
	Component   string
	Dependency  string
	Resource    string
	Strategy    string
	Outcome     Outcome
	Wait        time.Duration
	InFlight    int
	MaxInflight int
	Err         error
}

Event 描述一次限流器状态变化。

type Limiter

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

Limiter 提供一个可选等待超时的 in-flight 限流器。

func NewLimiter

func NewLimiter(maxInflight int, timeout time.Duration) *Limiter

NewLimiter 根据最大 in-flight 数和可选等待超时创建限流器。

func NewLimiterWithOptions

func NewLimiterWithOptions(maxInflight int, timeout time.Duration, opts Options) *Limiter

func (*Limiter) Acquire

func (l *Limiter) Acquire(ctx context.Context) (context.Context, func(), error)

Acquire 等待一个槽位,直到成功、context 取消或等待超时。 返回的 context 保持为原始请求 context,release 用于释放已获取的槽位。

func (*Limiter) Stats

func (l *Limiter) Stats(name string) Stats

Stats 返回机制级限流器快照。

type NopObserver

type NopObserver struct{}

NopObserver 忽略所有限流事件。

func (NopObserver) OnBackpressure

func (NopObserver) OnBackpressure(context.Context, Event)

type Observer

type Observer interface {
	OnBackpressure(ctx context.Context, event Event)
}

Observer 接收背压限流事件。

type Options

type Options struct {
	Component  string
	Dependency string
	Observer   Observer
}

Options 配置一个背压限流器。

type Outcome

type Outcome string

Outcome 描述一次背压限流事件的结果。

const (
	OutcomeAcquired Outcome = "backpressure_acquired"
	OutcomeTimeout  Outcome = "backpressure_timeout"
	OutcomeReleased Outcome = "backpressure_released"
)

type Stats

type Stats struct {
	Component     string
	Name          string
	Dependency    string
	Strategy      string
	Enabled       bool
	MaxInflight   int
	InFlight      int
	TimeoutMillis int64
	Degraded      bool
	Reason        string
}

Stats 是一个限流器的机制级快照。

Jump to

Keyboard shortcuts

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