changed how the mutexes were handled, aka fixed some possible deadlocks
This commit is contained in:
parent
9a5e133820
commit
dec87937ca
47
scribble.go
47
scribble.go
|
@ -99,6 +99,7 @@ func (d *Driver) write(trans Transaction) error {
|
||||||
|
|
||||||
mutex := d.getOrCreateMutex(trans.Collection)
|
mutex := d.getOrCreateMutex(trans.Collection)
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
|
|
||||||
//
|
//
|
||||||
dir := d.dir + "/" + trans.Collection
|
dir := d.dir + "/" + trans.Collection
|
||||||
|
@ -123,11 +124,7 @@ func (d *Driver) write(trans Transaction) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// move final file into place
|
// move final file into place
|
||||||
err = os.Rename(tmpPath, finalPath)
|
return os.Rename(tmpPath, finalPath)
|
||||||
|
|
||||||
mutex.Unlock()
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read does the opposite operation as write. Reading a record from the database
|
// read does the opposite operation as write. Reading a record from the database
|
||||||
|
@ -144,11 +141,7 @@ func (d *Driver) read(trans Transaction) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarshal data into the transaction.Container
|
// unmarshal data into the transaction.Container
|
||||||
if err := json.Unmarshal(b, trans.Container); err != nil {
|
return json.Unmarshal(b, trans.Container)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// readAll does the same operation as read, reading all the records in the specified
|
// readAll does the same operation as read, reading all the records in the specified
|
||||||
|
@ -180,11 +173,7 @@ func (d *Driver) readAll(trans Transaction) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarhsal the read files as a comma delimeted byte array
|
// unmarhsal the read files as a comma delimeted byte array
|
||||||
if err := json.Unmarshal([]byte("["+strings.Join(f, ",")+"]"), trans.Container); err != nil {
|
return json.Unmarshal([]byte("["+strings.Join(f, ",")+"]"), trans.Container)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete locks that database and then proceeds to remove the record (specified by
|
// delete locks that database and then proceeds to remove the record (specified by
|
||||||
|
@ -193,18 +182,12 @@ func (d *Driver) delete(trans Transaction) error {
|
||||||
|
|
||||||
mutex := d.getOrCreateMutex(trans.Collection)
|
mutex := d.getOrCreateMutex(trans.Collection)
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
|
|
||||||
dir := d.dir + "/" + trans.Collection
|
dir := d.dir + "/" + trans.Collection
|
||||||
|
|
||||||
// remove record from database
|
// remove record from database
|
||||||
err := os.Remove(dir + "/" + trans.ResourceID)
|
return os.Remove(dir + "/" + trans.ResourceID)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex.Unlock()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
@ -217,8 +200,8 @@ func (d *Driver) getOrCreateMutex(collection string) sync.Mutex {
|
||||||
|
|
||||||
// if the mutex doesn't exist make it
|
// if the mutex doesn't exist make it
|
||||||
if !ok {
|
if !ok {
|
||||||
d.mutexes[collection] = sync.Mutex{}
|
c = sync.Mutex{}
|
||||||
return d.mutexes[collection]
|
d.mutexes[collection] = c
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
@ -226,17 +209,17 @@ func (d *Driver) getOrCreateMutex(collection string) sync.Mutex {
|
||||||
|
|
||||||
// mkDir is a simple wrapper that attempts to make a directory at a specified
|
// mkDir is a simple wrapper that attempts to make a directory at a specified
|
||||||
// location
|
// location
|
||||||
func mkDir(d string) error {
|
func mkDir(d string) (err error) {
|
||||||
|
|
||||||
//
|
//
|
||||||
dir, _ := os.Stat(d)
|
dir, _ := os.Stat(d)
|
||||||
|
|
||||||
if dir == nil {
|
switch {
|
||||||
err := os.MkdirAll(d, 0755)
|
case dir == nil:
|
||||||
if err != nil {
|
err = os.MkdirAll(d, 0755)
|
||||||
return err
|
case !dir.IsDir():
|
||||||
}
|
err = os.ErrInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue