Skip to content

Commit

Permalink
Use rwmutex in sessionData to allow concurrent readers
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-woods committed Apr 9, 2024
1 parent 7e11d57 commit 9b07998
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
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

0 comments on commit 9b07998

Please sign in to comment.