forked from TrueCloudLab/frostfs-node
[#1255] node/session: Create separate dir for in-memory storage
Move in-memory session storage to the separate directory of `storage`. It is done for future support of different kind of session storages. Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
2a69aaf976
commit
929c9851a6
7 changed files with 36 additions and 19 deletions
65
pkg/services/session/storage/temporary/storage.go
Normal file
65
pkg/services/session/storage/temporary/storage.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
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/owner"
|
||||
)
|
||||
|
||||
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 *owner.ID, tokenID []byte) *storage.PrivateToken {
|
||||
ownerBytes, err := ownerID.Marshal()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
s.mtx.RLock()
|
||||
t := s.tokens[key{
|
||||
tokenID: base58.Encode(tokenID),
|
||||
ownerID: base58.Encode(ownerBytes),
|
||||
}]
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue