myratelimit

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: BSD-3-Clause Imports: 7 Imported by: ? 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSharedKey added in v1.0.3

func BuildSharedKey(parts ...string) string

BuildSharedKey 根据多个业务维度生成共享限速池 key。

parts: 组成共享限速维度的字段,例如入口 IP、用户 ID、用户名等。 空格会被裁剪,最终用 "|" 连接。相同 key 会复用同一个共享限速池。

func WrapWithRateLimit

func WrapWithRateLimit(rw io.ReadWriter, readLimit, writeLimit int64) io.ReadWriter

WrapWithRateLimit 包装 io.ReadWriter,为读写方向添加速率限制。 readLimit 和 writeLimit 单位是字节每秒(Byte/s),若 <= 0 表示该方向不限制。

Types

type Direction added in v1.0.3

type Direction int
const (
	DirectionUpload Direction = iota + 1
	DirectionDownload
)

type RateLimitedConn

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

RateLimitedConn 封装后的限速器,实现 io.ReadWriter 接口

func (RateLimitedConn) Close added in v1.0.5

func (c RateLimitedConn) Close() error

func (RateLimitedConn) CloseRead added in v1.0.5

func (c RateLimitedConn) CloseRead() error

func (RateLimitedConn) CloseWrite added in v1.0.5

func (c RateLimitedConn) CloseWrite() error

func (*RateLimitedConn) Read

func (r *RateLimitedConn) Read(p []byte) (int, error)

Read 调用限速后的 reader

func (*RateLimitedConn) ReadFrom added in v1.0.2

func (c *RateLimitedConn) ReadFrom(r io.Reader) (int64, error)

func (RateLimitedConn) SetDeadline added in v1.0.5

func (c RateLimitedConn) SetDeadline(t time.Time) error

func (RateLimitedConn) SetReadDeadline added in v1.0.5

func (c RateLimitedConn) SetReadDeadline(t time.Time) error

func (RateLimitedConn) SetWriteDeadline added in v1.0.5

func (c RateLimitedConn) SetWriteDeadline(t time.Time) error

func (*RateLimitedConn) Write

func (r *RateLimitedConn) Write(p []byte) (int, error)

Write 调用限速后的 writer

func (*RateLimitedConn) WriteTo added in v1.0.2

func (r *RateLimitedConn) WriteTo(w io.Writer) (int64, error)

type SharedLimiter added in v1.0.3

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

func GetOrUpdateSharedLimiter added in v1.0.3

func GetOrUpdateSharedLimiter(key string, uploadBytesSec, downloadBytesSec int64) *SharedLimiter

GetOrUpdateSharedLimiter 使用默认单例获取或更新共享限速器。

key: 共享限速池的唯一标识,建议由 BuildSharedKey 生成。 uploadBytesSec: 上传方向带宽上限,单位 Byte/s;<= 0 表示上传方向不限速。 downloadBytesSec: 下载方向带宽上限,单位 Byte/s;<= 0 表示下载方向不限速。

同一个 key 的多条连接会拿到同一个 SharedLimiter,从而共享同一个带宽池。 如果后续同一个 key 传入新的上下行限速值,会复用原限速器并更新限速配置。

func (*SharedLimiter) DownloadBytesSec added in v1.0.3

func (l *SharedLimiter) DownloadBytesSec() int64

func (*SharedLimiter) Update added in v1.0.3

func (l *SharedLimiter) Update(uploadBytesSec, downloadBytesSec int64)

Update 更新共享限速器的上下行限速值。

uploadBytesSec 和 downloadBytesSec 单位均为 Byte/s;<= 0 表示对应方向不限速。

func (*SharedLimiter) UploadBytesSec added in v1.0.3

func (l *SharedLimiter) UploadBytesSec() int64

func (*SharedLimiter) Wrap added in v1.0.3

func (l *SharedLimiter) Wrap(rw io.ReadWriter, readDirection, writeDirection Direction) io.ReadWriter

Wrap 将连接或流包装为受共享限速器控制的 io.ReadWriter。

rw: 原始连接或转发流。 readDirection: rw.Read 读取到的数据应该消耗的共享限速方向。 writeDirection: rw.Write 写出的数据应该消耗的共享限速方向。

对客户端连接来说,通常 Read 是上传方向、Write 是下载方向;对远端连接 来说,方向通常相反。调用方应根据实际数据流方向传入对应参数。

Jump to

Keyboard shortcuts

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