Documentation
¶
Index ¶
- func AsTCPConn(c net.Conn) *net.TCPConn
- func Chain(l net.Listener, mws ...Middleware) net.Listener
- func ChainUDP(c net.PacketConn, mws ...UDPMiddleware) net.PacketConn
- func GetContext(c any) context.Context
- func ListenTCP(network, address string, cfg ListenConfig) (net.Listener, error)
- func ListenUDP(network, address string, cfg ListenConfig) (net.PacketConn, error)
- type Bucket
- type ConnState
- type ContextFactory
- type ContextGetter
- type LimitConfig
- type ListenConfig
- type Middleware
- func WithContext(factory ContextFactory) Middleware
- func WithIOTimeout(read, write time.Duration) Middleware
- func WithKeepAlive(period time.Duration) Middleware
- func WithLimit(n int) Middleware
- func WithObserve(observer Observer) Middleware
- func WithProxyProtocol(trustedCIDRs []string) Middleware
- func WithShaper(factory ShaperFactory) Middleware
- type Observer
- type ShaperFactory
- type UDPMiddleware
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain(l net.Listener, mws ...Middleware) net.Listener
Chain 是一个辅助函数,用于将多个中间件应用到一个 Listener 上。 顺序:Chain(l, A, B, C) -> C(B(A(l))) 越靠后的中间件越外层(越先处理 Accept)。
func ChainUDP ¶ added in v1.4.0
func ChainUDP(c net.PacketConn, mws ...UDPMiddleware) net.PacketConn
ChainUDP 串联 UDP 中间件
func GetContext ¶
GetContext 递归地尝试从连接中获取上下文。 如果连接没有绑定 Context,返回 context.Background()。
func ListenTCP ¶ added in v1.4.0
func ListenTCP(network, address string, cfg ListenConfig) (net.Listener, error)
ListenTCP 创建一个 TCP 监听器
func ListenUDP ¶ added in v1.4.0
func ListenUDP(network, address string, cfg ListenConfig) (net.PacketConn, error)
ListenUDP 创建一个 UDP 连接(用于 QUIC/HTTP3)
Types ¶
type Bucket ¶
type Bucket interface {
// Take 阻塞等待,直到获取到 tokens 个令牌。
// ctx 可用于取消等待。
Take(ctx context.Context, tokens int64) error
}
Bucket 是一个抽象的令牌桶接口。 外部可以使用 time/rate 或 juju/ratelimit 实现此接口。
type ContextFactory ¶
ContextFactory 允许用户自定义 Context 的初始化逻辑(例如注入 TraceID, Logger)。
type ContextGetter ¶
ContextGetter 接口用于直接获取连接绑定的上下文。
type LimitConfig ¶ added in v1.4.0
type LimitConfig struct {
Read *rate.Limiter // 入站限流 (nil 表示不限制)
Write *rate.Limiter // 出站限流 (nil 表示不限制)
}
LimitConfig 定义限流配置
type ListenConfig ¶ added in v1.4.0
type ListenConfig struct {
// EnableReusePort 开启 SO_REUSEPORT 特性
// 允许在多核机器上启动多个进程/线程监听同一端口,由内核进行负载均衡
EnableReusePort bool
}
ListenConfig 封装监听配置
type Middleware ¶
Middleware 定义了 Listener 的装饰器函数签名。
func WithContext ¶
func WithContext(factory ContextFactory) Middleware
WithContext 返回一个中间件,用于为每个连接绑定生命周期上下文。 当连接 Close 时,Context 会自动 Cancel。
func WithIOTimeout ¶ added in v1.4.0
func WithIOTimeout(read, write time.Duration) Middleware
WithIOTimeout 设置读写超时。 这是一个非常重要的安全配置,防止客户端建立连接后不发送数据(Slowloris 攻击)。
func WithKeepAlive ¶
func WithKeepAlive(period time.Duration) Middleware
WithKeepAlive 返回一个中间件,自动开启 TCP KeepAlive。 period: 探测间隔。如果为 0,默认使用 3 分钟。
func WithProxyProtocol ¶ added in v1.4.0
func WithProxyProtocol(trustedCIDRs []string) Middleware
WithProxyProtocol 启用 HAProxy PROXY 协议解析。 trustedCIDRs: 允许发送 PROXY 头的来源 IP 网段 (如 "10.0.0.0/8", "127.0.0.1/32")。 如果连接来自非信任 IP,将跳过 PROXY 解析(视为普通 TCP 连接),防止 IP 伪造。
func WithShaper ¶
func WithShaper(factory ShaperFactory) Middleware
WithShaper 返回一个中间件,用于对连接进行流量整形(限速)。
type ShaperFactory ¶
ShaperFactory 根据连接信息返回读写限速器。 如果返回 nil,表示不限制。
type UDPMiddleware ¶ added in v1.4.0
type UDPMiddleware func(net.PacketConn) net.PacketConn
UDPMiddleware 定义 UDP 连接装饰器
func WithPPSLimit ¶ added in v1.4.0
func WithPPSLimit(cfg LimitConfig) UDPMiddleware
WithPPSLimit 限制 UDP 的每秒包数。
func WithUDPBuffer ¶ added in v1.4.0
func WithUDPBuffer(readBuf, writeBuf int) UDPMiddleware
WithUDPBuffer 设置内核缓冲区大小 QUIC 协议强烈建议增大 UDP 缓冲区以提升吞吐量并减少丢包