Prechádzať zdrojové kódy

Send resp immediately on GET /events

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Brian Goff 10 rokov pred
rodič
commit
d0a299c027

+ 3 - 1
api/server/server.go

@@ -430,7 +430,9 @@ func (s *Server) getEvents(version version.Version, w http.ResponseWriter, r *ht
 	d := s.daemon
 	es := d.EventsService
 	w.Header().Set("Content-Type", "application/json")
-	enc := json.NewEncoder(ioutils.NewWriteFlusher(w))
+	outStream := ioutils.NewWriteFlusher(w)
+	outStream.Write(nil) // make sure response is sent immediately
+	enc := json.NewEncoder(outStream)
 
 	getContainerId := func(cn string) string {
 		c, err := d.Get(cn)

+ 29 - 0
integration-cli/docker_api_events_test.go

@@ -0,0 +1,29 @@
+package main
+
+import (
+	"net/http"
+	"time"
+
+	"github.com/go-check/check"
+)
+
+func (s *DockerSuite) TestEventsApiEmptyOutput(c *check.C) {
+	type apiResp struct {
+		resp *http.Response
+		err  error
+	}
+	chResp := make(chan *apiResp)
+	go func() {
+		resp, body, err := sockRequestRaw("GET", "/events", nil, "")
+		body.Close()
+		chResp <- &apiResp{resp, err}
+	}()
+
+	select {
+	case r := <-chResp:
+		c.Assert(r.err, check.IsNil)
+		c.Assert(r.resp.StatusCode, check.Equals, http.StatusOK)
+	case <-time.After(3 * time.Second):
+		c.Fatal("timeout waiting for events api to respond, should have responded immediately")
+	}
+}