Skip to content

Commit

Permalink
Merge pull request #10411 from owncloud/sse-keepalive
Browse files Browse the repository at this point in the history
make sse keepalive interval configurable
  • Loading branch information
butonic authored Oct 25, 2024
2 parents 84a9da5 + 4d47323 commit 821d7b5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
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

0 comments on commit 821d7b5

Please sign in to comment.