Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
bugfix: the timeout specified by user should be used firstly
Browse files Browse the repository at this point in the history
Signed-off-by: lowzj <zj3142063@gmail.com>
  • Loading branch information
lowzj committed Nov 22, 2019
1 parent 04f0ffe commit 5633b9e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions dfget/config/constants.go
Expand Up @@ -116,9 +116,9 @@ const (
LocalHTTPPathRate = "/rate/"
LocalHTTPPing = "/server/ping"

DataExpireTime = 3 * time.Minute
ServerAliveTime = 5 * time.Minute
DefaultDownlodTimeout = 5 * time.Minute
DataExpireTime = 3 * time.Minute
ServerAliveTime = 5 * time.Minute
DefaultDownloadTimeout = 5 * time.Minute

DefaultSupernodePort = 8002
)
Expand Down
21 changes: 17 additions & 4 deletions dfget/core/core.go
Expand Up @@ -168,10 +168,7 @@ func registerToSuperNode(cfg *config.Config, register regist.SupernodeRegister)

func downloadFile(cfg *config.Config, supernodeAPI api.SupernodeAPI,
register regist.SupernodeRegister, result *regist.RegisterResult) error {
timeout := netutils.CalculateTimeout(cfg.RV.FileLength, cfg.MinRate, config.DefaultMinRate, 10*time.Second)
if timeout == 0 && cfg.Timeout > 0 {
timeout = cfg.Timeout
}
timeout := calculateTimeout(cfg)

success := true
err := doDownload(cfg, supernodeAPI, register, result, timeout)
Expand Down Expand Up @@ -328,3 +325,19 @@ func reportMetrics(cfg *config.Config, supernodeAPI api.SupernodeAPI, downloadTi
}
}
}

func calculateTimeout(cfg *config.Config) time.Duration {
if cfg == nil {
return config.DefaultDownloadTimeout
}
// the timeout specified by user should be used firstly
if cfg.Timeout > 0 {
return cfg.Timeout
}
timeout := netutils.CalculateTimeout(cfg.RV.FileLength, cfg.MinRate,
config.DefaultMinRate, 10*time.Second)
if timeout > 0 {
return timeout
}
return config.DefaultDownloadTimeout
}
5 changes: 3 additions & 2 deletions dfget/core/downloader/downloader.go
Expand Up @@ -42,8 +42,9 @@ type Downloader interface {
// the given timeout duration.
func DoDownloadTimeout(downloader Downloader, timeout time.Duration) error {
if timeout <= 0 {
logrus.Warnf("invalid download timeout(%.3fs)", timeout.Seconds())
timeout = config.DefaultDownlodTimeout
logrus.Warnf("invalid download timeout(%.3fs), use default:(%.3fs)",
timeout.Seconds(), config.DefaultDownloadTimeout.Seconds())
timeout = config.DefaultDownloadTimeout
}
ctx, cancel := context.WithCancel(context.Background())

Expand Down

0 comments on commit 5633b9e

Please sign in to comment.