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

Commit 5633b9e

Browse files
committed
bugfix: the timeout specified by user should be used firstly
Signed-off-by: lowzj <[email protected]>
1 parent 04f0ffe commit 5633b9e

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

dfget/config/constants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ const (
116116
LocalHTTPPathRate = "/rate/"
117117
LocalHTTPPing = "/server/ping"
118118

119-
DataExpireTime = 3 * time.Minute
120-
ServerAliveTime = 5 * time.Minute
121-
DefaultDownlodTimeout = 5 * time.Minute
119+
DataExpireTime = 3 * time.Minute
120+
ServerAliveTime = 5 * time.Minute
121+
DefaultDownloadTimeout = 5 * time.Minute
122122

123123
DefaultSupernodePort = 8002
124124
)

dfget/core/core.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,7 @@ func registerToSuperNode(cfg *config.Config, register regist.SupernodeRegister)
168168

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

176173
success := true
177174
err := doDownload(cfg, supernodeAPI, register, result, timeout)
@@ -328,3 +325,19 @@ func reportMetrics(cfg *config.Config, supernodeAPI api.SupernodeAPI, downloadTi
328325
}
329326
}
330327
}
328+
329+
func calculateTimeout(cfg *config.Config) time.Duration {
330+
if cfg == nil {
331+
return config.DefaultDownloadTimeout
332+
}
333+
// the timeout specified by user should be used firstly
334+
if cfg.Timeout > 0 {
335+
return cfg.Timeout
336+
}
337+
timeout := netutils.CalculateTimeout(cfg.RV.FileLength, cfg.MinRate,
338+
config.DefaultMinRate, 10*time.Second)
339+
if timeout > 0 {
340+
return timeout
341+
}
342+
return config.DefaultDownloadTimeout
343+
}

dfget/core/downloader/downloader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ type Downloader interface {
4242
// the given timeout duration.
4343
func DoDownloadTimeout(downloader Downloader, timeout time.Duration) error {
4444
if timeout <= 0 {
45-
logrus.Warnf("invalid download timeout(%.3fs)", timeout.Seconds())
46-
timeout = config.DefaultDownlodTimeout
45+
logrus.Warnf("invalid download timeout(%.3fs), use default:(%.3fs)",
46+
timeout.Seconds(), config.DefaultDownloadTimeout.Seconds())
47+
timeout = config.DefaultDownloadTimeout
4748
}
4849
ctx, cancel := context.WithCancel(context.Background())
4950

0 commit comments

Comments
 (0)