Skip to content

Commit

Permalink
events endpoint: header: do not wait for events
Browse files Browse the repository at this point in the history
Do not wait for events to occur before writing the OK header.
Events can take an unknown amount of time to occur and clients
do not need to wait until then to know if the connection is
good.

Fixes: containers#7263
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Sep 15, 2020
1 parent 5c47a33 commit 0b7cb2c
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions pkg/api/handlers/compat/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"sync"

"github.com/containers/podman/v2/libpod"
"github.com/containers/podman/v2/libpod/events"
Expand Down Expand Up @@ -113,8 +112,13 @@ func GetEvents(w http.ResponseWriter, r *http.Request) {
errorChannel <- runtime.Events(r.Context(), readOpts)
}()

var coder *jsoniter.Encoder
var writeHeader sync.Once
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
coder := json.NewEncoder(w)
coder.SetEscapeHTML(true)

for stream := true; stream; stream = query.Stream {
select {
Expand All @@ -124,18 +128,6 @@ func GetEvents(w http.ResponseWriter, r *http.Request) {
return
}
case evt := <-eventChannel:
writeHeader.Do(func() {
// Use a sync.Once so that we write the header
// only once.
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
coder = json.NewEncoder(w)
coder.SetEscapeHTML(true)
})

if evt == nil {
continue
}
Expand Down

0 comments on commit 0b7cb2c

Please sign in to comment.