make sse keepalive interval configurable

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2024-10-24 15:13:45 +02:00
parent 4dfba210e1
commit fddf50ce71
3 changed files with 21 additions and 1 deletions

View File

@@ -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

View File

@@ -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"`

View File

@@ -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)