diff --git a/dfget/core/downloader/downloader.go b/dfget/core/downloader/downloader.go index 29503c817..8e0357341 100644 --- a/dfget/core/downloader/downloader.go +++ b/dfget/core/downloader/downloader.go @@ -42,7 +42,8 @@ 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()) + logrus.Debugf("invalid download timeout(%.3fs), use default:(%.3fs)", + timeout.Seconds(), config.DefaultDownlodTimeout) timeout = config.DefaultDownlodTimeout } ctx, cancel := context.WithCancel(context.Background()) diff --git a/dfget/core/uploader/peer_server_executor.go b/dfget/core/uploader/peer_server_executor.go index 8b3bdbcd2..430a85c3b 100644 --- a/dfget/core/uploader/peer_server_executor.go +++ b/dfget/core/uploader/peer_server_executor.go @@ -144,6 +144,10 @@ func (pe *peerServerExecutor) checkPeerServerExist(cfg *config.Config, port int) if port <= 0 { port = getPortFromMeta(cfg.RV.MetaPath) } + if port <= 0 { + // port 0 is invalid + return 0 + } // check the peer server whether is available result, err := checkServer(cfg.RV.LocalIP, port, cfg.RV.DataDir, taskFileName, int(cfg.TotalLimit)) diff --git a/dfget/core/uploader/uploader.go b/dfget/core/uploader/uploader.go index 88e6cafeb..b53a39b67 100644 --- a/dfget/core/uploader/uploader.go +++ b/dfget/core/uploader/uploader.go @@ -120,27 +120,38 @@ func launch(cfg *config.Config, p2pPtr *unsafe.Pointer) error { return fmt.Errorf("start peer server error and retried at most %d times", retryCount) } -func waitForStartup(result chan error, p2pPtr *unsafe.Pointer) error { - select { - case err := <-result: - tmp := loadSrvPtr(p2pPtr) - if err == nil { - logrus.Infof("reuse exist server on port:%d", tmp.port) - tmp.setFinished() - } - return err - case <-time.After(100 * time.Millisecond): - // The peer server go routine will block and serve if it starts successfully. - // So we have to wait a moment and check again whether the peer server is - // started. - tmp := loadSrvPtr(p2pPtr) - if tmp == nil { - return fmt.Errorf("initialize peer server error") - } - if !uploaderAPI.PingServer(tmp.host, tmp.port) { - return fmt.Errorf("can't ping port:%d", tmp.port) +func waitForStartup(result chan error, p2pPtr *unsafe.Pointer) (err error) { + ticker := time.NewTicker(5 * time.Millisecond) + defer ticker.Stop() + timeout := time.After(233 * time.Millisecond) + + for { + select { + case <-ticker.C: + tmp := loadSrvPtr(p2pPtr) + if tmp != nil && uploaderAPI.PingServer(tmp.host, tmp.port) { + return nil + } + case err = <-result: + tmp := loadSrvPtr(p2pPtr) + if err == nil { + logrus.Infof("reuse exist server on port:%d", tmp.port) + tmp.setFinished() + } + return err + case <-timeout: + // The peer server go routine will block and serve if it starts successfully. + // So we have to wait a moment and check again whether the peer server is + // started. + tmp := loadSrvPtr(p2pPtr) + if tmp == nil { + return fmt.Errorf("initialize peer server error") + } + if !uploaderAPI.PingServer(tmp.host, tmp.port) { + return fmt.Errorf("can't ping port:%d", tmp.port) + } + return nil } - return nil } }