anacrolix / anacrolix/torrent

Support passing context to `torrent.Reader`

未关闭
#878 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
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 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。