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

getEvents a bit simpler

This commit is contained in:
Victor Vieux 2013-07-17 13:56:09 +00:00
parent ec559c02b8
commit 8b3519c5f7

18
api.go
View file

@ -218,20 +218,21 @@ func getInfo(srv *Server, version float64, w http.ResponseWriter, r *http.Reques
} }
func getEvents(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { func getEvents(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
sendEvent := func(wf *utils.WriteFlusher, event *utils.JSONMessage) (bool, error) { sendEvent := func(wf *utils.WriteFlusher, event *utils.JSONMessage) (error) {
b, err := json.Marshal(event) b, err := json.Marshal(event)
if err != nil { if err != nil {
return true, nil return fmt.Errorf("JSON error")
} }
_, err = wf.Write(b) _, err = wf.Write(b)
if err != nil { if err != nil {
// On error, evict the listener
utils.Debugf("%s", err) utils.Debugf("%s", err)
srv.Lock() srv.Lock()
delete(srv.listeners, r.RemoteAddr) delete(srv.listeners, r.RemoteAddr)
srv.Unlock() srv.Unlock()
return false, err return err
} }
return false, nil return nil
} }
if err := parseForm(r); err != nil { if err := parseForm(r); err != nil {
@ -248,10 +249,11 @@ func getEvents(srv *Server, version float64, w http.ResponseWriter, r *http.Requ
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
wf := utils.NewWriteFlusher(w) wf := utils.NewWriteFlusher(w)
if since != 0 { if since != 0 {
// If since, send previous events that happened after the timestamp
for _, event := range srv.events { for _, event := range srv.events {
if event.Time >= since { if event.Time >= since {
skip, err := sendEvent(wf, &event) err := sendEvent(wf, &event)
if skip { if err != nil && err.Error() == "JSON error" {
continue continue
} }
if err != nil { if err != nil {
@ -262,8 +264,8 @@ func getEvents(srv *Server, version float64, w http.ResponseWriter, r *http.Requ
} }
for { for {
event := <-listener event := <-listener
skip, err := sendEvent(wf, &event) err := sendEvent(wf, &event)
if skip { if err != nil && err.Error() == "JSON error" {
continue continue
} }
if err != nil { if err != nil {