Support passing context to `torrent.Reader`
- Langage dominant
- Go
- Étoiles
- 6.1k
- Forks
- 690
- Merge moyen
- 3 j 10 h
- PR mergées (30 j)
- 12
Description
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`.
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Évaluation
Cette issue n'a pas encore été évaluée.