GODT-1482: Comment or mitigate panics, unlock cache when needed.

This commit is contained in:
Jakub
2021-12-21 11:34:21 +01:00
committed by Jakub Cuth
parent e9c05c5a6b
commit df601ecbbd
15 changed files with 312 additions and 51 deletions

View File

@ -28,7 +28,8 @@ type inMemoryCache struct {
size, limit int
}
// NewInMemoryCache creates a new in memory cache which stores up to the given number of bytes of cached data.
// NewInMemoryCache creates a new in memory cache which stores up to the given
// number of bytes of cached data.
// NOTE(GODT-1158): Make this threadsafe.
func NewInMemoryCache(limit int) Cache {
return &inMemoryCache{
@ -42,7 +43,7 @@ func (c *inMemoryCache) Unlock(userID string, passphrase []byte) error {
return nil
}
func (c *inMemoryCache) Delete(userID string) error {
func (c *inMemoryCache) Lock(userID string) {
c.lock.Lock()
defer c.lock.Unlock()
@ -51,23 +52,44 @@ func (c *inMemoryCache) Delete(userID string) error {
}
delete(c.data, userID)
}
func (c *inMemoryCache) Delete(userID string) error {
c.Lock(userID)
return nil
}
// Has returns whether the given message exists in the cache.
func (c *inMemoryCache) Has(userID, messageID string) bool {
if _, err := c.Get(userID, messageID); err != nil {
return false
c.lock.RLock()
defer c.lock.RUnlock()
if !c.isUserUnlocked(userID) {
// This might look counter intuitive but in order to be able to test
// "re-unlocking" mechanism we need to return true here.
//
// The situation is the same as it would happen for onDiskCache with
// locked user. Later during `Get` cache would return proper error
// `ErrCacheNeedsUnlock`. It is expected that store would then try to
// re-unlock.
//
// In order to do proper behaviour we should implement
// encryption for inMemoryCache.
return true
}
return true
_, ok := c.data[userID][messageID]
return ok
}
func (c *inMemoryCache) Get(userID, messageID string) ([]byte, error) {
c.lock.RLock()
defer c.lock.RUnlock()
if !c.isUserUnlocked(userID) {
return nil, ErrCacheNeedsUnlock
}
literal, ok := c.data[userID][messageID]
if !ok {
return nil, errors.New("no such message in cache")
@ -76,12 +98,23 @@ func (c *inMemoryCache) Get(userID, messageID string) ([]byte, error) {
return literal, nil
}
// NOTE(GODT-1158): What to actually do when memory limit is reached? Replace something existing? Return error? Drop silently?
// NOTE(GODT-1158): Pull in cache-rotating feature from old IMAP cache.
func (c *inMemoryCache) isUserUnlocked(userID string) bool {
_, ok := c.data[userID]
return ok
}
// Set saves the message literal to memory for further usage.
//
// NOTE(GODT-1158, GODT-1488): Once memory limit is reached we should do proper
// rotation based on usage frequency.
func (c *inMemoryCache) Set(userID, messageID string, literal []byte) error {
c.lock.Lock()
defer c.lock.Unlock()
if !c.isUserUnlocked(userID) {
return ErrCacheNeedsUnlock
}
if c.size+len(literal) > c.limit {
return nil
}
@ -96,6 +129,10 @@ func (c *inMemoryCache) Rem(userID, messageID string) error {
c.lock.Lock()
defer c.lock.Unlock()
if !c.isUserUnlocked(userID) {
return nil
}
c.size -= len(c.data[userID][messageID])
delete(c.data[userID], messageID)