GODT-1431 Prevent watcher when not using disk on cache

- change: Rename Cacher -> MsgCachePool
- change: Do not run watcher when using memory cache
- add: Allow to cancel cacher jobs (added context)
- change: Fix behavior on context cancel (was causing no internet)
This commit is contained in:
Jakub
2021-11-22 15:12:15 +01:00
parent b82e2ca176
commit d7c5ace8e4
12 changed files with 81 additions and 58 deletions

View File

@ -18,6 +18,8 @@
package store
import (
"context"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/pkg/message"
"github.com/sirupsen/logrus"
@ -59,7 +61,7 @@ func (store *Store) UnlockCache(kr *crypto.KeyRing) error {
return err
}
store.cacher.start()
store.msgCachePool.start()
return nil
}
@ -112,7 +114,7 @@ func (store *Store) getCachedMessage(messageID string) ([]byte, error) {
return store.cache.Get(store.user.ID(), messageID)
}
job, done := store.newBuildJob(messageID, message.ForegroundPriority)
job, done := store.newBuildJob(context.Background(), messageID, message.ForegroundPriority)
defer done()
literal, err := job.GetResult()
@ -135,11 +137,11 @@ func (store *Store) IsCached(messageID string) bool {
// BuildAndCacheMessage builds the given message (with background priority) and puts it in the cache.
// It builds with background priority.
func (store *Store) BuildAndCacheMessage(messageID string) error {
func (store *Store) BuildAndCacheMessage(ctx context.Context, messageID string) error {
buildAndCacheJobs <- struct{}{}
defer func() { <-buildAndCacheJobs }()
job, done := store.newBuildJob(messageID, message.BackgroundPriority)
job, done := store.newBuildJob(ctx, messageID, message.BackgroundPriority)
defer done()
literal, err := job.GetResult()