Skip to content

Commit

Permalink
Add InMemStoreOptions.Logger, so users can pass a custom logger or di…
Browse files Browse the repository at this point in the history
…sable session event logging
  • Loading branch information
icza committed Sep 10, 2018
1 parent e62c1f6 commit 5815dfd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
33 changes: 26 additions & 7 deletions inmem_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,35 @@ An in-memory session store implementation.
package session

import (
"fmt"
"io/ioutil"
"log"
"sync"
"time"
)

// In-memory session Store implementation.
type inMemStore struct {
sessions map[string]Session // Map of sessions (mapped from ID)
mux *sync.RWMutex // mutex to synchronize access to sessions
ticker *time.Ticker // Ticker for the session cleaner
closeTicker chan struct{} // Channel to signal close for the session cleaner
sessions map[string]Session // Map of sessions (mapped from ID)
mux *sync.RWMutex // mutex to synchronize access to sessions
ticker *time.Ticker // Ticker for the session cleaner
closeTicker chan struct{} // Channel to signal close for the session cleaner
logPrintln func(v ...interface{}) // Function used to log session lifecycle events (e.g. added, removed, timed out).
}

// NoopLogger that may be used as InMemStoreOptions.Logger to disable logging.
var NoopLogger = log.New(ioutil.Discard, "", 0)

// InMemStoreOptions defines options that may be passed when creating a new in-memory Store.
// All fields are optional; default value will be used for any field that has the zero value.
type InMemStoreOptions struct {
// Session cleaner check interval, default is 10 seconds.
SessCleanerInterval time.Duration

// Logger to log session lifecycle events (e.g. added, removed, timed out).
// Default is to use the global functions of the log package.
// To disable logging, you may use NoopLogger.
Logger *log.Logger
}

// Pointer to zero value of InMemStoreOptions to be reused for efficiency.
Expand All @@ -48,6 +59,14 @@ func NewInMemStoreOptions(o *InMemStoreOptions) Store {
closeTicker: make(chan struct{}),
}

output := log.Output
if o.Logger != nil {
output = o.Logger.Output
}
s.logPrintln = func(v ...interface{}) {
output(3, fmt.Sprintln(v...))
}

interval := o.SessCleanerInterval
if interval == 0 {
interval = 10 * time.Second
Expand Down Expand Up @@ -98,7 +117,7 @@ func (s *inMemStore) sessCleaner(interval time.Duration) {

for _, sess := range s.sessions {
if now.Sub(sess.Accessed()) > sess.Timeout() {
log.Println("Session timed out:", sess.ID())
s.logPrintln("Session timed out:", sess.ID())
delete(s.sessions, sess.ID())
}
}
Expand Down Expand Up @@ -126,7 +145,7 @@ func (s *inMemStore) Add(sess Session) {
s.mux.Lock()
defer s.mux.Unlock()

log.Println("Session added:", sess.ID())
s.logPrintln("Session added:", sess.ID())
s.sessions[sess.ID()] = sess
}

Expand All @@ -135,7 +154,7 @@ func (s *inMemStore) Remove(sess Session) {
s.mux.Lock()
defer s.mux.Unlock()

log.Println("Session removed:", sess.ID())
s.logPrintln("Session removed:", sess.ID())
delete(s.sessions, sess.ID())
}

Expand Down
7 changes: 6 additions & 1 deletion inmem_store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package session

import (
"log"
"os"
"testing"
"time"

Expand Down Expand Up @@ -28,7 +30,10 @@ func TestInMemStore(t *testing.T) {
func TestInMemStoreSessCleaner(t *testing.T) {
eq := mighty.Eq(t)

st := NewInMemStoreOptions(&InMemStoreOptions{SessCleanerInterval: 10 * time.Millisecond})
st := NewInMemStoreOptions(&InMemStoreOptions{
SessCleanerInterval: 10 * time.Millisecond,
Logger: log.New(os.Stderr, "test~", log.LstdFlags|log.Llongfile),
})
defer st.Close()

s := NewSessionOptions(&SessOptions{Timeout: 50 * time.Millisecond})
Expand Down

0 comments on commit 5815dfd

Please sign in to comment.