Skip to content

Commit

Permalink
Return and log the GC crash error instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Dec 11, 2020
1 parent a19d7c0 commit e6d36a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ func New(cfg Config) *Session {
cfg.DecodeFunc = Base64Decode
}

if cfg.Logger == nil {
cfg.Logger = log.New(log.Writer(), "session-manager", log.Flags())
}

session := &Session{
config: cfg,
cookie: newCookie(),
log: cfg.Logger,
storePool: &sync.Pool{
New: func() interface{} {
return NewStore()
Expand Down Expand Up @@ -74,7 +79,7 @@ func (s *Session) startGC() {
case <-time.After(s.config.GCLifetime):
err := s.provider.GC()
if err != nil {
log.Printf("session GC crash: %v", err)
s.log.Printf("session GC crash: %v", err)
}
case <-s.stopGCChan:
return
Expand Down
7 changes: 4 additions & 3 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,19 @@ func TestSession_SetProvider(t *testing.T) {
}

func TestSession_startGC(t *testing.T) {
output := &bytes.Buffer{}
logger := log.New(output, "test", log.Flags())

s := New(Config{
GCLifetime: 100 * time.Millisecond,
Logger: logger,
})
provider := &mockProvider{
needGCValue: true,
errGC: errors.New("mock error"),
}
s.provider = provider

output := &bytes.Buffer{}
log.SetOutput(output)

go s.startGC()
time.Sleep(s.config.GCLifetime + 100*time.Millisecond)

Expand Down
9 changes: 9 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type Config struct {
// DecodeFunc session value unSerialize func
DecodeFunc func(dst *Dict, src []byte) error

// Logger
Logger Logger

// value cookie length
cookieLen uint32
}
Expand All @@ -67,6 +70,7 @@ type Session struct {
provider Provider
config Config
cookie *cookie
log Logger

storePool *sync.Pool
stopGCChan chan struct{}
Expand All @@ -87,6 +91,11 @@ type Store struct {

type cookie struct{}

type Logger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
}

// Provider interface implemented by providers
type Provider interface {
Get(id []byte) ([]byte, error)
Expand Down

0 comments on commit e6d36a8

Please sign in to comment.