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
+7 -4
View File
@@ -32,8 +32,8 @@ import (
_ "github.com/tredoe/osutil/user/crypt/sha512_crypt" _ "github.com/tredoe/osutil/user/crypt/sha512_crypt"
) )
// M synchronizes access to account files to readers and writers // accLock mutually exclude readers from writers on account files
var M sync.RWMutex var accLock sync.RWMutex
func (s Service) indexAccounts(path string) (err error) { func (s Service) indexAccounts(path string) (err error) {
var f *os.File 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) { func (s Service) loadAccount(id string, a *proto.Account) (err error) {
path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", id) path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", id)
accLock.Lock()
defer accLock.Unlock()
var data []byte var data []byte
if data, err = ioutil.ReadFile(path); err != nil { if data, err = ioutil.ReadFile(path); err != nil {
return merrors.NotFound(s.id, "could not read account: %v", err.Error()) 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) path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", a.Id)
M.Lock() accLock.Lock()
defer M.Unlock() defer accLock.Unlock()
if err = ioutil.WriteFile(path, bytes, 0600); err != nil { if err = ioutil.WriteFile(path, bytes, 0600); err != nil {
return merrors.InternalServerError(s.id, "could not write account: %v", err.Error()) return merrors.InternalServerError(s.id, "could not write account: %v", err.Error())
} }
+8 -2
View File
@@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"github.com/CiscoM31/godata" "github.com/CiscoM31/godata"
"github.com/blevesearch/bleve" "github.com/blevesearch/bleve"
@@ -35,6 +36,9 @@ func (s Service) indexGroups(path string) (err error) {
return return
} }
// accLock mutually exclude readers from writers on group files
var groupLock sync.RWMutex
func (s Service) indexGroup(id string) error { func (s Service) indexGroup(id string) error {
g := &proto.BleveGroup{ g := &proto.BleveGroup{
BleveType: "group", BleveType: "group",
@@ -54,6 +58,8 @@ func (s Service) indexGroup(id string) error {
func (s Service) loadGroup(id string, g *proto.Group) (err error) { func (s Service) loadGroup(id string, g *proto.Group) (err error) {
path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", id) path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", id)
groupLock.Lock()
defer groupLock.Unlock()
var data []byte var data []byte
if data, err = ioutil.ReadFile(path); err != nil { if data, err = ioutil.ReadFile(path); err != nil {
return merrors.NotFound(s.id, "could not read group: %v", err.Error()) 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) path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", g.Id)
M.Lock() groupLock.Lock()
defer M.Unlock() defer groupLock.Unlock()
if err = ioutil.WriteFile(path, bytes, 0600); err != nil { if err = ioutil.WriteFile(path, bytes, 0600); err != nil {
return merrors.InternalServerError(s.id, "could not write group: %v", err.Error()) return merrors.InternalServerError(s.id, "could not write group: %v", err.Error())
} }