Skip to content

Commit

Permalink
chore: check task id (#2636)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ma <[email protected]>
  • Loading branch information
jim3ma authored Aug 15, 2023
1 parent 554b27c commit 0e04a56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions client/daemon/peer/piece_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ func NewPieceDownloader(timeout time.Duration, caCertPool *x509.CertPool) PieceD
}

func (p *pieceDownloader) DownloadPiece(ctx context.Context, req *DownloadPieceRequest) (io.Reader, io.Closer, error) {
httpRequest := p.buildDownloadPieceHTTPRequest(ctx, req)
httpRequest, err := p.buildDownloadPieceHTTPRequest(ctx, req)
if err != nil {
return nil, nil, err
}
resp, err := p.httpClient.Do(httpRequest)
if err != nil {
logger.Errorf("task id: %s, piece num: %d, dst: %s, download piece failed: %s",
Expand Down Expand Up @@ -198,7 +201,10 @@ func (p *pieceDownloader) DownloadPiece(ctx context.Context, req *DownloadPieceR
return reader, closer, nil
}

func (p *pieceDownloader) buildDownloadPieceHTTPRequest(ctx context.Context, d *DownloadPieceRequest) *http.Request {
func (p *pieceDownloader) buildDownloadPieceHTTPRequest(ctx context.Context, d *DownloadPieceRequest) (*http.Request, error) {
if len(d.TaskID) <= 3 {
return nil, fmt.Errorf("invalid task id")
}
// FIXME switch to https when tls enabled
targetURL := url.URL{
Scheme: p.scheme,
Expand All @@ -216,5 +222,5 @@ func (p *pieceDownloader) buildDownloadPieceHTTPRequest(ctx context.Context, d *

// inject trace id into request header
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header))
return req
return req, nil
}
4 changes: 3 additions & 1 deletion scheduler/resource/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ func (p *Peer) Children() []*Peer {
func (p *Peer) DownloadTinyFile() ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), downloadTinyFileContextTimeout)
defer cancel()

if len(p.Task.ID) <= 3 {
return nil, fmt.Errorf("invalid task id")
}
// Download path: ${host}:${port}/download/${taskIndex}/${taskID}?peerId=${peerID}
targetURL := url.URL{
Scheme: p.Config.Task.DownloadTiny.Scheme,
Expand Down

0 comments on commit 0e04a56

Please sign in to comment.