Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2abf5d9

Browse files
committed
refactor access to daemon member EventsService
Signed-off-by: Morgan Bauer <[email protected]>
1 parent e62fd33 commit 2abf5d9

4 files changed

Lines changed: 24 additions & 14 deletions

File tree

api/server/router/local/info.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.R
8888
}
8989

9090
enc := buildOutputEncoder(w)
91-
d := s.daemon
92-
es := d.EventsService
93-
current, l := es.Subscribe()
94-
defer es.Evict(l)
9591

96-
eventFilter := d.GetEventFilter(ef)
92+
current, l, cancel := s.daemon.SubscribeToEvents()
93+
defer cancel()
94+
95+
eventFilter := s.daemon.GetEventFilter(ef)
9796
handleEvent := func(ev *jsonmessage.JSONMessage) error {
9897
if eventFilter.Include(ev) {
9998
if err := enc.Encode(ev); err != nil {

daemon/daemon.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/docker/docker/pkg/graphdb"
4040
"github.com/docker/docker/pkg/idtools"
4141
"github.com/docker/docker/pkg/ioutils"
42+
"github.com/docker/docker/pkg/jsonmessage"
4243
"github.com/docker/docker/pkg/namesgenerator"
4344
"github.com/docker/docker/pkg/nat"
4445
"github.com/docker/docker/pkg/parsers/filters"
@@ -548,6 +549,11 @@ func (daemon *Daemon) GetEventFilter(filter filters.Args) *events.Filter {
548549
return events.NewFilter(filter, daemon.GetLabels)
549550
}
550551

552+
// SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events.
553+
func (daemon *Daemon) SubscribeToEvents() ([]*jsonmessage.JSONMessage, chan interface{}, func()) {
554+
return daemon.EventsService.Subscribe()
555+
}
556+
551557
// GetLabels for a container or image id
552558
func (daemon *Daemon) GetLabels(id string) map[string]string {
553559
// TODO: TestCase

daemon/events/events.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ func New() *Events {
2525
}
2626
}
2727

28-
// Subscribe adds new listener to events, returns slice of 64 stored last events
29-
// channel in which you can expect new events in form of interface{}, so you
30-
// need type assertion.
31-
func (e *Events) Subscribe() ([]*jsonmessage.JSONMessage, chan interface{}) {
28+
// Subscribe adds new listener to events, returns slice of 64 stored
29+
// last events, a channel in which you can expect new events (in form
30+
// of interface{}, so you need type assertion), and a function to call
31+
// to stop the stream of events.
32+
func (e *Events) Subscribe() ([]*jsonmessage.JSONMessage, chan interface{}, func()) {
3233
e.mu.Lock()
3334
current := make([]*jsonmessage.JSONMessage, len(e.events))
3435
copy(current, e.events)
3536
l := e.pub.Subscribe()
3637
e.mu.Unlock()
37-
return current, l
38+
39+
cancel := func() {
40+
e.Evict(l)
41+
}
42+
return current, l, cancel
3843
}
3944

4045
// Evict evicts listener from pubsub

daemon/events/events_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
func TestEventsLog(t *testing.T) {
1212
e := New()
13-
_, l1 := e.Subscribe()
14-
_, l2 := e.Subscribe()
13+
_, l1, _ := e.Subscribe()
14+
_, l2, _ := e.Subscribe()
1515
defer e.Evict(l1)
1616
defer e.Evict(l2)
1717
count := e.SubscribersCount()
@@ -65,7 +65,7 @@ func TestEventsLog(t *testing.T) {
6565

6666
func TestEventsLogTimeout(t *testing.T) {
6767
e := New()
68-
_, l := e.Subscribe()
68+
_, l, _ := e.Subscribe()
6969
defer e.Evict(l)
7070

7171
c := make(chan struct{})
@@ -91,7 +91,7 @@ func TestLogEvents(t *testing.T) {
9191
e.Log(action, id, from)
9292
}
9393
time.Sleep(50 * time.Millisecond)
94-
current, l := e.Subscribe()
94+
current, l, _ := e.Subscribe()
9595
for i := 0; i < 10; i++ {
9696
num := i + eventsLimit + 16
9797
action := fmt.Sprintf("action_%d", num)

0 commit comments

Comments
 (0)