aboutsummaryrefslogtreecommitdiff
path: root/pkg/api/handlers/compat/events.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-04-13 20:28:42 +0200
committerGitHub <noreply@github.com>2020-04-13 20:28:42 +0200
commitd7695dd957b162ed24c807dc91200fe1abaecff1 (patch)
tree449aa4d72af1ab130561b12935a5aeb8f1b2a5c6 /pkg/api/handlers/compat/events.go
parent465b4bc563b274ec166868aae079a65ee0284b1d (diff)
parentf8892e7c6b07cb77979199cf0474ac2af80829fd (diff)
downloadpodman-d7695dd957b162ed24c807dc91200fe1abaecff1.tar.gz
podman-d7695dd957b162ed24c807dc91200fe1abaecff1.tar.bz2
podman-d7695dd957b162ed24c807dc91200fe1abaecff1.zip
Merge pull request #5777 from jwhonce/issues/5599
Refactor service idle support
Diffstat (limited to 'pkg/api/handlers/compat/events.go')
-rw-r--r--pkg/api/handlers/compat/events.go19
1 files changed, 16 insertions, 3 deletions
diff --git a/pkg/api/handlers/compat/events.go b/pkg/api/handlers/compat/events.go
index 0f72ef328..8ef32716d 100644
--- a/pkg/api/handlers/compat/events.go
+++ b/pkg/api/handlers/compat/events.go
@@ -1,7 +1,6 @@
package compat
import (
- "encoding/json"
"fmt"
"net/http"
@@ -10,6 +9,7 @@ import (
"github.com/containers/libpod/pkg/api/handlers"
"github.com/containers/libpod/pkg/api/handlers/utils"
"github.com/gorilla/schema"
+ jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@@ -48,14 +48,27 @@ func GetEvents(w http.ResponseWriter, r *http.Request) {
}()
if eventsError != nil {
utils.InternalServerError(w, eventsError)
+ close(eventChannel)
return
}
- coder := json.NewEncoder(w)
- coder.SetEscapeHTML(true)
+ // If client disappears we need to stop listening for events
+ go func(done <-chan struct{}) {
+ <-done
+ close(eventChannel)
+ }(r.Context().Done())
+ // Headers need to be written out before turning Writer() over to json encoder
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
+ if flusher, ok := w.(http.Flusher); ok {
+ flusher.Flush()
+ }
+
+ json := jsoniter.ConfigCompatibleWithStandardLibrary
+ coder := json.NewEncoder(w)
+ coder.SetEscapeHTML(true)
+
for event := range eventChannel {
e := handlers.EventToApiEvent(event)
if err := coder.Encode(e); err != nil {