67ee76e58d
Deploy Slides / build-and-deploy (push) Successful in 1m8s
- Add 5 new slide pages: eino-deep-dive, ratelimit-overview, ratelimit-lua, async-deep-dive, harness - Add 4 new drawio SVGs: pipeline-detail, ratelimit, async-task, harness - Update all existing slides with v-clicks progressive reveal - Add presenter notes and interactive prompts - Expand slides.md to register all new pages (16 → 25+ slides) - Fix text overflow by trimming content per Item block - Add Eino Graph deep dive: WithGenLocalState, Pre/Post Handler, branch routing - Add rate limiting section: algorithm comparison, Lua script, Gin middleware - Add async task deep dive: TaskQueue FIFO, context progress injection - Add consistency section: style/pipeline/data/deployment 4-layer guarantees
1.5 KiB
1.5 KiB
令牌桶 — Redis Lua 脚本
原子操作,单次 Redis 往返
local data = redis.call('HMGET', key, 'tokens', 'ts')
local tokens = tonumber(data[1])
local last_ts = tonumber(data[2])
-- 首次访问,初始化满桶
if tokens == nil then
tokens = burst
last_ts = now
end
-- 计算时间差,补充令牌
local delta = now - last_ts
if delta > 0 then
local refill = (delta * rate) / 1000
tokens = math.min(burst, tokens + refill)
end
限流中间件集成
Gin 中间件 + 统一错误格式
`func RateLimit(limiter Limiter, keyFunc func(c *gin.Context) string) gin.HandlerFunc` — 可配置的 key 提取函数,支持按用户ID、IP、端点等维度限流。- 用户级:auth 路由组,key = userID,保护个人配额
- 全局级:
/generate端点,key = endpoint,保护总容量
- 通过:设置
X-RateLimit-Remaining头 - 拒绝:HTTP 429 +
Retry-After头 + 统一 JSON 错误体