-
Notifications
You must be signed in to change notification settings - Fork 200
feat(sse): support multiple topic subscriptions #2650
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/tmaxmax/go-sse" | ||
|
|
||
| revactx "github.com/opencloud-eu/reva/v2/pkg/ctx" | ||
| "github.com/opencloud-eu/reva/v2/pkg/events" | ||
|
|
||
| "github.com/opencloud-eu/opencloud/pkg/log" | ||
| "github.com/opencloud-eu/opencloud/services/sse/pkg/config" | ||
| ) | ||
|
|
||
| const ( | ||
| SSETopicAllUsers = "all" | ||
| ) | ||
|
|
||
| // SSEHandler defines implements the business logic for Service. | ||
| type SSEHandler struct { | ||
| conf *config.Config | ||
| logger log.Logger | ||
| server *sse.Server | ||
| channel <-chan events.Event | ||
| } | ||
|
|
||
| // NewSSEHandler returns a service implementation for Service. | ||
| func NewSSEHandler(ctx context.Context, conf *config.Config, logger log.Logger, ch <-chan events.Event) (SSEHandler, error) { | ||
| handler := SSEHandler{ | ||
| conf: conf, | ||
| logger: logger, | ||
| channel: ch, | ||
| } | ||
|
|
||
| handler.server = &sse.Server{ | ||
| OnSession: func(_ http.ResponseWriter, r *http.Request) (topics []string, allowed bool) { | ||
| return handler.topics(r) | ||
| }, | ||
| } | ||
|
|
||
| go func() { | ||
| select { | ||
| case <-ctx.Done(): | ||
| if err := handler.server.Shutdown(ctx); err != nil { | ||
| logger.Error().Err(err).Msg("failed to shutdown SSE handler") | ||
| } | ||
| return | ||
| } | ||
| }() | ||
|
|
||
| go handler.listen() | ||
|
|
||
| return handler, nil | ||
| } | ||
|
|
||
| // ServeHTTP fulfills Handler interface | ||
| func (h SSEHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
| topics, ok := h.topics(r) | ||
| if !ok { | ||
| h.logger.Error().Msg("sse: failed to get topics") | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| if h.conf.KeepAliveInterval != 0 { | ||
| ticker := time.NewTicker(h.conf.KeepAliveInterval) | ||
| defer ticker.Stop() | ||
| go func() { | ||
| for range ticker.C { | ||
| m := &sse.Message{} | ||
| m.AppendData("keep-alive") | ||
| if err := h.server.Publish(m, topics...); err != nil { | ||
| h.logger.Error().Err(err).Msg("sse: failed to publish message") | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
Comment on lines
+67
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 HIGH RISK The current keep-alive implementation has significant performance and resource issues:
Refactoring Suggestion: Move the keep-alive logic to a single background goroutine in |
||
|
|
||
| h.server.ServeHTTP(w, r) | ||
| } | ||
|
|
||
| // ListenForEvents listens for events | ||
| func (h SSEHandler) listen() { | ||
| for e := range h.channel { | ||
| switch ev := e.Event.(type) { | ||
| default: | ||
| h.logger.Error().Interface("event", ev).Msg("unhandled event") | ||
| case events.SendSSE: | ||
| m := &sse.Message{ | ||
| Type: sse.Type(ev.Type), | ||
| } | ||
| m.AppendData(string(ev.Message)) | ||
| if err := h.server.Publish(m, ev.UserIDs...); err != nil { | ||
| h.logger.Error().Err(err).Msg("sse: failed to publish message") | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+85
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK The |
||
| } | ||
|
|
||
| func (h SSEHandler) topics(r *http.Request) ([]string, bool) { | ||
| u, ok := revactx.ContextGetUser(r.Context()) | ||
| if !ok { | ||
| return nil, false | ||
| } | ||
|
|
||
| uid := u.GetId().GetOpaqueId() | ||
| if uid == "" { | ||
| return nil, false | ||
| } | ||
|
|
||
| return append([]string{SSETopicAllUsers}, uid), true | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 MEDIUM RISK
Suggestion: Using the already-cancelled
ctxforShutdownwill prevent the server from waiting for active connections to drain. Use a fresh context with a timeout.