adding a mutex to getOrCreateMutex to avoid race conditions (per @chrhlnd pr)
This commit is contained in:
parent
9d6df7eff7
commit
a2e31af24c
@ -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)
|
||||
|
12
scribble.go
12
scribble.go
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user