sessions.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "container/list"
  4. "sync"
  5. "time"
  6. )
  7. const SessionTimeout = time.Minute * 30
  8. type Session struct {
  9. sessionId string
  10. accountId string
  11. updateTime time.Time
  12. }
  13. var sessionManager = new(SessionManager)
  14. type SessionManager struct {
  15. stopped bool
  16. sessions *list.List
  17. sessionLocker *sync.RWMutex
  18. sessionMap1 map[string]*Session
  19. sessionMap2 map[string]*Session
  20. }
  21. func (manager *SessionManager) Start() {
  22. if manager.sessionMap1 == nil {
  23. manager.sessionMap1 = make(map[string]*Session)
  24. }
  25. if manager.sessionMap2 == nil {
  26. manager.sessionMap2 = make(map[string]*Session)
  27. }
  28. if manager.sessions == nil {
  29. manager.sessions = list.New()
  30. }
  31. manager.sessionLocker = new(sync.RWMutex)
  32. manager.stopped = false
  33. go func() {
  34. for !manager.stopped {
  35. timeoutSessions := list.New()
  36. manager.sessionLocker.RLock()
  37. for e := manager.sessions.Front(); e != nil; e = e.Next() {
  38. if manager.stopped {
  39. break
  40. }
  41. session := e.Value.(*Session)
  42. now := time.Now()
  43. if now.Sub(session.updateTime) >= SessionTimeout {
  44. //Timeout
  45. timeoutSessions.PushBack(e)
  46. }
  47. }
  48. manager.sessionLocker.RUnlock()
  49. manager.sessionLocker.Lock()
  50. for e := timeoutSessions.Front(); e != nil; e = e.Next() {
  51. if manager.stopped {
  52. break
  53. }
  54. session := e.Value.(*Session)
  55. manager.sessions.Remove(e.Value.(*list.Element))
  56. manager.sessionMap1[session.sessionId] = nil
  57. manager.sessionMap2[session.accountId] = nil
  58. }
  59. manager.sessionLocker.Unlock()
  60. time.Sleep(1 * time.Second)
  61. }
  62. }()
  63. manager.stopped = false
  64. }
  65. func (manager *SessionManager) Stop() {
  66. manager.stopped = true
  67. }
  68. func (manager *SessionManager) ActivateSession(sessionId string) {
  69. session := manager.sessionMap1[sessionId]
  70. if session != nil {
  71. session.updateTime = time.Now()
  72. }
  73. }
  74. func (manager *SessionManager) AddSession(session *Session) {
  75. manager.sessionLocker.Lock()
  76. manager.sessionMap1[session.sessionId] = session
  77. manager.sessionMap2[session.accountId] = session
  78. manager.sessionLocker.Unlock()
  79. }
  80. func (manager *SessionManager) RemoveSessionBySessionId(sessionId string) {
  81. manager.sessionLocker.Lock()
  82. for e := manager.sessions.Front(); e != nil; e = e.Next() {
  83. if manager.stopped {
  84. break
  85. }
  86. session := e.Value.(*Session)
  87. if session.sessionId == sessionId {
  88. manager.sessions.Remove(e.Value.(*list.Element))
  89. manager.sessionMap1[session.sessionId] = nil
  90. manager.sessionMap2[session.accountId] = nil
  91. }
  92. }
  93. manager.sessionLocker.Unlock()
  94. }
  95. func (manager *SessionManager) RemoveSessionByAccountId(accountId string) {
  96. manager.sessionLocker.Lock()
  97. for e := manager.sessions.Front(); e != nil; e = e.Next() {
  98. if manager.stopped {
  99. break
  100. }
  101. session := e.Value.(*Session)
  102. if session.accountId == accountId {
  103. manager.sessions.Remove(e.Value.(*list.Element))
  104. manager.sessionMap1[session.sessionId] = nil
  105. manager.sessionMap2[session.accountId] = nil
  106. }
  107. }
  108. manager.sessionLocker.Unlock()
  109. }
  110. func (manager *SessionManager) GetSessionBySessionId(sessionId string) *Session {
  111. manager.sessionLocker.RLock()
  112. session := manager.sessionMap1[sessionId]
  113. manager.sessionLocker.RUnlock()
  114. return session
  115. }
  116. func (manager *SessionManager) GetSessionByAccountId(accountId string) *Session {
  117. manager.sessionLocker.RLock()
  118. session := manager.sessionMap2[accountId]
  119. manager.sessionLocker.RUnlock()
  120. return session
  121. }