Support passing context to `torrent.Reader`
- 主要语言
- Go
- 星标
- 6.1k
- 派生
- 690
- 平均合并
- 3 天 10 小时
- 30 天内合并 PR
- 12
描述
I'm building a server that streams torrents through HTTP.
I just found out that we had the same idea of creating a "ContextReader", but I saw that [here](https://github.com/anacrolix/torrent/blob/e10ffd133acfb476b1ebd69f9380a4e300ad75b9/reader.go#L148C1-L148C1) you pass a fixed `context.Background()`, so there is no way for me to pass a context and cancel it.
What I ended up doing was:
```go
func streamChunk(ctx context.Context, start int64, end int64, dst io.Writer, src io.ReadSeekCloser) error {
_, err := src.Seek(start, io.SeekStart)
if err != nil {
return err
}
_, err = io.CopyN(dst, NewContextReader(ctx, src), end-start+1)
if err != nil {
return err
}
return nil
}
```
Where the context is the `http.Request.Context()`, `src` is the `torrent.Reader` and `dst` is the `http.ResponseWriter`.
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。