adding a mutex to getOrCreateMutex to avoid race conditions (per @chrhlnd pr)

This commit is contained in:
Steve Domino 2015-10-20 11:06:33 -06:00
parent 9d6df7eff7
commit a2e31af24c
2 changed files with 10 additions and 4 deletions

View File

@ -56,6 +56,8 @@ Complete documentation is available on [godoc](http://godoc.org/github.com/nanob
## Todo/Doing
- Support for windows
- Better support for concurrency
- Better support for sub collections
- More methods to allow different types of reads/writes
- More tests (you can never have enough)

View File

@ -27,6 +27,7 @@ type (
// a Driver is what is used to interact with the scribble database. It runs
// transactions, and provides log output
Driver struct {
mutex sync.Mutex
mutexes map[string]sync.Mutex
dir string // the directory where scribble will create the database
log hatchet.Logger // the logger scribble will log to
@ -196,15 +197,18 @@ func stat(path string) (fi os.FileInfo, err error) {
// is being modfied to avoid unsafe operations
func (d *Driver) getOrCreateMutex(collection string) sync.Mutex {
c, ok := d.mutexes[collection]
d.mutex.Lock()
defer d.mutex.Unlock()
m, ok := d.mutexes[collection]
// if the mutex doesn't exist make it
if !ok {
c = sync.Mutex{}
d.mutexes[collection] = c
m = sync.Mutex{}
d.mutexes[collection] = m
}
return c
return m
}
// mkDir is a simple wrapper that attempts to make a directory at a specified