throttle

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package throttle implements token-bucket bandwidth limiting for proxy connections.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MbpsToBytes

func MbpsToBytes(mbps float64) int64

MbpsToBytes converts megabits per second to bytes per second.

func NewContext

func NewContext(ctx context.Context, l *Limits) context.Context

NewContext returns a child context carrying the given Limits.

Types

type Bucket

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

Bucket is a goroutine-safe token-bucket rate limiter.

func NewBucket

func NewBucket(bytesPerSec int64) *Bucket

NewBucket creates a rate limiter allowing bytesPerSec throughput. Returns nil if bytesPerSec <= 0.

func (*Bucket) SetRate

func (b *Bucket) SetRate(bytesPerSec int64)

SetRate updates the bucket's throughput rate. Safe to call concurrently with Wait.

func (*Bucket) Wait

func (b *Bucket) Wait(n int)

Wait consumes n tokens, sleeping if the bucket is empty.

type GroupRegistry

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

GroupRegistry manages shared bandwidth buckets keyed by a group identifier. All connections with the same group key share a single download/upload budget.

func NewGroupRegistry

func NewGroupRegistry() *GroupRegistry

NewGroupRegistry creates an empty group registry.

func (*GroupRegistry) Acquire

func (g *GroupRegistry) Acquire(key string, dlRate, ulRate int64) (dl, ul *Bucket)

Acquire returns shared download/upload buckets for the given key, creating them on first call. Each Acquire must be paired with Release. If rates differ from the existing entry, the bucket rates are updated.

func (*GroupRegistry) Release

func (g *GroupRegistry) Release(key string)

Release decrements the reference count for the given key. When the count reaches zero, the entry is removed.

func (*GroupRegistry) UpdateRate

func (g *GroupRegistry) UpdateRate(key string, dlRate, ulRate int64)

UpdateRate changes the download/upload rates for an existing group. Non-positive values are ignored. No-op if the group does not exist.

type Limits

type Limits struct {
	Download []*Bucket
	Upload   []*Bucket
}

Limits holds per-direction bandwidth buckets for a connection.

func FromContext

func FromContext(ctx context.Context) *Limits

FromContext extracts Limits from the context, or nil if not set.

type Reader

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

Reader throttles reads through one or more Buckets.

func NewReader

func NewReader(r io.Reader, buckets ...*Bucket) *Reader

NewReader wraps r with bandwidth limiting. Nil buckets are filtered out. Returns nil if no valid buckets remain (caller should use the original reader).

func (*Reader) Read

func (tr *Reader) Read(p []byte) (int, error)

Read reads from the underlying reader, then throttles based on bytes read.

type Writer

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

Writer throttles writes through one or more Buckets.

func NewWriter

func NewWriter(w io.Writer, buckets ...*Bucket) *Writer

NewWriter wraps w with bandwidth limiting. Nil buckets are filtered out. Returns nil if no valid buckets remain (caller should use the original writer).

func (*Writer) Write

func (tw *Writer) Write(p []byte) (int, error)

Write splits p into chunks, throttling each through all buckets.

Jump to

Keyboard shortcuts

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