Documentation
¶
Overview ¶
Package throttle 提供批量聚合触发原语:攒满 N 条或到达 T 时间即批量 flush。
典型场景:
- 日志/事件批量写入(攒够 100 条或每秒 flush 一次)
- 消息推送批量合并(减少网络往返)
- 数据库批量 INSERT(攒 batch 提升吞吐)
与 chanx/stream 区别: throttle 是"攒批 + 定时触发"的专用原语, 不做扇出/路由/背压,只关注"何时 flush"。
纯标准库、并发安全。
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*config)
Option 配置 Throttle。
func WithInterval ¶
WithInterval 设置定时 flush 间隔(默认 1s)。到达间隔时间未满 batch 也 flush。
type Throttle ¶
type Throttle[T any] struct { // contains filtered or unexported fields }
Throttle 批量聚合触发器。并发安全。 调用 Add 添加数据;数据攒满 maxBatch 或间隔到达时触发 flushFn。 需调用 Start 启动定时器,Stop 停止并 flush 剩余。
func (*Throttle[T]) AddBatch ¶
func (t *Throttle[T]) AddBatch(items []T)
AddBatch 添加多条数据。可能触发一次或多次 flush。
Click to show internal directories.
Click to hide internal directories.