Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sse keepalive interval configurable #10411

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/sse-keepalive.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions services/sse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ Log services like the `userlog`, `clientlog` and `sse` are responsible for compo
## Subscribing

Clients can subscribe to the `/sse` endpoint to be informed by the server when an event happens. The `sse` endpoint will respect language changes of the user without needing to reconnect. Note that SSE has a limitation of six open connections per browser which can be reached if one has opened various tabs of the Web UI pointing to the same Infinite Scale instance.

## Keep SSE Connections Alive

Some intermediate proxies drop connections after an idle time with no activity. If this is the case, configure the `SSE_KEEPALIVE_INTERVAL` envvar. This will send periodic SSE comments to keep connections open.

4 changes: 3 additions & 1 deletion services/sse/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"time"

"github.com/owncloud/ocis/v2/ocis-pkg/shared"
)
Expand All @@ -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 to keep it open." introductionVersion:"7.0"`

Events Events
HTTP HTTP `yaml:"http"`
Expand Down
13 changes: 13 additions & 0 deletions services/sse/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/r3labs/sse/v2"
Expand Down Expand Up @@ -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)
Expand Down