From fddf50ce71b92482fb853c6938dc8ebfebded689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 24 Oct 2024 15:13:45 +0200 Subject: [PATCH] make sse keepalive interval configurable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- changelog/unreleased/sse-keepalive.md | 5 +++++ services/sse/pkg/config/config.go | 4 +++- services/sse/pkg/service/service.go | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/sse-keepalive.md diff --git a/changelog/unreleased/sse-keepalive.md b/changelog/unreleased/sse-keepalive.md new file mode 100644 index 0000000000..8b61ffba7d --- /dev/null +++ b/changelog/unreleased/sse-keepalive.md @@ -0,0 +1,5 @@ +Bugfix: make SSE keepalive interval configurable + +To prevent intermediate proxies from closing the SSE connection admins can now configure a `SSE_KEEPALIVE_INTERVAL`. + +https://github.com/owncloud/ocis/pull/10411 diff --git a/services/sse/pkg/config/config.go b/services/sse/pkg/config/config.go index fef62abc11..81a56d1e1c 100644 --- a/services/sse/pkg/config/config.go +++ b/services/sse/pkg/config/config.go @@ -2,6 +2,7 @@ package config import ( "context" + "time" "github.com/owncloud/ocis/v2/ocis-pkg/shared" ) @@ -14,7 +15,8 @@ type Config struct { Debug Debug `mask:"struct" yaml:"debug"` Tracing *Tracing `yaml:"tracing"` - Service Service `yaml:"-"` + Service Service `yaml:"-"` + KeepAliveInterval time.Duration `yaml:"keepalive_interval" env:"SSE_KEEPALIVE_INTERVAL" desc:"To prevent intermediate proxies from closing the SSE connection send periodic SSE comments." introductionVersion:"6.7"` Events Events HTTP HTTP `yaml:"http"` diff --git a/services/sse/pkg/service/service.go b/services/sse/pkg/service/service.go index aac0cdd426..df94316acb 100644 --- a/services/sse/pkg/service/service.go +++ b/services/sse/pkg/service/service.go @@ -2,6 +2,7 @@ package service import ( "net/http" + "time" "github.com/go-chi/chi/v5" "github.com/r3labs/sse/v2" @@ -81,6 +82,18 @@ func (s SSE) HandleSSE(w http.ResponseWriter, r *http.Request) { stream := s.sse.CreateStream(uid) stream.AutoReplay = false + if s.c.KeepAliveInterval != 0 { + ticker := time.NewTicker(s.c.KeepAliveInterval) + defer ticker.Stop() + go func() { + for range ticker.C { + s.sse.Publish(uid, &sse.Event{ + Comment: []byte("keepalive"), + }) + } + }() + } + // add stream to URL q := r.URL.Query() q.Set("stream", uid)