diff --git a/client/dfget/dfget.go b/client/dfget/dfget.go index 20238016a..6c80f2347 100644 --- a/client/dfget/dfget.go +++ b/client/dfget/dfget.go @@ -126,25 +126,16 @@ func downloadFromSource(cfg *config.DfgetConfig, hdr map[string]string) (err err logger.Warnf("%s", err) return err } - - var ( - start = time.Now() - end time.Time - ) - fmt.Println("dfget download error, try to download from source") + var ( + start = time.Now() + end time.Time target *os.File response io.ReadCloser - _ map[string]string written int64 ) - if err != nil { - logger.Errorf("init source client error: %s", err) - return err - } - response, err = source.Download(context.Background(), cfg.URL, hdr) if err != nil { logger.Errorf("download from source error: %s", err) diff --git a/cmd/dfget/cmd/root.go b/cmd/dfget/cmd/root.go index c44665e77..b36c0a197 100644 --- a/cmd/dfget/cmd/root.go +++ b/cmd/dfget/cmd/root.go @@ -160,9 +160,9 @@ func runDfget() error { daemonClient, err := checkAndSpawnDaemon() if err != nil { logger.Errorf("check and spawn daemon error:%v", err) + } else { + logger.Info("check and spawn daemon success") } - - logger.Info("check and spawn daemon success") return dfget.Download(dfgetConfig, daemonClient) } @@ -201,24 +201,19 @@ func checkAndSpawnDaemon() (client.DaemonClient, error) { return nil, err } - // 3.First check since starting - if daemonClient.CheckHealth(context.Background(), target) == nil { - return daemonClient, nil - } + // 3. check health with at least 5s timeout + tick := time.Tick(50 * time.Millisecond) + timeout := time.After(5 * time.Second) - times := 0 - limit := 100 - interval := 50 * time.Millisecond for { - // 4.Cycle check with 5s timeout - if daemonClient.CheckHealth(context.Background(), target) == nil { - return daemonClient, nil - } - - times++ - if times > limit { + select { + case <-timeout: return nil, errors.New("the daemon is unhealthy") + case <-tick: + if err = daemonClient.CheckHealth(context.Background(), target); err != nil { + continue + } + return daemonClient, nil } - time.Sleep(interval) } }