diff options
author | Jhon Honce <jhonce@redhat.com> | 2020-04-09 11:05:18 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2020-04-13 08:11:36 -0700 |
commit | f8892e7c6b07cb77979199cf0474ac2af80829fd (patch) | |
tree | 8a46fa46124460e7e8602b8d518b184837df01aa /pkg/api/handlers | |
parent | 309a7f7d1bbd046b4bd1a961a7e8c5313aa11b8e (diff) | |
download | podman-f8892e7c6b07cb77979199cf0474ac2af80829fd.tar.gz podman-f8892e7c6b07cb77979199cf0474ac2af80829fd.tar.bz2 podman-f8892e7c6b07cb77979199cf0474ac2af80829fd.zip |
Refactor service idle support
* Move connection tracking into APIServer using ConnState()
* Remove Connection counters from CLI code
* Update events handler to support client not closing connection
* Improve logging messages
Fixes #5599
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'pkg/api/handlers')
-rw-r--r-- | pkg/api/handlers/compat/events.go | 19 |
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 { |