Skip to content

Commit

Permalink
Merge pull request #1053 from Fenny/master
Browse files Browse the repository at this point in the history
🩹 fix session storage
  • Loading branch information
Fenny authored Dec 3, 2020
2 parents d2b31e8 + c22107e commit c9df437
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// Version of current fiber package
const Version = "2.2.2"
const Version = "2.2.3"

// Handler defines a function to serve HTTP requests.
type Handler = func(*Ctx) error
Expand Down
8 changes: 2 additions & 6 deletions internal/storage/memory/memory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package memory

import (
"errors"
"sync"
"time"
)
Expand All @@ -14,9 +13,6 @@ type Storage struct {
done chan struct{}
}

// Common storage errors
var ErrNotExist = errors.New("key does not exist")

type entry struct {
data []byte
expiry int64
Expand All @@ -40,13 +36,13 @@ func New() *Storage {
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, ErrNotExist
return nil, nil
}
s.mux.RLock()
v, ok := s.db[key]
s.mux.RUnlock()
if !ok || v.expiry != 0 && v.expiry <= time.Now().Unix() {
return nil, ErrNotExist
return nil, nil
}

return v.data, nil
Expand Down
4 changes: 2 additions & 2 deletions middleware/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ func Test_Session_Cookie(t *testing.T) {
sess, _ := store.Get(ctx)
sess.Save()

// cookie should not be set if empty data
utils.AssertEqual(t, 0, len(ctx.Response().Header.PeekCookie(store.CookieName)))
// cookie should be set on Save ( even if empty data )
utils.AssertEqual(t, 84, len(ctx.Response().Header.PeekCookie(store.CookieName)))
}

// go test -v -run=^$ -bench=Benchmark_Session -benchmem -count=4
Expand Down
4 changes: 2 additions & 2 deletions middleware/session/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func (s *Store) Get(c *fiber.Ctx) (*Session, error) {
if !fresh {
raw, err := s.Storage.Get(id)
// Unmashal if we found data
if err == nil {
if raw != nil && err == nil {
mux.Lock()
gotiny.Unmarshal(raw, &sess.data)
mux.Unlock()
sess.fresh = false
} else if raw != nil && err.Error() != "key does not exist" {
} else if err != nil {
return nil, err
} else {
sess.fresh = true
Expand Down

0 comments on commit c9df437

Please sign in to comment.