1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-17 17:05:59 +00:00

set iris logger to the sessiondb/badger logs and update deps

Former-commit-id: 7578dec5752cc2bfa012440c24d59f41425812f8
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-05-17 22:08:43 +03:00
parent 1e20996330
commit 47c3bad58d
7 changed files with 38 additions and 36 deletions

View File

@@ -23,11 +23,6 @@ type (
// CookieSecureTLS set to true if server is running over TLS
// and you need the session's cookie "Secure" field to be set true.
//
// Note: The user should fill the Decode configuation field in order for this to work.
// Recommendation: You don't need this to be set to true, just fill the Encode and Decode fields
// with a third-party library like secure cookie, example is provided at the _examples folder.
//
// Defaults to false.
CookieSecureTLS bool

View File

@@ -10,10 +10,7 @@ type (
// provider contains the sessions and external databases (load and update).
// It's the session memory manager
provider struct {
// we don't use RWMutex because all actions have read and write at the same action function.
// (or write to a *Session's value which is race if we don't lock)
// narrow locks are fasters but are useless here.
mu sync.Mutex
mu sync.RWMutex
sessions map[string]*Session
db Database
destroyListeners []DestroyListener
@@ -22,10 +19,12 @@ type (
// newProvider returns a new sessions provider
func newProvider() *provider {
return &provider{
p := &provider{
sessions: make(map[string]*Session),
db: newMemDB(),
}
return p
}
// RegisterDatabase sets a session database.
@@ -41,8 +40,17 @@ func (p *provider) RegisterDatabase(db Database) {
// newSession returns a new session from sessionid
func (p *provider) newSession(man *Sessions, sid string, expires time.Duration) *Session {
sess := &Session{
sid: sid,
Man: man,
provider: p,
flashes: make(map[string]*flashMessage),
}
onExpire := func() {
p.Destroy(sid)
p.mu.Lock()
p.deleteSession(sess)
p.mu.Unlock()
}
lifetime := p.db.Acquire(sid, expires)
@@ -62,14 +70,7 @@ func (p *provider) newSession(man *Sessions, sid string, expires time.Duration)
lifetime.Begin(expires, onExpire)
}
sess := &Session{
sid: sid,
Man: man,
provider: p,
flashes: make(map[string]*flashMessage),
Lifetime: lifetime,
}
sess.Lifetime = lifetime
return sess
}
@@ -103,9 +104,9 @@ func (p *provider) UpdateExpiration(sid string, expires time.Duration) error {
return nil
}
p.mu.Lock()
p.mu.RLock()
sess, found := p.sessions[sid]
p.mu.Unlock()
p.mu.RUnlock()
if !found {
return ErrNotFound
}
@@ -116,14 +117,14 @@ func (p *provider) UpdateExpiration(sid string, expires time.Duration) error {
// Read returns the store which sid parameter belongs
func (p *provider) Read(man *Sessions, sid string, expires time.Duration) *Session {
p.mu.Lock()
p.mu.RLock()
if sess, found := p.sessions[sid]; found {
sess.runFlashGC() // run the flash messages GC, new request here of existing session
p.mu.Unlock()
p.mu.RUnlock()
return sess
}
p.mu.Unlock()
p.mu.RUnlock()
return p.Init(man, sid, expires) // if not found create new
}

View File

@@ -53,6 +53,7 @@ func New(directoryPath string) (*Database, error) {
}
opts := badger.DefaultOptions(directoryPath)
opts.Logger = golog.Default.Child("[sessionsdb.badger]").DisableNewLine()
service, err := badger.Open(opts)
if err != nil {

View File

@@ -11,13 +11,11 @@ func init() {
context.SetHandlerName("iris/sessions.*Handler", "iris.session")
}
// A Sessions manager should be responsible to Start a sesion, based
// on a Context, which should return
// a compatible Session interface, type. If the external session manager
// doesn't qualifies, then the user should code the rest of the functions with empty implementation.
//
// Sessions should be responsible to Destroy a session based
// on the Context.
// A Sessions manager should be responsible to Start/Get a sesion, based
// on a Context, which returns a *Session, type.
// It performs automatic memory cleanup on expired sessions.
// It can accept a `Database` for persistence across server restarts.
// A session can set temporarly values (flash messages).
type Sessions struct {
config Config
provider *provider