Document how to have read timeout be larger than idle timeout
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 20/100
- Issue 类型
- 功能
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- go
- 领域
- api, networking
调研方向
首先阅读 Reader 入口点和 Conn.msgReader,然后查看关联的 issue 87 以及示例中展示的 context 行为。确定当前 API 是否支持为获取 Reader 和完成读取分别设置时间限制,并记录受支持的方法或所需的 API 设计变更。
由索引模型根据 Issue 内容生成。
描述
Hi! Thanks for this excellent library.
I have a use case where I want the server to wait up to 10 seconds for the client to start sending data. If the server doesn't get a new Reader within that time, it closes the connection and bails.
However, if the client does start sending data, then they have a much longer time limit to finish sending. Say 2 minutes. As far as I can tell, there's not a good way to accomplish this with the current API design.
I saw this issue: https://github.com/nhooyr/websocket/issues/87
Which works IFF the idle timeout is larger than the read timeout.
This is because the Conn.msgReader holds on to the context passed in during Reader() Example:
ctx, cancel := context.WithTimeout(rootCtx, 10 *second)
defer cancel()
_, reader, err := ws.Reader(ctx)
if err != nil {
// Err handling....
log.Error(err)
return
}
// If reading data takes longer than 10 seconds, the timeout above will fire, and the context will be cancelled
// Killing the connnection
data, err := io.ReadAll(reader)
if err != nil {
// Err handling....
log.Error(err)
return
}
I can potentially work around this. Instead of using context.WithTimeout, I could just use context.WithCancel. Then have a time.AfterFunc(), which uses atomics to check if we got the reader already. In which case, don't cancel. Example:
ctx, cancel := context.WithCancel(rootCtx)
defer cancel()
gotReader := atomic.Bool{}
time.AfterFunc(10*time.Second, func() {
if !gotReader.Load() {
cancel()
}
})
_, reader, err := ws.Reader(ctx)
if err != nil {
// Err handling....
log.Error(err)
return
}
gotReader.Store(true)
time.AfterFunc(2*time.Minute, func() {
cancel()
})
data, err := io.ReadAll(reader)
if err != nil {
// Err handling....
log.Error(err)
return
}
I'm not sure what the best way to modify the API would be. You're unfortunately stuck with the io.Reader interface, without the ability to add a context. Thoughts?
- 主要语言
- Go
- 星标
- 5.5k
- 派生
- 379
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
coder/websocket 的其他 Issue
-
Must not wrap io.EOF可能已有人在做 @Yanhu007 于 174 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
export wstest未关闭enhancement
难度 2/5 1-3 小时 新手友好度 68/100
-
wasm: Close with codes other than 1000 throws, so CloseNow never closes可能已有人在做 @0pcom 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 20/100
-
难度 4/5 3-5 天 新手友好度 42/100
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 92/100
JuliusBrussee/caveman#1189 ·
维护者通常 1 天内回复
-
agent-review-finding chore
难度 2/5 半天 新手友好度 78/100
jordansmall/spindrift#4497 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
bug from-studio
难度 2/5 1-3 小时 新手友好度 70/100
esengine/DeepSeek-Reasonix#12044 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1-3 小时 新手友好度 82/100