diff --git a/clientsession.go b/clientsession.go index 53e8e55..ff8a488 100644 --- a/clientsession.go +++ b/clientsession.go @@ -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 { diff --git a/hub.go b/hub.go index 301ac08..53f6ee4 100644 --- a/hub.go +++ b/hub.go @@ -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() diff --git a/roomsessions_test.go b/roomsessions_test.go index 3511ba7..5a8ffe0 100644 --- a/roomsessions_test.go +++ b/roomsessions_test.go @@ -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() { } diff --git a/session.go b/session.go index de04ca5..79286a3 100644 --- a/session.go +++ b/session.go @@ -69,7 +69,6 @@ type Session interface { GetRoom() *Room LeaveRoom(notify bool) *Room - IsExpired(now time.Time) bool Close() HasPermission(permission Permission) bool diff --git a/virtualsession.go b/virtualsession.go index f298265..6288396 100644 --- a/virtualsession.go +++ b/virtualsession.go @@ -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) }