Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

dfdaemon: specify context when invokes dfget #1041

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions dfdaemon/downloader/dfget/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dfget

import (
"context"
"fmt"
netUrl "net/url"
"os/exec"
Expand All @@ -42,11 +43,11 @@ func NewGetter(cfg config.DFGetConfig) *DFGetter {
return &DFGetter{config: cfg}
}

// Download is the method of DFGetter to download by dragonfly.
func (dfGetter *DFGetter) Download(url string, header map[string][]string, name string) (string, error) {
// DownloadContext downloads the resources as specified in url.
func (dfGetter *DFGetter) DownloadContext(ctx context.Context, url string, header map[string][]string, name string) (string, error) {
startTime := time.Now()
dstPath := filepath.Join(dfGetter.config.DFRepo, name)
cmd := dfGetter.getCommand(url, header, dstPath)
cmd := dfGetter.getCommand(ctx, url, header, dstPath)
err := cmd.Run()
if cmd.ProcessState.Success() {
log.Infof("dfget url:%s [SUCCESS] cost:%.3fs", url, time.Since(startTime).Seconds())
Expand All @@ -62,7 +63,7 @@ func (dfGetter *DFGetter) Download(url string, header map[string][]string, name

// getCommand returns the command to download the given resource.
func (dfGetter *DFGetter) getCommand(
url string, header map[string][]string, output string,
ctx context.Context, url string, header map[string][]string, output string,
) (cmd *exec.Cmd) {
args := []string{
"-u", url,
Expand Down Expand Up @@ -107,5 +108,5 @@ func (dfGetter *DFGetter) getCommand(
}
}

return exec.Command(dfGetter.config.DFPath, args...)
return exec.CommandContext(ctx, dfGetter.config.DFPath, args...)
}
8 changes: 5 additions & 3 deletions dfdaemon/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package downloader

import "context"

// Interface specifies on how an plugin can download a file.
type Interface interface {
// Download download url file to file name
// return dst path and download error
Download(url string, header map[string][]string, name string) (string, error)
// DownloadContext downloads the resource as specified in url, and it accepts
// a context parameter so that it can handle timeouts correctly.
DownloadContext(ctx context.Context, url string, header map[string][]string, name string) (string, error)
}

// Factory is a function that returns a new downloader.
Expand Down
7 changes: 4 additions & 3 deletions dfdaemon/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package transport

import (
"context"
"crypto/tls"
"net"
"net/http"
Expand Down Expand Up @@ -133,7 +134,7 @@ func (roundTripper *DFRoundTripper) RoundTrip(req *http.Request) (*http.Response

// download uses dfget to download.
func (roundTripper *DFRoundTripper) download(req *http.Request, urlString string) (*http.Response, error) {
dstPath, err := roundTripper.downloadByGetter(urlString, req.Header, uuid.New())
dstPath, err := roundTripper.downloadByGetter(req.Context(), urlString, req.Header, uuid.New())
if err != nil {
logrus.Errorf("download fail: %v", err)
return nil, err
Expand All @@ -155,9 +156,9 @@ func (roundTripper *DFRoundTripper) download(req *http.Request, urlString string
}

// downloadByGetter is used to download file by DFGetter.
func (roundTripper *DFRoundTripper) downloadByGetter(url string, header map[string][]string, name string) (string, error) {
func (roundTripper *DFRoundTripper) downloadByGetter(ctx context.Context, url string, header map[string][]string, name string) (string, error) {
logrus.Infof("start download url:%s to %s in repo", url, name)
return roundTripper.Downloader.Download(url, header, name)
return roundTripper.Downloader.DownloadContext(ctx, url, header, name)
}

// needUseGetter is the default value for ShouldUseDfget, which downloads all
Expand Down