session_manager.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package state
  2. import (
  3. "context"
  4. "log/slog"
  5. "sync"
  6. "github.com/mk6i/retro-aim-server/wire"
  7. )
  8. // InMemorySessionManager handles the lifecycle of a user session and provides
  9. // synchronized message relay between sessions in the session pool. An
  10. // InMemorySessionManager is safe for concurrent use by multiple goroutines.
  11. type InMemorySessionManager struct {
  12. store map[IdentScreenName]*Session
  13. mapMutex sync.RWMutex
  14. logger *slog.Logger
  15. }
  16. // NewInMemorySessionManager creates a new instance of InMemorySessionManager.
  17. func NewInMemorySessionManager(logger *slog.Logger) *InMemorySessionManager {
  18. return &InMemorySessionManager{
  19. logger: logger,
  20. store: make(map[IdentScreenName]*Session),
  21. }
  22. }
  23. // RelayToAll relays a message to all sessions in the session pool.
  24. func (s *InMemorySessionManager) RelayToAll(ctx context.Context, msg wire.SNACMessage) {
  25. s.mapMutex.RLock()
  26. defer s.mapMutex.RUnlock()
  27. for _, sess := range s.store {
  28. s.maybeRelayMessage(ctx, msg, sess)
  29. }
  30. }
  31. // RelayToAllExcept relays a message to all session in the pool except for one
  32. // particular session.
  33. func (s *InMemorySessionManager) RelayToAllExcept(ctx context.Context, except *Session, msg wire.SNACMessage) {
  34. s.mapMutex.RLock()
  35. defer s.mapMutex.RUnlock()
  36. for _, sess := range s.store {
  37. if sess == except {
  38. continue
  39. }
  40. s.maybeRelayMessage(ctx, msg, sess)
  41. }
  42. }
  43. // RelayToScreenName relays a message to a session with a matching screen name.
  44. func (s *InMemorySessionManager) RelayToScreenName(ctx context.Context, screenName IdentScreenName, msg wire.SNACMessage) {
  45. sess := s.RetrieveByScreenName(screenName)
  46. if sess == nil {
  47. s.logger.WarnContext(ctx, "can't send notification because user is not online", "recipient", screenName, "message", msg)
  48. return
  49. }
  50. s.maybeRelayMessage(ctx, msg, sess)
  51. }
  52. // RelayToScreenNames relays a message to sessions with matching screenNames.
  53. func (s *InMemorySessionManager) RelayToScreenNames(ctx context.Context, screenNames []IdentScreenName, msg wire.SNACMessage) {
  54. for _, sess := range s.retrieveByScreenNames(screenNames) {
  55. s.maybeRelayMessage(ctx, msg, sess)
  56. }
  57. }
  58. func (s *InMemorySessionManager) maybeRelayMessage(ctx context.Context, msg wire.SNACMessage, sess *Session) {
  59. switch sess.RelayMessage(msg) {
  60. case SessSendClosed:
  61. s.logger.WarnContext(ctx, "can't send notification because the user's session is closed", "recipient", sess.IdentScreenName(), "message", msg)
  62. case SessQueueFull:
  63. s.logger.WarnContext(ctx, "can't send notification because queue is full", "recipient", sess.IdentScreenName(), "message", msg)
  64. sess.Close()
  65. }
  66. }
  67. // AddSession adds a new session to the pool. It replaces an existing session
  68. // with a matching screen name, ensuring that each screen name is unique in the
  69. // pool.
  70. func (s *InMemorySessionManager) AddSession(displayScreenName DisplayScreenName) *Session {
  71. s.mapMutex.Lock()
  72. defer s.mapMutex.Unlock()
  73. identScreenName := NewIdentScreenName(string(displayScreenName))
  74. // Only allow one session at a time per screen name. A session may already
  75. // exist because:
  76. // 1) the user is signing on using an already logged-on screen name.
  77. // 2) the session might be orphaned due to an undetected client
  78. // disconnection.
  79. for _, sess := range s.store {
  80. if identScreenName == sess.IdentScreenName() {
  81. sess.Close()
  82. delete(s.store, identScreenName)
  83. break
  84. }
  85. }
  86. sess := NewSession()
  87. sess.SetIdentScreenName(identScreenName)
  88. sess.SetDisplayScreenName(displayScreenName)
  89. s.store[identScreenName] = sess
  90. return sess
  91. }
  92. // RemoveSession takes a session out of the session pool.
  93. func (s *InMemorySessionManager) RemoveSession(sess *Session) {
  94. s.mapMutex.Lock()
  95. defer s.mapMutex.Unlock()
  96. delete(s.store, sess.IdentScreenName())
  97. }
  98. // RetrieveSession finds a session with a matching sessionID. Returns nil if
  99. // session is not found.
  100. func (s *InMemorySessionManager) RetrieveSession(screenName IdentScreenName) *Session {
  101. s.mapMutex.RLock()
  102. defer s.mapMutex.RUnlock()
  103. return s.store[screenName]
  104. }
  105. // RetrieveByScreenName find a session with a matching screen name. Returns nil
  106. // if session is not found.
  107. func (s *InMemorySessionManager) RetrieveByScreenName(screenName IdentScreenName) *Session {
  108. s.mapMutex.RLock()
  109. defer s.mapMutex.RUnlock()
  110. for _, sess := range s.store {
  111. if screenName == sess.IdentScreenName() {
  112. return sess
  113. }
  114. }
  115. return nil
  116. }
  117. func (s *InMemorySessionManager) retrieveByScreenNames(screenNames []IdentScreenName) []*Session {
  118. s.mapMutex.RLock()
  119. defer s.mapMutex.RUnlock()
  120. var ret []*Session
  121. for _, sn := range screenNames {
  122. for _, sess := range s.store {
  123. if sn == sess.IdentScreenName() {
  124. ret = append(ret, sess)
  125. }
  126. }
  127. }
  128. return ret
  129. }
  130. // Empty returns true if the session pool contains 0 sessions.
  131. func (s *InMemorySessionManager) Empty() bool {
  132. s.mapMutex.RLock()
  133. defer s.mapMutex.RUnlock()
  134. return len(s.store) == 0
  135. }
  136. // AllSessions returns all sessions in the session pool.
  137. func (s *InMemorySessionManager) AllSessions() []*Session {
  138. s.mapMutex.RLock()
  139. defer s.mapMutex.RUnlock()
  140. var sessions []*Session
  141. for _, sess := range s.store {
  142. sessions = append(sessions, sess)
  143. }
  144. return sessions
  145. }