Skip to content

Commit 9645db7

Browse files
committed
cli/command/container: RunStats: pass ctx to stats event handlers
Wire up the context explicitly instead of capturing it in the closures. Also pass through the context to `watch` to replace the context.TODO() Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 499a4c5 commit 9645db7

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

cli/command/container/stats.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
145145

146146
eh := newEventHandler()
147147
if options.All {
148-
eh.setHandler(events.ActionCreate, func(e events.Message) {
148+
eh.setHandler(events.ActionCreate, func(ctx context.Context, e events.Message) {
149149
if s := NewStats(e.Actor.ID); cStats.add(s) {
150150
waitFirst.Add(1)
151151
log.G(ctx).WithFields(log.Fields{
@@ -157,7 +157,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
157157
})
158158
}
159159

160-
eh.setHandler(events.ActionStart, func(e events.Message) {
160+
eh.setHandler(events.ActionStart, func(ctx context.Context, e events.Message) {
161161
if s := NewStats(e.Actor.ID); cStats.add(s) {
162162
waitFirst.Add(1)
163163
log.G(ctx).WithFields(log.Fields{
@@ -169,7 +169,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
169169
})
170170

171171
if !options.All {
172-
eh.setHandler(events.ActionDie, func(e events.Message) {
172+
eh.setHandler(events.ActionDie, func(ctx context.Context, e events.Message) {
173173
log.G(ctx).WithFields(log.Fields{
174174
"event": e.Action,
175175
"container": e.Actor.ID,
@@ -216,7 +216,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
216216
}
217217

218218
eventChan := make(chan events.Message)
219-
go eh.watch(eventChan)
219+
go eh.watch(ctx, eventChan)
220220
stopped := make(chan struct{})
221221
go monitorContainerEvents(started, eventChan, stopped)
222222
defer close(stopped)
@@ -369,33 +369,33 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
369369

370370
// newEventHandler initializes and returns an eventHandler
371371
func newEventHandler() *eventHandler {
372-
return &eventHandler{handlers: make(map[events.Action]func(events.Message))}
372+
return &eventHandler{handlers: make(map[events.Action]func(context.Context, events.Message))}
373373
}
374374

375375
// eventHandler allows for registering specific events to setHandler.
376376
type eventHandler struct {
377-
handlers map[events.Action]func(events.Message)
377+
handlers map[events.Action]func(context.Context, events.Message)
378378
}
379379

380-
func (eh *eventHandler) setHandler(action events.Action, handler func(events.Message)) {
380+
func (eh *eventHandler) setHandler(action events.Action, handler func(context.Context, events.Message)) {
381381
eh.handlers[action] = handler
382382
}
383383

384384
// watch ranges over the passed in event chan and processes the events based on the
385385
// handlers created for a given action.
386386
// To stop watching, close the event chan.
387-
func (eh *eventHandler) watch(c <-chan events.Message) {
387+
func (eh *eventHandler) watch(ctx context.Context, c <-chan events.Message) {
388388
for e := range c {
389389
h, exists := eh.handlers[e.Action]
390390
if !exists {
391391
continue
392392
}
393393
if e.Actor.ID == "" {
394-
log.G(context.TODO()).WithField("event", e).Errorf("event handler: received %s event with empty ID", e.Action)
394+
log.G(ctx).WithField("event", e).Errorf("event handler: received %s event with empty ID", e.Action)
395395
continue
396396
}
397397

398-
log.G(context.TODO()).WithField("event", e).Debugf("event handler: received %s event for: %s", e.Action, e.Actor.ID)
399-
go h(e)
398+
log.G(ctx).WithField("event", e).Debugf("event handler: received %s event for: %s", e.Action, e.Actor.ID)
399+
go h(ctx, e)
400400
}
401401
}

0 commit comments

Comments
 (0)