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 ( var (
// Sessions expire 30 seconds after the connection closed.
sessionExpireDuration = 30 * time.Second
// Warn if a session has 32 or more pending messages. // Warn if a session has 32 or more pending messages.
warnPendingMessagesCount = 32 warnPendingMessagesCount = 32
@ -68,8 +65,6 @@ type ClientSession struct {
backendUrl string backendUrl string
parsedBackendUrl *url.URL parsedBackendUrl *url.URL
expires time.Time
mu sync.Mutex mu sync.Mutex
client *Client client *Client
@ -313,21 +308,6 @@ func (s *ClientSession) UserData() *json.RawMessage {
return s.userData 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) { func (s *ClientSession) SetRoom(room *Room) {
s.room.Store(room) s.room.Store(room)
if room != nil { if room != nil {

22
hub.go
View file

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

View file

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

View file

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

View file

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