mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-11 12:37:13 -04:00
address review feedback on cache and session store
This commit is contained in:
@@ -42,7 +42,7 @@ func (s *SessionStore) RegisterToken(ctx context.Context, token string, expiresA
|
||||
key := usedTokenKeyPrefix + hashToken(token)
|
||||
created, err := s.cache.SetNX(ctx, key, usedTokenMarker, ttl)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to store used token entry: %w", err)
|
||||
return fmt.Errorf("store used token entry: %w", err)
|
||||
}
|
||||
if !created {
|
||||
return ErrTokenAlreadyUsed
|
||||
|
||||
@@ -64,12 +64,12 @@ func TestSessionStore_ConcurrentRegistrationAllowsOneCaller(t *testing.T) {
|
||||
case errors.Is(err, ErrTokenAlreadyUsed):
|
||||
alreadyUsed++
|
||||
default:
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, err, "concurrent registration returned an unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, succeeded)
|
||||
assert.Equal(t, attempts-1, alreadyUsed)
|
||||
assert.Equal(t, 1, succeeded, "exactly one concurrent caller should register the token")
|
||||
assert.Equal(t, attempts-1, alreadyUsed, "every other caller should be rejected as already used")
|
||||
}
|
||||
|
||||
func TestSessionStore_RegisterDifferentTokensAreIndependent(t *testing.T) {
|
||||
@@ -119,8 +119,8 @@ func TestSessionStore_CacheErrorIsReturned(t *testing.T) {
|
||||
s := NewSessionStore(failingTokenCache{err: cacheErr})
|
||||
|
||||
err := s.RegisterToken(context.Background(), "token", time.Now().Add(time.Hour))
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, cacheErr)
|
||||
require.Error(t, err, "cache failure should be surfaced to the caller")
|
||||
assert.ErrorIs(t, err, cacheErr, "cache error should be wrapped, not replaced")
|
||||
}
|
||||
|
||||
func TestHashToken_StableAndDoesNotLeak(t *testing.T) {
|
||||
|
||||
6
management/server/cache/memory.go
vendored
6
management/server/cache/memory.go
vendored
@@ -33,6 +33,12 @@ func (s *goCacheStore) SetNX(_ context.Context, key, value string, ttl time.Dura
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetDel reads the value under key and removes it. go-cache has no native read-and-delete
|
||||
// and releases its own lock between the two calls, so mu holds the pair together and no
|
||||
// value is consumed twice.
|
||||
//
|
||||
// Writes do not take mu: a Set landing mid-pair is lost, since GetDel returns the prior
|
||||
// value and deletes the new one. Callers must write a consumed key only once.
|
||||
func (s *goCacheStore) GetDel(_ context.Context, key string) (string, bool, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user