Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Read Write lock on accounts operations #72

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/service/v0/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (
_ "github.com/tredoe/osutil/user/crypt/sha512_crypt"
)

// 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
if f, err = os.Open(path); err != nil {
Expand Down Expand Up @@ -74,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())
Expand All @@ -85,8 +91,6 @@ func (s Service) loadAccount(id string, a *proto.Account) (err error) {
return
}

var accountMutex sync.Mutex

func (s Service) writeAccount(a *proto.Account) (err error) {

// leave only the group id
Expand All @@ -99,8 +103,8 @@ func (s Service) writeAccount(a *proto.Account) (err error) {

path := filepath.Join(s.Config.Server.AccountsDataPath, "accounts", a.Id)

accountMutex.Lock()
defer accountMutex.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())
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/service/v0/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,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",
Expand All @@ -55,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())
Expand All @@ -67,8 +72,6 @@ func (s Service) loadGroup(id string, g *proto.Group) (err error) {
return
}

var groupMutex sync.Mutex

func (s Service) writeGroup(g *proto.Group) (err error) {

// leave only the member id
Expand All @@ -81,8 +84,8 @@ func (s Service) writeGroup(g *proto.Group) (err error) {

path := filepath.Join(s.Config.Server.AccountsDataPath, "groups", g.Id)

groupMutex.Lock()
defer groupMutex.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())
}
Expand Down