1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

refactor access to daemon member EventsService

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
This commit is contained in:
Morgan Bauer 2015-10-12 11:54:46 -07:00
parent e62fd33d96
commit 2abf5d986e
No known key found for this signature in database
GPG key ID: 23F15C502128F348
4 changed files with 24 additions and 14 deletions

View file

@ -88,12 +88,11 @@ func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.R
}
enc := buildOutputEncoder(w)
d := s.daemon
es := d.EventsService
current, l := es.Subscribe()
defer es.Evict(l)
eventFilter := d.GetEventFilter(ef)
current, l, cancel := s.daemon.SubscribeToEvents()
defer cancel()
eventFilter := s.daemon.GetEventFilter(ef)
handleEvent := func(ev *jsonmessage.JSONMessage) error {
if eventFilter.Include(ev) {
if err := enc.Encode(ev); err != nil {

View file

@ -39,6 +39,7 @@ import (
"github.com/docker/docker/pkg/graphdb"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/docker/docker/pkg/nat"
"github.com/docker/docker/pkg/parsers/filters"
@ -548,6 +549,11 @@ func (daemon *Daemon) GetEventFilter(filter filters.Args) *events.Filter {
return events.NewFilter(filter, daemon.GetLabels)
}
// SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events.
func (daemon *Daemon) SubscribeToEvents() ([]*jsonmessage.JSONMessage, chan interface{}, func()) {
return daemon.EventsService.Subscribe()
}
// GetLabels for a container or image id
func (daemon *Daemon) GetLabels(id string) map[string]string {
// TODO: TestCase

View file

@ -25,16 +25,21 @@ func New() *Events {
}
}
// Subscribe adds new listener to events, returns slice of 64 stored last events
// channel in which you can expect new events in form of interface{}, so you
// need type assertion.
func (e *Events) Subscribe() ([]*jsonmessage.JSONMessage, chan interface{}) {
// Subscribe adds new listener to events, returns slice of 64 stored
// last events, a channel in which you can expect new events (in form
// of interface{}, so you need type assertion), and a function to call
// to stop the stream of events.
func (e *Events) Subscribe() ([]*jsonmessage.JSONMessage, chan interface{}, func()) {
e.mu.Lock()
current := make([]*jsonmessage.JSONMessage, len(e.events))
copy(current, e.events)
l := e.pub.Subscribe()
e.mu.Unlock()
return current, l
cancel := func() {
e.Evict(l)
}
return current, l, cancel
}
// Evict evicts listener from pubsub

View file

@ -10,8 +10,8 @@ import (
func TestEventsLog(t *testing.T) {
e := New()
_, l1 := e.Subscribe()
_, l2 := e.Subscribe()
_, l1, _ := e.Subscribe()
_, l2, _ := e.Subscribe()
defer e.Evict(l1)
defer e.Evict(l2)
count := e.SubscribersCount()
@ -65,7 +65,7 @@ func TestEventsLog(t *testing.T) {
func TestEventsLogTimeout(t *testing.T) {
e := New()
_, l := e.Subscribe()
_, l, _ := e.Subscribe()
defer e.Evict(l)
c := make(chan struct{})
@ -91,7 +91,7 @@ func TestLogEvents(t *testing.T) {
e.Log(action, id, from)
}
time.Sleep(50 * time.Millisecond)
current, l := e.Subscribe()
current, l, _ := e.Subscribe()
for i := 0; i < 10; i++ {
num := i + eventsLimit + 16
action := fmt.Sprintf("action_%d", num)