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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-12-20 11:34:34 +01:00
parent 2389768fb7
commit 5ed6c128e8
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 8 additions and 8 deletions

View File

@ -129,15 +129,15 @@ func runStats(ctx context.Context, dockerCLI command.Cli, options *statsOptions)
// would "miss" a creation. // would "miss" a creation.
started := make(chan struct{}) started := make(chan struct{})
eh := command.InitEventHandler() 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]) s := NewStats(e.Actor.ID[:12])
if cStats.add(s) { if cStats.add(s) {
waitFirst.Add(1) waitFirst.Add(1)
go collect(ctx, s, apiClient, !options.noStream, waitFirst) go collect(ctx, s, apiClient, !options.noStream, waitFirst)
} }
} })
}) }
eh.Handle(events.ActionStart, func(e events.Message) { eh.Handle(events.ActionStart, func(e events.Message) {
s := NewStats(e.Actor.ID[:12]) 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]) cStats.remove(e.Actor.ID[:12])
} })
}) }
eventChan := make(chan events.Message) eventChan := make(chan events.Message)
go eh.Watch(eventChan) go eh.Watch(eventChan)