Merge pull request #713 from strukturag/session-expiration

Don't keep expiration timestamp in each session.
This commit is contained in:
Joachim Bauch 2024-04-22 15:19:16 +02:00 committed by GitHub
commit 952b8ae460
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 13 additions and 40 deletions

View file

@ -36,9 +36,6 @@ import (
)
var (
// Sessions expire 30 seconds after the connection closed.
sessionExpireDuration = 30 * time.Second
// Warn if a session has 32 or more pending messages.
warnPendingMessagesCount = 32
@ -68,8 +65,6 @@ type ClientSession struct {
backendUrl string
parsedBackendUrl *url.URL
expires time.Time
mu sync.Mutex
client *Client
@ -313,21 +308,6 @@ func (s *ClientSession) UserData() *json.RawMessage {
return s.userData
}
func (s *ClientSession) StartExpire() {
// The hub mutex must be held when calling this method.
s.expires = time.Now().Add(sessionExpireDuration)
s.hub.expiredSessions[s] = true
}
func (s *ClientSession) StopExpire() {
// The hub mutex must be held when calling this method.
delete(s.hub.expiredSessions, s)
}
func (s *ClientSession) IsExpired(now time.Time) bool {
return now.After(s.expires)
}
func (s *ClientSession) SetRoom(room *Room) {
s.room.Store(room)
if room != nil {

22
hub.go
View file

@ -78,6 +78,9 @@ var (
// Anonymous clients have to join a room after 10 seconds.
anonmyousJoinRoomTimeout = 10 * time.Second
// Sessions expire 30 seconds after the connection closed.
sessionExpireDuration = 30 * time.Second
// Run housekeeping jobs once per second
housekeepingInterval = time.Second
@ -148,7 +151,7 @@ type Hub struct {
allowSubscribeAnyStream bool
expiredSessions map[Session]bool
expiredSessions map[Session]time.Time
anonymousSessions map[*ClientSession]time.Time
expectHelloClients map[*Client]time.Time
dialoutSessions map[*ClientSession]bool
@ -336,7 +339,7 @@ func NewHub(config *goconf.ConfigFile, events AsyncEvents, rpcServer *GrpcServer
allowSubscribeAnyStream: allowSubscribeAnyStream,
expiredSessions: make(map[Session]bool),
expiredSessions: make(map[Session]time.Time),
anonymousSessions: make(map[*ClientSession]time.Time),
expectHelloClients: make(map[*Client]time.Time),
dialoutSessions: make(map[*ClientSession]bool),
@ -600,14 +603,14 @@ func (h *Hub) GetDialoutSession(roomId string, backend *Backend) *ClientSession
}
func (h *Hub) checkExpiredSessions(now time.Time) {
for s := range h.expiredSessions {
if s.IsExpired(now) {
for session, expires := range h.expiredSessions {
if now.After(expires) {
h.mu.Unlock()
log.Printf("Closing expired session %s (private=%s)", s.PublicId(), s.PrivateId())
s.Close()
log.Printf("Closing expired session %s (private=%s)", session.PublicId(), session.PrivateId())
session.Close()
h.mu.Lock()
// Should already be deleted by the close code, but better be sure.
delete(h.expiredSessions, s)
delete(h.expiredSessions, session)
}
}
}
@ -848,7 +851,8 @@ func (h *Hub) processUnregister(client *Client) *ClientSession {
delete(h.expectHelloClients, client)
if session != nil {
delete(h.clients, session.Data().Sid)
session.StartExpire()
now := time.Now()
h.expiredSessions[session] = now.Add(sessionExpireDuration)
}
h.mu.Unlock()
if session != nil {
@ -980,7 +984,7 @@ func (h *Hub) processHello(client *Client, message *ClientMessage) {
prev.SendByeResponseWithReason(nil, "session_resumed")
}
clientSession.StopExpire()
delete(h.expiredSessions, clientSession)
h.clients[data.Sid] = client
delete(h.expectHelloClients, client)
h.mu.Unlock()

View file

@ -26,7 +26,6 @@ import (
"errors"
"net/url"
"testing"
"time"
)
type DummySession struct {
@ -80,10 +79,6 @@ func (s *DummySession) LeaveRoom(notify bool) *Room {
return nil
}
func (s *DummySession) IsExpired(now time.Time) bool {
return false
}
func (s *DummySession) Close() {
}

View file

@ -69,7 +69,6 @@ type Session interface {
GetRoom() *Room
LeaveRoom(notify bool) *Room
IsExpired(now time.Time) bool
Close()
HasPermission(permission Permission) bool

View file

@ -27,7 +27,6 @@ import (
"log"
"net/url"
"sync/atomic"
"time"
)
const (
@ -160,10 +159,6 @@ func (s *VirtualSession) LeaveRoom(notify bool) *Room {
return room
}
func (s *VirtualSession) IsExpired(now time.Time) bool {
return false
}
func (s *VirtualSession) Close() {
s.CloseWithFeedback(nil, nil)
}