Wait on waitgroup instead of using time.Sleep

Closes #2784
Signed-off-by: bn4t <me@bn4t.me>
This commit is contained in:
bn4t 2020-10-12 10:32:53 +02:00
parent ac365d7175
commit 53e2110fa3
1 changed files with 6 additions and 5 deletions

View File

@ -156,6 +156,9 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
// Start a short-lived goroutine to retrieve the initial list of // Start a short-lived goroutine to retrieve the initial list of
// containers. // containers.
getContainerList() getContainerList()
// make sure each container get at least one valid stat data
waitFirst.Wait()
} else { } else {
// Artificially send creation events for the containers we were asked to // Artificially send creation events for the containers we were asked to
// monitor (same code path than we use when monitoring all containers). // monitor (same code path than we use when monitoring all containers).
@ -170,9 +173,9 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
// We don't expect any asynchronous errors: closeChan can be closed. // We don't expect any asynchronous errors: closeChan can be closed.
close(closeChan) close(closeChan)
// Do a quick pause to detect any error with the provided list of // make sure each container get at least one valid stat data
// container names. waitFirst.Wait()
time.Sleep(1500 * time.Millisecond)
var errs []string var errs []string
cStats.mu.Lock() cStats.mu.Lock()
for _, c := range cStats.cs { for _, c := range cStats.cs {
@ -186,8 +189,6 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
} }
} }
// before print to screen, make sure each container get at least one valid stat data
waitFirst.Wait()
format := opts.format format := opts.format
if len(format) == 0 { if len(format) == 0 {
if len(dockerCli.ConfigFile().StatsFormat) > 0 { if len(dockerCli.ConfigFile().StatsFormat) > 0 {