Skip to content

Commit

Permalink
Save extra http Request alloc in webseed request
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Feb 7, 2024
1 parent f067eff commit 877c4af
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion webseed/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (ws *Client) NewRequest(r RequestSpec) Request {
var requestParts []requestPart
if !ws.fileIndex.Locate(r, func(i int, e segments.Extent) bool {
req, err := newRequest(
ctx,
ws.Url, i, ws.info, e.Start, e.Length,
ws.PathEscaper,
)
if err != nil {
panic(err)
}
req = req.WithContext(ctx)
part := requestPart{
req: req,
result: make(chan requestPartResult, 1),
Expand Down
4 changes: 3 additions & 1 deletion webseed/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webseed

import (
"context"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -46,6 +47,7 @@ func trailingPath(

// Creates a request per BEP 19.
func newRequest(
ctx context.Context,
url_ string, fileIndex int,
info *metainfo.Info,
offset, length int64,
Expand All @@ -57,7 +59,7 @@ func newRequest(
// for things like spaces and '#'.
url_ += trailingPath(info.Name, fileInfo.Path, pathEscaper)
}
req, err := http.NewRequest(http.MethodGet, url_, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url_, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 877c4af

Please sign in to comment.