set lock on read

This commit is contained in:
A.Unger
2020-07-30 10:10:52 +02:00
parent 4074565a6b
commit c432f296eb
2 changed files with 15 additions and 6 deletions

View File

@@ -32,8 +32,8 @@ import (
_ "github.com/tredoe/osutil/user/crypt/sha512_crypt"
)
// M synchronizes access to account files to readers and writers
var M sync.RWMutex
// accLock mutually exclude readers from writers on account files
var accLock sync.RWMutex
func (s Service) indexAccounts(path string) (err error) {
var f *os.File
@@ -77,6 +77,9 @@ var authQuery = regexp.MustCompile(`^login eq '(.*)' and password eq '(.*)'$`) /
func (s Service) loadAccount(id string, a *proto.Account) (err error) {
path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", id)
accLock.Lock()
defer accLock.Unlock()
var data []byte
if data, err = ioutil.ReadFile(path); err != nil {
return merrors.NotFound(s.id, "could not read account: %v", err.Error())
@@ -100,8 +103,8 @@ func (s Service) writeAccount(a *proto.Account) (err error) {
path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", a.Id)
M.Lock()
defer M.Unlock()
accLock.Lock()
defer accLock.Unlock()
if err = ioutil.WriteFile(path, bytes, 0600); err != nil {
return merrors.InternalServerError(s.id, "could not write account: %v", err.Error())
}

View File

@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/CiscoM31/godata"
"github.com/blevesearch/bleve"
@@ -35,6 +36,9 @@ func (s Service) indexGroups(path string) (err error) {
return
}
// accLock mutually exclude readers from writers on group files
var groupLock sync.RWMutex
func (s Service) indexGroup(id string) error {
g := &proto.BleveGroup{
BleveType: "group",
@@ -54,6 +58,8 @@ func (s Service) indexGroup(id string) error {
func (s Service) loadGroup(id string, g *proto.Group) (err error) {
path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", id)
groupLock.Lock()
defer groupLock.Unlock()
var data []byte
if data, err = ioutil.ReadFile(path); err != nil {
return merrors.NotFound(s.id, "could not read group: %v", err.Error())
@@ -78,8 +84,8 @@ func (s Service) writeGroup(g *proto.Group) (err error) {
path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", g.Id)
M.Lock()
defer M.Unlock()
groupLock.Lock()
defer groupLock.Unlock()
if err = ioutil.WriteFile(path, bytes, 0600); err != nil {
return merrors.InternalServerError(s.id, "could not write group: %v", err.Error())
}