Help us shape the future of Go! Take the 2026 Go Developer Survey

pacer

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package pacer 流式增量节流器:LLM token 级分片若逐条发布会洪泛事件通道与 前端(每秒数次×千字级文本),按「距上次发布 ≥interval 或缓冲 ≥bytes」合并 发布;总量帽内单流 seq 自 1 单调递增;权威快照到达即 Absorb 收口(此后迟到 分片丢弃);收尾残尾用 Flush 落地。

单 goroutine 契约:与消费方事件回调同一约束面(回调在 drain 循环内同步执行, 不阻塞不并发),故不加锁;跨 goroutine 使用由调用方自行串行化。

Index

Constants

View Source
const (
	// DefaultInterval 缺省时间窗。
	DefaultInterval = 400 * time.Millisecond
	// DefaultBytes 缺省缓冲窗(字节口径:缓冲达到即提前发布;CJK 场景
	// 200 字节 ≈ 66 汉字)。v0.12.0 改名 DefaultChars→DefaultBytes——
	// 实现一直是 len(s.text) 字节比较,命名与口径对齐(第八轮审计)。
	DefaultBytes = 200
	// DefaultCapBytes 单流发布总量帽(深度思考可极长,超帽静默停发)。
	DefaultCapBytes = 64 << 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Delta

type Delta struct {
	Seq  int
	Text string
}

Delta 一次待发布增量:Seq 单流内自 1 递增;Text 为合并分片(非累积快照—— 前端按序追加,避免长文本 O(n²) 重发)。

type Option

type Option func(*Stream)

Option Stream 可选项。

func WithClock

func WithClock(now func() time.Time) Option

WithClock 注入时钟(测试用;缺省 time.Now)。

type Stream

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

Stream 单条流的增量缓冲与节拍判定。

func NewStream

func NewStream(interval time.Duration, bytes, capBytes int, opts ...Option) *Stream

NewStream 构造;interval<=0 / bytes<=0 / capBytes<=0 取对应缺省值。

func (*Stream) Absorb

func (s *Stream) Absorb()

Absorb 权威快照到达:增量缓冲作废并收口(消费方以权威整段替换增量累积)。

func (*Stream) Buffered

func (s *Stream) Buffered() int

Buffered 当前滞留缓冲的字节数(观测面:超帽/收口后应恒 0——超帽静默停发 的感知锚点,消费方据此区分「流进行中」与「已被帽截断、等权威快照」)。

func (*Stream) Flush

func (s *Stream) Flush() (Delta, bool)

Flush 立即发布缓冲残尾(帽内);空缓冲/已收口/超帽不动序号。

func (*Stream) Offer

func (s *Stream) Offer(chunk string) (Delta, bool)

Offer 接收一个增量分片:按节流窗口判定是否发布。done(权威已到)后的 迟到分片丢弃。

func (*Stream) Sealed

func (s *Stream) Sealed() bool

Sealed 是否已收口(Absorb 之后)。

Jump to

Keyboard shortcuts

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