ratelimit

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ratelimit provides a net.Conn wrapper that throttles read and write throughput using a token bucket. It is meant to be applied at the dial level so the underlying protocol library (go-imap) is unaware of throttling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLimiter

func NewLimiter(bps int) *rate.Limiter

NewLimiter constructs a *rate.Limiter for the given bytes-per-second budget. Returns nil when bps <= 0, signaling "unlimited" to callers.

Types

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

Conn wraps a net.Conn and applies token-bucket rate limiting to Read and Write. Either limiter may be nil — that direction is then unlimited.

Conn is safe for concurrent use by multiple goroutines because *rate.Limiter is itself concurrency-safe and net.Conn implementations are required to be safe for concurrent reads and writes.

func New

func New(c net.Conn, read, write *rate.Limiter) *Conn

New wraps c with the provided limiters. Either limiter may be nil.

func (*Conn) Close

func (c *Conn) Close() error

Close cancels any in-flight WaitN call and closes the underlying connection.

func (*Conn) Read

func (c *Conn) Read(p []byte) (int, error)

Read reads from the underlying connection, then waits for tokens proportional to the bytes returned. Throttling after the read is correct because the bytes are already in flight: we just delay the next read.

func (*Conn) Write

func (c *Conn) Write(p []byte) (int, error)

Write throttles before sending so a large write does not produce a network burst that could trigger server-side rate limits.

Jump to

Keyboard shortcuts

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