mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Add EventSource support (#11235)
If the browser supports EventSource switch to use this instead of polling notifications. Signed-off-by: Andrew Thornton art27@cantab.net
This commit is contained in:
112
routers/events/events.go
Normal file
112
routers/events/events.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package events
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/eventsource"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/routers/user"
|
||||
)
|
||||
|
||||
// Events listens for events
|
||||
func Events(ctx *context.Context) {
|
||||
// FIXME: Need to check if resp is actually a http.Flusher! - how though?
|
||||
|
||||
// Set the headers related to event streaming.
|
||||
ctx.Resp.Header().Set("Content-Type", "text/event-stream")
|
||||
ctx.Resp.Header().Set("Cache-Control", "no-cache")
|
||||
ctx.Resp.Header().Set("Connection", "keep-alive")
|
||||
ctx.Resp.Header().Set("X-Accel-Buffering", "no")
|
||||
ctx.Resp.WriteHeader(http.StatusOK)
|
||||
|
||||
// Listen to connection close and un-register messageChan
|
||||
notify := ctx.Req.Context().Done()
|
||||
ctx.Resp.Flush()
|
||||
|
||||
shutdownCtx := graceful.GetManager().ShutdownContext()
|
||||
|
||||
uid := ctx.User.ID
|
||||
|
||||
messageChan := eventsource.GetManager().Register(uid)
|
||||
|
||||
unregister := func() {
|
||||
eventsource.GetManager().Unregister(uid, messageChan)
|
||||
// ensure the messageChan is closed
|
||||
for {
|
||||
_, ok := <-messageChan
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := ctx.Resp.Write([]byte("\n")); err != nil {
|
||||
log.Error("Unable to write to EventStream: %v", err)
|
||||
unregister()
|
||||
return
|
||||
}
|
||||
|
||||
timer := time.NewTicker(30 * time.Second)
|
||||
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case <-timer.C:
|
||||
event := &eventsource.Event{
|
||||
Name: "ping",
|
||||
}
|
||||
_, err := event.WriteTo(ctx.Resp)
|
||||
if err != nil {
|
||||
log.Error("Unable to write to EventStream for user %s: %v", ctx.User.Name, err)
|
||||
go unregister()
|
||||
break loop
|
||||
}
|
||||
ctx.Resp.Flush()
|
||||
case <-notify:
|
||||
go unregister()
|
||||
break loop
|
||||
case <-shutdownCtx.Done():
|
||||
go unregister()
|
||||
break loop
|
||||
case event, ok := <-messageChan:
|
||||
if !ok {
|
||||
break loop
|
||||
}
|
||||
|
||||
// Handle logout
|
||||
if event.Name == "logout" {
|
||||
if ctx.Session.ID() == event.Data {
|
||||
_, _ = (&eventsource.Event{
|
||||
Name: "logout",
|
||||
Data: "here",
|
||||
}).WriteTo(ctx.Resp)
|
||||
ctx.Resp.Flush()
|
||||
go unregister()
|
||||
user.HandleSignOut(ctx)
|
||||
break loop
|
||||
}
|
||||
// Replace the event - we don't want to expose the session ID to the user
|
||||
event = (&eventsource.Event{
|
||||
Name: "logout",
|
||||
Data: "elsewhere",
|
||||
})
|
||||
}
|
||||
|
||||
_, err := event.WriteTo(ctx.Resp)
|
||||
if err != nil {
|
||||
log.Error("Unable to write to EventStream for user %s: %v", ctx.User.Name, err)
|
||||
go unregister()
|
||||
break loop
|
||||
}
|
||||
ctx.Resp.Flush()
|
||||
}
|
||||
}
|
||||
timer.Stop()
|
||||
}
|
@@ -15,6 +15,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/auth/sso"
|
||||
"code.gitea.io/gitea/modules/cache"
|
||||
"code.gitea.io/gitea/modules/cron"
|
||||
"code.gitea.io/gitea/modules/eventsource"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/highlight"
|
||||
code_indexer "code.gitea.io/gitea/modules/indexer/code"
|
||||
@@ -123,6 +124,7 @@ func GlobalInit(ctx context.Context) {
|
||||
if err := task.Init(); err != nil {
|
||||
log.Fatal("Failed to initialize task scheduler: %v", err)
|
||||
}
|
||||
eventsource.GetManager().Init()
|
||||
}
|
||||
if setting.EnableSQLite3 {
|
||||
log.Info("SQLite3 Supported")
|
||||
|
@@ -27,6 +27,7 @@ import (
|
||||
"code.gitea.io/gitea/routers/admin"
|
||||
apiv1 "code.gitea.io/gitea/routers/api/v1"
|
||||
"code.gitea.io/gitea/routers/dev"
|
||||
"code.gitea.io/gitea/routers/events"
|
||||
"code.gitea.io/gitea/routers/org"
|
||||
"code.gitea.io/gitea/routers/private"
|
||||
"code.gitea.io/gitea/routers/repo"
|
||||
@@ -340,6 +341,8 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||
})
|
||||
}, reqSignOut)
|
||||
|
||||
m.Any("/user/events", reqSignIn, events.Events)
|
||||
|
||||
m.Group("/login/oauth", func() {
|
||||
m.Get("/authorize", bindIgnErr(auth.AuthorizationForm{}), user.AuthorizeOAuth)
|
||||
m.Post("/grant", bindIgnErr(auth.GrantApplicationForm{}), user.GrantApplicationOAuth)
|
||||
|
@@ -16,6 +16,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/auth/oauth2"
|
||||
"code.gitea.io/gitea/modules/base"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/eventsource"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/password"
|
||||
"code.gitea.io/gitea/modules/recaptcha"
|
||||
@@ -991,7 +992,8 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
|
||||
ctx.Redirect(setting.AppSubURL + "/user/login")
|
||||
}
|
||||
|
||||
func handleSignOut(ctx *context.Context) {
|
||||
// HandleSignOut resets the session and sets the cookies
|
||||
func HandleSignOut(ctx *context.Context) {
|
||||
_ = ctx.Session.Delete("uid")
|
||||
_ = ctx.Session.Delete("uname")
|
||||
_ = ctx.Session.Delete("socialId")
|
||||
@@ -1006,7 +1008,13 @@ func handleSignOut(ctx *context.Context) {
|
||||
|
||||
// SignOut sign out from login status
|
||||
func SignOut(ctx *context.Context) {
|
||||
handleSignOut(ctx)
|
||||
if ctx.User != nil {
|
||||
eventsource.GetManager().SendMessageBlocking(ctx.User.ID, &eventsource.Event{
|
||||
Name: "logout",
|
||||
Data: ctx.Session.ID(),
|
||||
})
|
||||
}
|
||||
HandleSignOut(ctx)
|
||||
ctx.Redirect(setting.AppSubURL + "/")
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user