0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-08 08:04:01 +02:00

feat(websocket): Phase 1 — replace SSE notification count with WebSocket

Add a thin in-memory pubsub broker and a SharedWorker-based WebSocket
client to deliver real-time notification count updates. This replaces
the SSE path for notification-count events with a persistent WebSocket
connection shared across all tabs.

New files:
- services/pubsub/broker.go: fan-out pubsub broker (DefaultBroker singleton)
- services/websocket/notifier.go: polls notification counts, publishes to broker
- routers/web/websocket/websocket.go: /-/ws endpoint, per-user topic subscription
- web_src/js/features/websocket.sharedworker.ts: SharedWorker with exponential
  backoff reconnect (50ms initial, 10s max, reconnect on close and error)

Modified files:
- routers/init.go: register websocket_service.Init()
- routers/web/web.go: add GET /-/ws route
- services/context/response.go: add Hijack() to forward http.Hijacker
  so coder/websocket can upgrade the connection
- web_src/js/features/notification.ts: port from SSE SharedWorker to WS SharedWorker
- webpack.config.ts: add websocket.sharedworker entry point

Part of RFC #36942.
This commit is contained in:
Epid 2026-03-23 23:31:17 +03:00
parent 2633f9677d
commit b9a94c688e
6 changed files with 342 additions and 0 deletions

View File

@ -54,6 +54,7 @@ import (
"code.gitea.io/gitea/services/task"
"code.gitea.io/gitea/services/uinotification"
"code.gitea.io/gitea/services/webhook"
websocket_service "code.gitea.io/gitea/services/websocket"
)
func mustInit(fn func() error) {
@ -160,6 +161,7 @@ func InitWebInstalled(ctx context.Context) {
mustInit(task.Init)
mustInit(repo_migrations.Init)
eventsource.GetManager().Init()
mustInit(websocket_service.Init)
mustInitCtx(ctx, mailer_incoming.Init)
mustInitCtx(ctx, syncAppConfForGit)

View File

@ -41,6 +41,7 @@ import (
"code.gitea.io/gitea/routers/web/user"
user_setting "code.gitea.io/gitea/routers/web/user/setting"
"code.gitea.io/gitea/routers/web/user/setting/security"
gitea_websocket "code.gitea.io/gitea/routers/web/websocket"
auth_service "code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/forms"
@ -592,6 +593,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) {
}, reqSignOut)
m.Any("/user/events", routing.MarkLongPolling, events.Events)
m.Get("/-/ws", gitea_websocket.Serve)
m.Group("/login/oauth", func() {
m.Group("", func() {

View File

@ -0,0 +1,53 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package websocket
import (
"encoding/json"
"fmt"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/pubsub"
gitea_ws "github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
)
// Serve handles WebSocket upgrade and event delivery for the signed-in user.
func Serve(ctx *context.Context) {
if !ctx.IsSigned {
ctx.Status(401)
return
}
conn, err := gitea_ws.Accept(ctx.Resp, ctx.Req, &gitea_ws.AcceptOptions{
InsecureSkipVerify: false,
})
if err != nil {
log.Error("websocket: accept failed: %v", err)
return
}
defer conn.CloseNow() //nolint:errcheck
topic := fmt.Sprintf("user-%d", ctx.Doer.ID)
ch, cancel := pubsub.DefaultBroker.Subscribe(topic)
defer cancel()
wsCtx := ctx.Req.Context()
for {
select {
case <-wsCtx.Done():
return
case msg, ok := <-ch:
if !ok {
return
}
if err := wsjson.Write(wsCtx, conn, json.RawMessage(msg)); err != nil {
log.Trace("websocket: write failed: %v", err)
return
}
}
}
}

65
services/pubsub/broker.go Normal file
View File

@ -0,0 +1,65 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package pubsub
import (
"sync"
)
// Broker is a simple in-memory pub/sub broker.
// It supports fan-out: one Publish call delivers the message to all active subscribers.
type Broker struct {
mu sync.RWMutex
subs map[string][]chan []byte
}
// DefaultBroker is the global singleton used by both routers and notifiers.
var DefaultBroker = NewBroker()
// NewBroker creates a new in-memory Broker.
func NewBroker() *Broker {
return &Broker{
subs: make(map[string][]chan []byte),
}
}
// Subscribe returns a channel that receives messages published to topic.
// Call the returned cancel function to unsubscribe.
func (b *Broker) Subscribe(topic string) (<-chan []byte, func()) {
ch := make(chan []byte, 8)
b.mu.Lock()
b.subs[topic] = append(b.subs[topic], ch)
b.mu.Unlock()
cancel := func() {
b.mu.Lock()
defer b.mu.Unlock()
subs := b.subs[topic]
for i, sub := range subs {
if sub == ch {
b.subs[topic] = append(subs[:i], subs[i+1:]...)
break
}
}
close(ch)
}
return ch, cancel
}
// Publish sends msg to all subscribers of topic.
// Non-blocking: slow subscribers are skipped.
func (b *Broker) Publish(topic string, msg []byte) {
b.mu.RLock()
subs := b.subs[topic]
b.mu.RUnlock()
for _, ch := range subs {
select {
case ch <- msg:
default:
// subscriber too slow — skip
}
}
}

View File

@ -0,0 +1,76 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package websocket
import (
"context"
"fmt"
"time"
activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/services/pubsub"
)
type notificationCountEvent struct {
Type string `json:"type"`
Count int64 `json:"count"`
}
func userTopic(userID int64) string {
return fmt.Sprintf("user-%d", userID)
}
// Init starts the background goroutine that polls notification counts
// and pushes updates to connected WebSocket clients.
func Init() error {
go graceful.GetManager().RunWithShutdownContext(run)
return nil
}
func run(ctx context.Context) {
ctx, _, finished := process.GetManager().AddTypedContext(ctx, "Service: WebSocket", process.SystemProcessType, true)
defer finished()
if setting.UI.Notification.EventSourceUpdateTime <= 0 {
return
}
then := timeutil.TimeStampNow().Add(-2)
timer := time.NewTicker(setting.UI.Notification.EventSourceUpdateTime)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-timer.C:
now := timeutil.TimeStampNow().Add(-2)
uidCounts, err := activities_model.GetUIDsAndNotificationCounts(ctx, then, now)
if err != nil {
log.Error("websocket: GetUIDsAndNotificationCounts: %v", err)
continue
}
for _, uidCount := range uidCounts {
msg, err := json.Marshal(notificationCountEvent{
Type: "notification-count",
Count: uidCount.Count,
})
if err != nil {
continue
}
pubsub.DefaultBroker.Publish(userTopic(uidCount.UserID), msg)
}
then = now
}
}
}

View File

@ -0,0 +1,144 @@
// One WebSocket connection per URL, shared across all tabs via SharedWorker.
// Messages from the server are JSON objects broadcast to all connected ports.
export {}; // make this a module to avoid global scope conflicts with other sharedworker files
const RECONNECT_DELAY_INITIAL = 50;
const RECONNECT_DELAY_MAX = 10000;
class WsSource {
url: string;
ws: WebSocket | null;
clients: MessagePort[];
reconnectTimer: ReturnType<typeof setTimeout> | null;
reconnectDelay: number;
constructor(url: string) {
this.url = url;
this.ws = null;
this.clients = [];
this.reconnectTimer = null;
this.reconnectDelay = RECONNECT_DELAY_INITIAL;
this.connect();
}
connect() {
this.ws = new WebSocket(this.url);
this.ws.addEventListener('open', () => {
this.reconnectDelay = RECONNECT_DELAY_INITIAL;
this.broadcast({type: 'status', message: `connected to ${this.url}`});
});
this.ws.addEventListener('message', (event: MessageEvent<string>) => {
try {
const msg = JSON.parse(event.data);
this.broadcast(msg);
} catch {
// ignore malformed JSON
}
});
this.ws.addEventListener('close', () => {
this.ws = null;
this.scheduleReconnect();
});
this.ws.addEventListener('error', () => {
this.broadcast({type: 'error', message: 'websocket error'});
this.ws = null;
this.scheduleReconnect();
});
}
scheduleReconnect() {
if (this.clients.length === 0 || this.reconnectTimer !== null) return;
this.reconnectDelay = Math.min(this.reconnectDelay * 2, RECONNECT_DELAY_MAX);
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
this.connect();
}, this.reconnectDelay);
}
register(port: MessagePort) {
if (this.clients.includes(port)) return;
this.clients.push(port);
port.postMessage({type: 'status', message: `registered to ${this.url}`});
}
deregister(port: MessagePort): number {
const idx = this.clients.indexOf(port);
if (idx >= 0) this.clients.splice(idx, 1);
return this.clients.length;
}
close() {
if (this.reconnectTimer !== null) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
}
this.ws?.close();
this.ws = null;
}
broadcast(msg: unknown) {
for (const port of this.clients) {
port.postMessage(msg);
}
}
}
const sourcesByUrl = new Map<string, WsSource | null>();
const sourcesByPort = new Map<MessagePort, WsSource | null>();
(self as unknown as SharedWorkerGlobalScope).addEventListener('connect', (e: MessageEvent) => {
for (const port of e.ports) {
port.addEventListener('message', (event: MessageEvent) => {
if (event.data.type === 'start') {
const {url} = event.data;
let source = sourcesByUrl.get(url);
if (source) {
source.register(port);
sourcesByPort.set(port, source);
return;
}
source = sourcesByPort.get(port);
if (source) {
const count = source.deregister(port);
if (count === 0) {
source.close();
sourcesByUrl.set(source.url, null);
}
}
source = new WsSource(url);
source.register(port);
sourcesByUrl.set(url, source);
sourcesByPort.set(port, source);
} else if (event.data.type === 'close') {
const source = sourcesByPort.get(port);
if (!source) return;
const count = source.deregister(port);
if (count === 0) {
source.close();
sourcesByUrl.set(source.url, null);
sourcesByPort.set(port, null);
}
} else if (event.data.type === 'status') {
const source = sourcesByPort.get(port);
if (!source) {
port.postMessage({type: 'status', message: 'not connected'});
return;
}
port.postMessage({
type: 'status',
message: `url: ${source.url} readyState: ${source.ws?.readyState ?? 'null'}`,
});
} else {
port.postMessage({
type: 'error',
message: `received but don't know how to handle: ${JSON.stringify(event.data)}`,
});
}
});
port.start();
}
});