Skip to content

Commit 46afd26

Browse files
committed
cli/command: deprecate EventHandler and InitEventHandler
This code was only used as part of container.RunStats, so moving the code there instead as a non-exported type. The actual use also did not have to handle concurrency, so the mutex is removed in the new location. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent d06f137 commit 46afd26

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

Diff for: cli/command/container/stats.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/docker/api/types/events"
1919
"github.com/docker/docker/api/types/filters"
2020
"github.com/pkg/errors"
21+
"github.com/sirupsen/logrus"
2122
"github.com/spf13/cobra"
2223
)
2324

@@ -129,9 +130,9 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
129130
return err
130131
}
131132

132-
eh := command.InitEventHandler()
133+
eh := newEventHandler()
133134
if options.All {
134-
eh.Handle(events.ActionCreate, func(e events.Message) {
135+
eh.setHandler(events.ActionCreate, func(e events.Message) {
135136
s := NewStats(e.Actor.ID[:12])
136137
if cStats.add(s) {
137138
waitFirst.Add(1)
@@ -140,7 +141,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
140141
})
141142
}
142143

143-
eh.Handle(events.ActionStart, func(e events.Message) {
144+
eh.setHandler(events.ActionStart, func(e events.Message) {
144145
s := NewStats(e.Actor.ID[:12])
145146
if cStats.add(s) {
146147
waitFirst.Add(1)
@@ -149,7 +150,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
149150
})
150151

151152
if !options.All {
152-
eh.Handle(events.ActionDie, func(e events.Message) {
153+
eh.setHandler(events.ActionDie, func(e events.Message) {
153154
cStats.remove(e.Actor.ID[:12])
154155
})
155156
}
@@ -186,7 +187,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
186187
}
187188

188189
eventChan := make(chan events.Message)
189-
go eh.Watch(eventChan)
190+
go eh.watch(eventChan)
190191
stopped := make(chan struct{})
191192
go monitorContainerEvents(started, eventChan, stopped)
192193
defer close(stopped)
@@ -313,3 +314,31 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
313314
}
314315
return err
315316
}
317+
318+
// newEventHandler initializes and returns an eventHandler
319+
func newEventHandler() *eventHandler {
320+
return &eventHandler{handlers: make(map[events.Action]func(events.Message))}
321+
}
322+
323+
// eventHandler allows for registering specific events to setHandler.
324+
type eventHandler struct {
325+
handlers map[events.Action]func(events.Message)
326+
}
327+
328+
func (eh *eventHandler) setHandler(action events.Action, handler func(events.Message)) {
329+
eh.handlers[action] = handler
330+
}
331+
332+
// watch ranges over the passed in event chan and processes the events based on the
333+
// handlers created for a given action.
334+
// To stop watching, close the event chan.
335+
func (eh *eventHandler) watch(c <-chan events.Message) {
336+
for e := range c {
337+
h, exists := eh.handlers[e.Action]
338+
if !exists {
339+
continue
340+
}
341+
logrus.Debugf("event handler: received event: %v", e)
342+
go h(e)
343+
}
344+
}

Diff for: cli/command/events_utils.go

+4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99

1010
// EventHandler is abstract interface for user to customize
1111
// own handle functions of each type of events
12+
//
13+
// Deprecated: EventHandler is no longer used, and will be removed in the next release.
1214
type EventHandler interface {
1315
Handle(action events.Action, h func(events.Message))
1416
Watch(c <-chan events.Message)
1517
}
1618

1719
// InitEventHandler initializes and returns an EventHandler
20+
//
21+
// Deprecated: InitEventHandler is no longer used, and will be removed in the next release.
1822
func InitEventHandler() EventHandler {
1923
return &eventHandler{handlers: make(map[events.Action]func(events.Message))}
2024
}

0 commit comments

Comments
 (0)