frostfs-node/pkg/services/session/storage/temporary/storage.go
Leonard Lyubich 1c30414a6c [#1454] Upgrade NeoFS SDK Go module with new IDs
Core changes:
 * avoid package-colliding variable naming
 * avoid using pointers to IDs where unnecessary
 * avoid using `idSDK` import alias pattern
 * use `EncodeToString` for protocol string calculation and `String` for
  printing

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2022-06-01 17:41:45 +03:00

60 lines
1.3 KiB
Go

package temporary
import (
"sync"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
"github.com/nspcc-dev/neofs-sdk-go/user"
)
type key struct {
tokenID string
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{
mtx: new(sync.RWMutex),
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: base58.Encode(ownerID.WalletBytes()),
}]
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)
}
}
}