Minor session manager code cleanup and refactor (#822)

* Return a copy of all sessions for session manager Sessions method()
* Use standard mutex Lock method when iterating over stale sessions
* Use defer for consistency
This commit is contained in:
Dan Sosedoff
2025-11-16 13:12:28 -08:00
committed by GitHub
parent 74e8482f96
commit 06d73805a8

View File

@@ -43,18 +43,21 @@ func (m *SessionManager) IDs() []string {
func (m *SessionManager) Sessions() map[string]*client.Client {
m.mu.Lock()
sessions := m.sessions
m.mu.Unlock()
defer m.mu.Unlock()
sessions := make(map[string]*client.Client, len(m.sessions))
for k, v := range m.sessions {
sessions[k] = v
}
return sessions
}
func (m *SessionManager) Get(id string) *client.Client {
m.mu.Lock()
c := m.sessions[id]
m.mu.Unlock()
defer m.mu.Unlock()
return c
return m.sessions[id]
}
func (m *SessionManager) Add(id string, conn *client.Client) {
@@ -81,10 +84,9 @@ func (m *SessionManager) Remove(id string) bool {
func (m *SessionManager) Len() int {
m.mu.Lock()
sz := len(m.sessions)
m.mu.Unlock()
defer m.mu.Unlock()
return sz
return len(m.sessions)
}
func (m *SessionManager) Cleanup() int {
@@ -118,7 +120,7 @@ func (m *SessionManager) RunPeriodicCleanup() {
}
func (m *SessionManager) staleSessions() []string {
m.mu.TryLock()
m.mu.Lock()
defer m.mu.Unlock()
now := time.Now()