From 5ed6c128e8d89bea0e37d42414118a047d086196 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 20 Dec 2023 11:34:34 +0100 Subject: [PATCH] cli/command/container: runStats(): don't register unused event handlers We were unconditionally registering event-handlers for these events, but the handler itself would ignore the event depending on the "all" option. This patch skips registering the event handlers, so that we're not handling them (saving some resources). Signed-off-by: Sebastiaan van Stijn --- cli/command/container/stats.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/command/container/stats.go b/cli/command/container/stats.go index a972c41225..d84126593e 100644 --- a/cli/command/container/stats.go +++ b/cli/command/container/stats.go @@ -129,15 +129,15 @@ func runStats(ctx context.Context, dockerCLI command.Cli, options *statsOptions) // would "miss" a creation. started := make(chan struct{}) eh := command.InitEventHandler() - eh.Handle(events.ActionCreate, func(e events.Message) { - if options.all { + if options.all { + eh.Handle(events.ActionCreate, func(e events.Message) { s := NewStats(e.Actor.ID[:12]) if cStats.add(s) { waitFirst.Add(1) go collect(ctx, s, apiClient, !options.noStream, waitFirst) } - } - }) + }) + } eh.Handle(events.ActionStart, func(e events.Message) { s := NewStats(e.Actor.ID[:12]) @@ -147,11 +147,11 @@ func runStats(ctx context.Context, dockerCLI command.Cli, options *statsOptions) } }) - eh.Handle(events.ActionDie, func(e events.Message) { - if !options.all { + if !options.all { + eh.Handle(events.ActionDie, func(e events.Message) { cStats.remove(e.Actor.ID[:12]) - } - }) + }) + } eventChan := make(chan events.Message) go eh.Watch(eventChan)