Skip to content

Go 后端 STREAMING_TIMEOUT 默认值过短,导致 Claude Code /compact 等长耗时流式请求经常被中转主动断流 #415

Closed

Description

@LIghtJUNction

现象

用户通过本服务中转使用 Claude Code 时,反馈"一直在压缩"、"压缩频繁失败"、"压缩卡在 95%"。实际部署使用的是 Go 后端(Rust 后端 apps/api-rust 目前尚未上线,见 .env.example 注释"Go is the stable/default backend"),因此问题根因必须定位在 Go 侧代码,而不是 Rust 侧的 relay_http.rs(后者是尚未启用的并行实现,行为可能相似但不是当前线上问题的成因)。

根因

  • apps/api-go/constant/env.go:3:var StreamingTimeout int 全局变量。
  • apps/api-go/common/init.go:193:启动时从环境变量读取,默认值 300 秒:
    constant.StreamingTimeout = GetEnvOrDefault("STREAMING_TIMEOUT", 300)
  • apps/api-go/relay/helper/stream_scanner.go:这是实际生效的地方。
    • 第 98 行:streamingTimeout := time.Duration(constant.StreamingTimeout) * time.Second
    • 第 119 行:ticker := time.NewTicker(streamingTimeout)
    • 第 304 行:scanner 每扫描到一行新数据就 ticker.Reset(streamingTimeout)
    • 第 361 行:case <-ticker.C: 触发时,判定为流式响应超时,主动中断连接。

这是一个空闲间隔(idle-gap)保护,不是总时长限制:只要上游持续有字节输出(哪怕只是心跳注释行),计时器就会不断被重置;但只要某一次等待间隔超过 300 秒没有任何新字节到达,流就会被判定超时并强制中断。

Claude Code 的 /compact 上下文压缩会向上游模型发送一次性的大摘要请求,尤其在推理型模型或负载较高的上游通道上,生成下一段可见输出之间的间隔很容易超过 300 秒,从而被中转误判为超时、主动断流,表现为客户端侧"压缩失败/卡住"。

建议方向(暂不需要立即实现)

  • 将 apps/api-go/common/init.go:193 里 STREAMING_TIMEOUT 的默认值从 300 秒调大(例如 900 秒),使其能覆盖长耗时摘要/推理场景下常见的字节间隔。
  • 同步更新 .env.example 里对 STREAMING_TIMEOUT 的说明(目前完全没有提及这个变量,用户不知道可以通过环境变量覆盖)。
  • 确认是否需要同步调整 Rust 侧 apps/api-rust/src/relay_http.rs 的 RelayTimeoutConfig 对应默认值(idle: Duration::from_secs(300) 及 from_lookup 里的默认 300),保持未来切换/双写时两个后端行为一致。

影响范围

  • apps/api-go/common/init.go
  • apps/api-go/constant/env.go
  • apps/api-go/relay/helper/stream_scanner.go
  • .env.example(未文档化 STREAMING_TIMEOUT)
  • (潜在,非本次问题成因)apps/api-rust/src/relay_http.rs、apps/api-rust/docs/relay-timeouts.md

Activity

  1. added a commit that references this issue on Sep 21, 2026
    da80c3c
  2. added a commit that references this issue on Sep 21, 2026
    5692b03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions