Skip to content

Commit

Permalink
fix: check health too long when dfdaemon is unavailable (dragonflyoss…
Browse files Browse the repository at this point in the history
…#344)

Signed-off-by: louhwz <[email protected]>
  • Loading branch information
Louhwz authored Jun 18, 2021
1 parent 3d9322f commit 41599d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
15 changes: 3 additions & 12 deletions client/dfget/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 12 additions & 17 deletions cmd/dfget/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 41599d6

Please sign in to comment.