session.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package session
  2. import (
  3. "math/rand"
  4. "time"
  5. ms "github.com/go-macaron/session"
  6. _ "github.com/go-macaron/session/memcache"
  7. _ "github.com/go-macaron/session/postgres"
  8. _ "github.com/go-macaron/session/redis"
  9. "github.com/grafana/grafana/pkg/log"
  10. "gopkg.in/macaron.v1"
  11. )
  12. const (
  13. SESS_KEY_USERID = "uid"
  14. SESS_KEY_LASTLDAPSYNC = "last_ldap_sync"
  15. )
  16. var sessionManager *ms.Manager
  17. var sessionOptions *ms.Options
  18. var StartSessionGC func() = func() {}
  19. var GetSessionCount func() int
  20. var sessionLogger = log.New("session")
  21. var sessionConnMaxLifetime int64
  22. func init() {
  23. StartSessionGC = func() {
  24. sessionManager.GC()
  25. sessionLogger.Debug("Session GC")
  26. time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, StartSessionGC)
  27. }
  28. GetSessionCount = func() int {
  29. return sessionManager.Count()
  30. }
  31. }
  32. func Init(options *ms.Options, connMaxLifetime int64) {
  33. var err error
  34. sessionOptions = prepareOptions(options)
  35. sessionConnMaxLifetime = connMaxLifetime
  36. sessionManager, err = ms.NewManager(options.Provider, *options)
  37. if err != nil {
  38. panic(err)
  39. }
  40. // start GC threads after some random seconds
  41. rndSeconds := 10 + rand.Int63n(180)
  42. time.AfterFunc(time.Duration(rndSeconds)*time.Second, StartSessionGC)
  43. }
  44. func prepareOptions(opt *ms.Options) *ms.Options {
  45. if len(opt.Provider) == 0 {
  46. opt.Provider = "memory"
  47. }
  48. if len(opt.ProviderConfig) == 0 {
  49. opt.ProviderConfig = "data/sessions"
  50. }
  51. if len(opt.CookieName) == 0 {
  52. opt.CookieName = "grafana_sess"
  53. }
  54. if len(opt.CookiePath) == 0 {
  55. opt.CookiePath = "/"
  56. }
  57. if opt.Gclifetime == 0 {
  58. opt.Gclifetime = 3600
  59. }
  60. if opt.Maxlifetime == 0 {
  61. opt.Maxlifetime = opt.Gclifetime
  62. }
  63. if opt.IDLength == 0 {
  64. opt.IDLength = 16
  65. }
  66. return opt
  67. }
  68. func GetSession() SessionStore {
  69. return &SessionWrapper{manager: sessionManager}
  70. }
  71. type SessionStore interface {
  72. // Set sets value to given key in session.
  73. Set(interface{}, interface{}) error
  74. // Get gets value by given key in session.
  75. Get(interface{}) interface{}
  76. // Delete deletes a key from session.
  77. Delete(interface{}) interface{}
  78. // ID returns current session ID.
  79. ID() string
  80. // Release releases session resource and save data to provider.
  81. Release() error
  82. // Destory deletes a session.
  83. Destory(*macaron.Context) error
  84. // init
  85. Start(*macaron.Context) error
  86. // RegenerateId regenerates the session id
  87. RegenerateId(*macaron.Context) error
  88. }
  89. type SessionWrapper struct {
  90. session ms.RawStore
  91. manager *ms.Manager
  92. }
  93. func (s *SessionWrapper) Start(c *macaron.Context) error {
  94. // See https://github.com/grafana/grafana/issues/11155 for details on why
  95. // a recover and retry is needed
  96. defer func() error {
  97. if err := recover(); err != nil {
  98. var retryErr error
  99. s.session, retryErr = s.manager.Start(c)
  100. return retryErr
  101. }
  102. return nil
  103. }()
  104. var err error
  105. s.session, err = s.manager.Start(c)
  106. return err
  107. }
  108. func (s *SessionWrapper) RegenerateId(c *macaron.Context) error {
  109. var err error
  110. s.session, err = s.manager.RegenerateId(c)
  111. return err
  112. }
  113. func (s *SessionWrapper) Set(k interface{}, v interface{}) error {
  114. if s.session != nil {
  115. return s.session.Set(k, v)
  116. }
  117. return nil
  118. }
  119. func (s *SessionWrapper) Get(k interface{}) interface{} {
  120. if s.session != nil {
  121. return s.session.Get(k)
  122. }
  123. return nil
  124. }
  125. func (s *SessionWrapper) Delete(k interface{}) interface{} {
  126. if s.session != nil {
  127. return s.session.Delete(k)
  128. }
  129. return nil
  130. }
  131. func (s *SessionWrapper) ID() string {
  132. if s.session != nil {
  133. return s.session.ID()
  134. }
  135. return ""
  136. }
  137. func (s *SessionWrapper) Release() error {
  138. if s.session != nil {
  139. return s.session.Release()
  140. }
  141. return nil
  142. }
  143. func (s *SessionWrapper) Destory(c *macaron.Context) error {
  144. if s.session != nil {
  145. if err := s.manager.Destory(c); err != nil {
  146. return err
  147. }
  148. s.session = nil
  149. }
  150. return nil
  151. }