From d0693e64c4b2b8159ec5873e03c044c93670eb65 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sat, 13 May 2023 18:49:11 +0000 Subject: [PATCH] feat: make sure of cache thread-safety --- distro/debian/cache.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/distro/debian/cache.go b/distro/debian/cache.go index 1821441..8ed3730 100644 --- a/distro/debian/cache.go +++ b/distro/debian/cache.go @@ -1,6 +1,8 @@ package debian import ( + "sync" + "github.com/rapidloop/skv" ) @@ -8,7 +10,12 @@ type Cache struct { store *skv.KVStore } +// cache is not thread-safe, so make sure there are only one user +var mu sync.Mutex + func NewCache(path string) (c *Cache, err error) { + mu.Lock() + c = &Cache{} c.store, err = skv.Open(path) return @@ -32,6 +39,8 @@ func (c Cache) GetVersions() (versions []string, err error) { return } -func (c Cache) Close() error { - return c.store.Close() +func (c Cache) Close() (err error) { + err = c.store.Close() + mu.Unlock() + return }