diff --git a/changelog/unreleased/sse-keepalive.md b/changelog/unreleased/sse-keepalive.md new file mode 100644 index 000000000..8b61ffba7 --- /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 fef62abc1..81a56d1e1 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 aac0cdd42..df94316ac 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)