Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sync.RWMutex in sessionData to allow concurrent readers #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type sessionData struct {
status Status
token string
values map[string]interface{}
mu sync.Mutex
mu sync.RWMutex
}

func newSessionData(lifetime time.Duration) *sessionData {
Expand Down Expand Up @@ -93,8 +93,8 @@ func (s *SessionManager) Load(ctx context.Context, token string) (context.Contex
func (s *SessionManager) Commit(ctx context.Context) (string, time.Time, error) {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
defer sd.mu.Unlock()
sd.mu.RLock()
defer sd.mu.RUnlock()

if sd.token == "" {
var err error
Expand Down Expand Up @@ -175,8 +175,8 @@ func (s *SessionManager) Put(ctx context.Context, key string, val interface{}) {
func (s *SessionManager) Get(ctx context.Context, key string) interface{} {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
defer sd.mu.Unlock()
sd.mu.RLock()
defer sd.mu.RUnlock()

return sd.values[key]
}
Expand Down Expand Up @@ -243,9 +243,9 @@ func (s *SessionManager) Clear(ctx context.Context) error {
func (s *SessionManager) Exists(ctx context.Context, key string) bool {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
sd.mu.RLock()
_, exists := sd.values[key]
sd.mu.Unlock()
sd.mu.RUnlock()

return exists
}
Expand All @@ -256,14 +256,14 @@ func (s *SessionManager) Exists(ctx context.Context, key string) bool {
func (s *SessionManager) Keys(ctx context.Context) []string {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
sd.mu.RLock()
keys := make([]string, len(sd.values))
i := 0
for key := range sd.values {
keys[i] = key
i++
}
sd.mu.Unlock()
sd.mu.RUnlock()

sort.Strings(keys)
return keys
Expand Down Expand Up @@ -346,8 +346,8 @@ func (s *SessionManager) MergeSession(ctx context.Context, token string) error {
func (s *SessionManager) Status(ctx context.Context) Status {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
defer sd.mu.Unlock()
sd.mu.RLock()
defer sd.mu.RUnlock()

return sd.status
}
Expand Down Expand Up @@ -572,8 +572,8 @@ func (s *SessionManager) Iterate(ctx context.Context, fn func(context.Context) e
func (s *SessionManager) Deadline(ctx context.Context) time.Time {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
defer sd.mu.Unlock()
sd.mu.RLock()
defer sd.mu.RUnlock()

return sd.deadline
}
Expand All @@ -597,8 +597,8 @@ func (s *SessionManager) SetDeadline(ctx context.Context, expire time.Time) {
func (s *SessionManager) Token(ctx context.Context) string {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
defer sd.mu.Unlock()
sd.mu.RLock()
defer sd.mu.RUnlock()

return sd.token
}
Expand Down
7 changes: 0 additions & 7 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"reflect"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -213,7 +212,6 @@ func TestSessionManager_Load(T *testing.T) {
values: map[string]interface{}{
"blah": "blah",
},
mu: sync.Mutex{},
})

actualToken, actualExpiry, err := s.Commit(initialCtx)
Expand Down Expand Up @@ -260,7 +258,6 @@ func TestSessionManager_Commit(T *testing.T) {
values: map[string]interface{}{
"blah": "blah",
},
mu: sync.Mutex{},
})

actualToken, actualExpiry, err := s.Commit(ctx)
Expand Down Expand Up @@ -288,7 +285,6 @@ func TestSessionManager_Commit(T *testing.T) {
values: map[string]interface{}{
"blah": "blah",
},
mu: sync.Mutex{},
})

actualToken, actualExpiry, err := s.Commit(ctx)
Expand Down Expand Up @@ -316,7 +312,6 @@ func TestSessionManager_Commit(T *testing.T) {
values: map[string]interface{}{
"blah": "blah",
},
mu: sync.Mutex{},
})

actualToken, actualExpiry, err := s.Commit(ctx)
Expand Down Expand Up @@ -344,7 +339,6 @@ func TestSessionManager_Commit(T *testing.T) {
values: map[string]interface{}{
"blah": "blah",
},
mu: sync.Mutex{},
}
expectedBytes, err := s.Codec.Encode(sd.deadline, sd.values)
if err != nil {
Expand Down Expand Up @@ -379,7 +373,6 @@ func TestSessionManager_Commit(T *testing.T) {
values: map[string]interface{}{
"blah": "blah",
},
mu: sync.Mutex{},
})

actualToken, actualExpiry, err := s.Commit(ctx)
Expand Down