provider.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package sessions
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/kataras/iris/core/errors"
  6. )
  7. type (
  8. // provider contains the sessions and external databases (load and update).
  9. // It's the session memory manager
  10. provider struct {
  11. // we don't use RWMutex because all actions have read and write at the same action function.
  12. // (or write to a *Session's value which is race if we don't lock)
  13. // narrow locks are fasters but are useless here.
  14. mu sync.Mutex
  15. sessions map[string]*Session
  16. db Database
  17. destroyListeners []DestroyListener
  18. }
  19. )
  20. // newProvider returns a new sessions provider
  21. func newProvider() *provider {
  22. return &provider{
  23. sessions: make(map[string]*Session, 0),
  24. db: newMemDB(),
  25. }
  26. }
  27. // RegisterDatabase sets a session database.
  28. func (p *provider) RegisterDatabase(db Database) {
  29. p.mu.Lock() // for any case
  30. p.db = db
  31. p.mu.Unlock()
  32. }
  33. // newSession returns a new session from sessionid
  34. func (p *provider) newSession(sid string, expires time.Duration) *Session {
  35. onExpire := func() {
  36. p.Destroy(sid)
  37. }
  38. lifetime := p.db.Acquire(sid, expires)
  39. // simple and straight:
  40. if !lifetime.IsZero() {
  41. // if stored time is not zero
  42. // start a timer based on the stored time, if not expired.
  43. lifetime.Revive(onExpire)
  44. } else {
  45. // Remember: if db not exist or it has been expired
  46. // then the stored time will be zero(see loadSessionFromDB) and the values will be empty.
  47. //
  48. // Even if the database has an unlimited session (possible by a previous app run)
  49. // priority to the "expires" is given,
  50. // again if <=0 then it does nothing.
  51. lifetime.Begin(expires, onExpire)
  52. }
  53. sess := &Session{
  54. sid: sid,
  55. provider: p,
  56. flashes: make(map[string]*flashMessage),
  57. Lifetime: lifetime,
  58. }
  59. return sess
  60. }
  61. // Init creates the session and returns it
  62. func (p *provider) Init(sid string, expires time.Duration) *Session {
  63. newSession := p.newSession(sid, expires)
  64. p.mu.Lock()
  65. p.sessions[sid] = newSession
  66. p.mu.Unlock()
  67. return newSession
  68. }
  69. // ErrNotFound can be returned when calling `UpdateExpiration` on a non-existing or invalid session entry.
  70. // It can be matched directly, i.e: `isErrNotFound := sessions.ErrNotFound.Equal(err)`.
  71. var ErrNotFound = errors.New("not found")
  72. // UpdateExpiration resets the expiration of a session.
  73. // if expires > 0 then it will try to update the expiration and destroy task is delayed.
  74. // if expires <= 0 then it does nothing it returns nil, to destroy a session call the `Destroy` func instead.
  75. //
  76. // If the session is not found, it returns a `NotFound` error, this can only happen when you restart the server and you used the memory-based storage(default),
  77. // because the call of the provider's `UpdateExpiration` is always called when the client has a valid session cookie.
  78. //
  79. // If a backend database is used then it may return an `ErrNotImplemented` error if the underline database does not support this operation.
  80. func (p *provider) UpdateExpiration(sid string, expires time.Duration) error {
  81. if expires <= 0 {
  82. return nil
  83. }
  84. p.mu.Lock()
  85. sess, found := p.sessions[sid]
  86. p.mu.Unlock()
  87. if !found {
  88. return ErrNotFound
  89. }
  90. sess.Lifetime.Shift(expires)
  91. return p.db.OnUpdateExpiration(sid, expires)
  92. }
  93. // Read returns the store which sid parameter belongs
  94. func (p *provider) Read(sid string, expires time.Duration) *Session {
  95. p.mu.Lock()
  96. if sess, found := p.sessions[sid]; found {
  97. sess.runFlashGC() // run the flash messages GC, new request here of existing session
  98. p.mu.Unlock()
  99. return sess
  100. }
  101. p.mu.Unlock()
  102. return p.Init(sid, expires) // if not found create new
  103. }
  104. func (p *provider) registerDestroyListener(ln DestroyListener) {
  105. if ln == nil {
  106. return
  107. }
  108. p.destroyListeners = append(p.destroyListeners, ln)
  109. }
  110. func (p *provider) fireDestroy(sid string) {
  111. for _, ln := range p.destroyListeners {
  112. ln(sid)
  113. }
  114. }
  115. // Destroy destroys the session, removes all sessions and flash values,
  116. // the session itself and updates the registered session databases,
  117. // this called from sessionManager which removes the client's cookie also.
  118. func (p *provider) Destroy(sid string) {
  119. p.mu.Lock()
  120. if sess, found := p.sessions[sid]; found {
  121. p.deleteSession(sess)
  122. }
  123. p.mu.Unlock()
  124. }
  125. // DestroyAll removes all sessions
  126. // from the server-side memory (and database if registered).
  127. // Client's session cookie will still exist but it will be reseted on the next request.
  128. func (p *provider) DestroyAll() {
  129. p.mu.Lock()
  130. for _, sess := range p.sessions {
  131. p.deleteSession(sess)
  132. }
  133. p.mu.Unlock()
  134. }
  135. func (p *provider) deleteSession(sess *Session) {
  136. sid := sess.sid
  137. delete(p.sessions, sid)
  138. p.db.Release(sid)
  139. p.fireDestroy(sid)
  140. }