Skip to content

Commit

Permalink
Merge pull request #20792 from cpuguy83/more_stats_client_cleanup
Browse files Browse the repository at this point in the history
Fixes issue with stats on start event
  • Loading branch information
Arnaud Porterie committed Mar 1, 2016
2 parents 67b16c7 + df95474 commit 187a2fb
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 233 deletions.
30 changes: 30 additions & 0 deletions api/client/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"io"
"sort"
"strings"
"sync"
"time"

"golang.org/x/net/context"

"github.com/Sirupsen/logrus"
Cli "github.com/docker/docker/cli"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/jsonlog"
Expand Down Expand Up @@ -115,3 +117,31 @@ func printOutput(event eventtypes.Message, output io.Writer) {
}
fmt.Fprint(output, "\n")
}

type eventHandler struct {
handlers map[string]func(eventtypes.Message)
mu sync.Mutex
closed bool
}

func (w *eventHandler) Handle(action string, h func(eventtypes.Message)) {
w.mu.Lock()
w.handlers[action] = h
w.mu.Unlock()
}

// Watch ranges over the passed in event chan and processes the events based on the
// handlers created for a given action.
// To stop watching, close the event chan.
func (w *eventHandler) Watch(c <-chan eventtypes.Message) {
for e := range c {
w.mu.Lock()
h, exists := w.handlers[e.Action]
w.mu.Unlock()
if !exists {
continue
}
logrus.Debugf("event handler: received event: %v", e)
go h(e)
}
}
Loading

0 comments on commit 187a2fb

Please sign in to comment.