frostfs-node/pkg/services/session/storage/temporary/storage.go
Evgenii Stratonikov f6c5222952
All checks were successful
Vulncheck / Vulncheck (push) Successful in 4m25s
Tests and linters / Run gofumpt (push) Successful in 4m33s
Build / Build Components (push) Successful in 5m14s
Pre-commit hooks / Pre-commit (push) Successful in 5m23s
Tests and linters / Staticcheck (push) Successful in 5m43s
Tests and linters / gopls check (push) Successful in 5m47s
Tests and linters / Tests (push) Successful in 6m0s
Tests and linters / Tests with -race (push) Successful in 6m6s
Tests and linters / Lint (push) Successful in 6m21s
[#1581] services/session: Use user.ID.EncodeToString() where possible
gopatch:
```
@@
var id expression
@@
-base58.Encode(id.WalletBytes())
+id.EncodeToString()
```

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-12-25 18:09:36 +00:00

61 lines
1.4 KiB
Go

package temporary
import (
"sync"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/session/storage"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
"github.com/mr-tron/base58"
)
type key struct {
// nolint:unused
tokenID string
// nolint:unused
ownerID string
}
// TokenStore is an in-memory session token store.
// It allows creating (storing), retrieving and
// expiring (removing) session tokens.
// Must be created only via calling NewTokenStore.
type TokenStore struct {
mtx sync.RWMutex
tokens map[key]*storage.PrivateToken
}
// NewTokenStore creates, initializes and returns a new TokenStore instance.
//
// The elements of the instance are stored in the map.
func NewTokenStore() *TokenStore {
return &TokenStore{
tokens: make(map[key]*storage.PrivateToken),
}
}
// Get returns private token corresponding to the given identifiers.
//
// Returns nil is there is no element in storage.
func (s *TokenStore) Get(ownerID user.ID, tokenID []byte) *storage.PrivateToken {
s.mtx.RLock()
t := s.tokens[key{
tokenID: base58.Encode(tokenID),
ownerID: ownerID.EncodeToString(),
}]
s.mtx.RUnlock()
return t
}
// RemoveOld removes all tokens expired since provided epoch.
func (s *TokenStore) RemoveOld(epoch uint64) {
s.mtx.Lock()
defer s.mtx.Unlock()
for k, tok := range s.tokens {
if tok.ExpiredAt() <= epoch {
delete(s.tokens, k)
}
}
}