1
0
Fork 0
mirror of https://github.com/Luzifer/mondash.git synced 2024-12-22 20:11:18 +00:00

Prevent write-collisions on frequent writes

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-02-18 16:06:06 +01:00
parent b6ea6fab0f
commit 88580ab7db
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -5,6 +5,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"sync"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -12,6 +13,9 @@ import (
// FileStorage is a storage adapter storing the data into single local files // FileStorage is a storage adapter storing the data into single local files
type FileStorage struct { type FileStorage struct {
storagePath string storagePath string
dashLock map[string]*sync.RWMutex
dashLockLock *sync.Mutex
} }
// NewFileStorage instanciates a new FileStorage // NewFileStorage instanciates a new FileStorage
@ -25,11 +29,17 @@ func NewFileStorage(uri *url.URL) *FileStorage {
return &FileStorage{ return &FileStorage{
storagePath: uri.Path, storagePath: uri.Path,
dashLock: map[string]*sync.RWMutex{},
dashLockLock: new(sync.Mutex),
} }
} }
// Put writes the given data to FS // Put writes the given data to FS
func (f *FileStorage) Put(dashboardID string, data []byte) error { func (f *FileStorage) Put(dashboardID string, data []byte) error {
f.getLock(dashboardID).Lock()
defer f.getLock(dashboardID).Unlock()
err := ioutil.WriteFile(f.getFilePath(dashboardID), data, 0600) err := ioutil.WriteFile(f.getFilePath(dashboardID), data, 0600)
return err return err
@ -37,6 +47,9 @@ func (f *FileStorage) Put(dashboardID string, data []byte) error {
// Get loads the data for the given dashboard from FS // Get loads the data for the given dashboard from FS
func (f *FileStorage) Get(dashboardID string) ([]byte, error) { func (f *FileStorage) Get(dashboardID string) ([]byte, error) {
f.getLock(dashboardID).RLock()
defer f.getLock(dashboardID).RUnlock()
data, err := ioutil.ReadFile(f.getFilePath(dashboardID)) data, err := ioutil.ReadFile(f.getFilePath(dashboardID))
if err != nil { if err != nil {
return nil, DashboardNotFoundError{dashboardID} return nil, DashboardNotFoundError{dashboardID}
@ -47,6 +60,9 @@ func (f *FileStorage) Get(dashboardID string) ([]byte, error) {
// Delete deletes the given dashboard from FS // Delete deletes the given dashboard from FS
func (f *FileStorage) Delete(dashboardID string) error { func (f *FileStorage) Delete(dashboardID string) error {
f.getLock(dashboardID).Lock()
defer f.getLock(dashboardID).Unlock()
if exists, err := f.Exists(dashboardID); err != nil || !exists { if exists, err := f.Exists(dashboardID); err != nil || !exists {
if err != nil { if err != nil {
return err return err
@ -59,6 +75,9 @@ func (f *FileStorage) Delete(dashboardID string) error {
// Exists checks for the existence of the given dashboard // Exists checks for the existence of the given dashboard
func (f *FileStorage) Exists(dashboardID string) (bool, error) { func (f *FileStorage) Exists(dashboardID string) (bool, error) {
f.getLock(dashboardID).RLock()
defer f.getLock(dashboardID).RUnlock()
if _, err := os.Stat(f.getFilePath(dashboardID)); err != nil { if _, err := os.Stat(f.getFilePath(dashboardID)); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false, nil return false, nil
@ -71,3 +90,16 @@ func (f *FileStorage) Exists(dashboardID string) (bool, error) {
func (f *FileStorage) getFilePath(dashboardID string) string { func (f *FileStorage) getFilePath(dashboardID string) string {
return path.Join(f.storagePath, dashboardID+".txt") return path.Join(f.storagePath, dashboardID+".txt")
} }
func (f *FileStorage) getLock(dashboardID string) *sync.RWMutex {
f.dashLockLock.Lock()
defer f.dashLockLock.Unlock()
l, ok := f.dashLock[dashboardID]
if !ok {
l = new(sync.RWMutex)
f.dashLock[dashboardID] = l
}
return l
}