Browse Source

提交代码。

Signed-off-by: zouap <zouap@pcl.ac.cn>
tags/v1.22.3.2^2
zouap 3 years ago
parent
commit
8c0472b961
1 changed files with 73 additions and 78 deletions
  1. +73
    -78
      routers/events/events.go

+ 73
- 78
routers/events/events.go View File

@@ -6,13 +6,8 @@ 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
@@ -27,86 +22,86 @@ func Events(ctx *context.Context) {
ctx.Resp.WriteHeader(http.StatusOK)

// Listen to connection close and un-register messageChan
notify := ctx.Req.Context().Done()
ctx.Resp.Flush()
// notify := ctx.Req.Context().Done()
// ctx.Resp.Flush()

shutdownCtx := graceful.GetManager().ShutdownContext()
// shutdownCtx := graceful.GetManager().ShutdownContext()

uid := ctx.User.ID
// uid := ctx.User.ID

messageChan := eventsource.GetManager().Register(uid)
// messageChan := eventsource.GetManager().Register(uid)

unregister := func() {
eventsource.GetManager().Unregister(uid, messageChan)
// ensure the messageChan is closed
for {
_, ok := <-messageChan
if !ok {
break
}
}
}
// 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
}
// 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)
// 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
}
// 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",
})
}
// // 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()
// _, 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()
}

Loading…
Cancel
Save