useTorrentSources: fix http request resource leak (#562)
This commit is contained in:
parent
62c6fbc8f8
commit
1fc6093383
40
client.go
40
client.go
|
@ -1214,24 +1214,22 @@ func (t *Torrent) MergeSpec(spec *TorrentSpec) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func useTorrentSources(sources []string, t *Torrent) {
|
func useTorrentSources(sources []string, t *Torrent) {
|
||||||
for _, s := range sources {
|
// TODO: bind context to the lifetime of *Torrent so that it's cancelled if the torrent closes
|
||||||
go func(s string) {
|
ctx := context.Background()
|
||||||
err := useTorrentSource(s, t)
|
for i := 0; i < len(sources); i += 1 {
|
||||||
if err != nil {
|
s := sources[i]
|
||||||
|
go func() {
|
||||||
|
if err := useTorrentSource(ctx, s, t); err != nil {
|
||||||
t.logger.WithDefaultLevel(log.Warning).Printf("using torrent source %q: %v", s, err)
|
t.logger.WithDefaultLevel(log.Warning).Printf("using torrent source %q: %v", s, err)
|
||||||
} else {
|
} else {
|
||||||
t.logger.Printf("successfully used source %q", s)
|
t.logger.Printf("successfully used source %q", s)
|
||||||
}
|
}
|
||||||
}(s)
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func useTorrentSource(source string, t *Torrent) error {
|
func useTorrentSource(ctx context.Context, source string, t *Torrent) (err error) {
|
||||||
req, err := http.NewRequest(http.MethodGet, source, nil)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
defer cancel()
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
|
@ -1241,21 +1239,27 @@ func useTorrentSource(source string, t *Torrent) error {
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
req = req.WithContext(ctx)
|
var req *http.Request
|
||||||
resp, err := http.DefaultClient.Do(req)
|
if req, err = http.NewRequestWithContext(ctx, http.MethodGet, source, nil); err != nil {
|
||||||
if err != nil {
|
panic(err)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
mi, err := metainfo.Load(resp.Body)
|
var resp *http.Response
|
||||||
|
if resp, err = http.DefaultClient.Do(req); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var mi metainfo.MetaInfo
|
||||||
|
err = bencode.NewDecoder(resp.Body).Decode(&mi)
|
||||||
|
resp.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
return t.MergeSpec(TorrentSpecFromMetaInfo(mi))
|
return t.MergeSpec(TorrentSpecFromMetaInfo(&mi))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (cl *Client) dropTorrent(infoHash metainfo.Hash) (err error) {
|
func (cl *Client) dropTorrent(infoHash metainfo.Hash) (err error) {
|
||||||
t, ok := cl.torrents[infoHash]
|
t, ok := cl.torrents[infoHash]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in New Issue