Skip to content

Commit

Permalink
defer mutex unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain committed Jan 31, 2017
1 parent 26fe8b8 commit b78f3d2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/vfs/download_store.go
Expand Up @@ -54,18 +54,19 @@ func cleanStore(s *memStore) {

func cleanDownloadStore() {
storeStoreMutex.Lock()
defer storeStoreMutex.Unlock()
for i, s := range storeStore {
cleanStore(s)
if len(s.Files) == 0 && len(s.Archives) == 0 {
delete(storeStore, i)
}
}
storeStoreMutex.Unlock()
}

// GetStore returns the DownloadStore for the given Instance
func GetStore(domain string) DownloadStore {
storeStoreMutex.Lock()
defer storeStoreMutex.Unlock()
if storeStore == nil {
storeStore = make(map[string]*memStore)
}
Expand All @@ -78,7 +79,6 @@ func GetStore(domain string) DownloadStore {
}
storeStore[domain] = store
}
storeStoreMutex.Unlock()
return store
}

Expand All @@ -98,25 +98,26 @@ func (s *memStore) AddFile(f string) (string, error) {
ExpiresAt: time.Now().Add(downloadStoreTTL),
}
s.Mutex.Lock()
defer s.Mutex.Unlock()
cleanStore(s)
key := s.makeSecret()
s.Files[key] = fref
s.Mutex.Unlock()
return key, nil
}

func (s *memStore) AddArchive(a *Archive) (string, error) {
a.ExpiresAt = time.Now().Add(downloadStoreTTL)
s.Mutex.Lock()
defer s.Mutex.Unlock()
cleanStore(s)
key := s.makeSecret()
s.Archives[key] = a
s.Mutex.Unlock()
return key, nil
}

func (s *memStore) GetFile(k string) (string, error) {
s.Mutex.Lock()
defer s.Mutex.Unlock()
f, ok := s.Files[k]
if !ok {
return "", nil
Expand All @@ -125,12 +126,12 @@ func (s *memStore) GetFile(k string) (string, error) {
delete(s.Files, k)
return "", nil
}
s.Mutex.Unlock()
return f.Path, nil
}

func (s *memStore) GetArchive(k string) (*Archive, error) {
s.Mutex.Lock()
defer s.Mutex.Unlock()
a, ok := s.Archives[k]
if !ok {
return nil, nil
Expand All @@ -139,6 +140,5 @@ func (s *memStore) GetArchive(k string) (*Archive, error) {
delete(s.Files, k)
return nil, nil
}
s.Mutex.Unlock()
return a, nil
}

0 comments on commit b78f3d2

Please sign in to comment.