frostfs-api-go/session/store.go

48 lines
911 B
Go
Raw Normal View History

2019-11-18 13:34:06 +00:00
package session
import (
"sync"
)
type mapTokenStore struct {
2019-11-18 13:34:06 +00:00
*sync.RWMutex
tokens map[TokenID]PrivateToken
2019-11-18 13:34:06 +00:00
}
// NewMapTokenStore creates new PrivateTokenStore instance.
//
// The elements of the instance are stored in the map.
func NewMapTokenStore() PrivateTokenStore {
return &mapTokenStore{
2019-11-18 13:34:06 +00:00
RWMutex: new(sync.RWMutex),
tokens: make(map[TokenID]PrivateToken),
2019-11-18 13:34:06 +00:00
}
}
// Store adds passed token to the map.
//
// Resulting error is always nil.
func (s *mapTokenStore) Store(id TokenID, token PrivateToken) error {
2019-11-18 13:34:06 +00:00
s.Lock()
s.tokens[id] = token
2019-11-18 13:34:06 +00:00
s.Unlock()
return nil
2019-11-18 13:34:06 +00:00
}
// Fetch returns the map element corresponding to the given key.
//
// Returns ErrPrivateTokenNotFound is there is no element in map.
func (s *mapTokenStore) Fetch(id TokenID) (PrivateToken, error) {
2019-11-18 13:34:06 +00:00
s.RLock()
defer s.RUnlock()
t, ok := s.tokens[id]
if !ok {
return nil, ErrPrivateTokenNotFound
}
2019-11-18 13:34:06 +00:00
return t, nil
2019-11-18 13:34:06 +00:00
}