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
|
## Todo/Doing
|
||||||
|
- Support for windows
|
||||||
|
- Better support for concurrency
|
||||||
- Better support for sub collections
|
- Better support for sub collections
|
||||||
- More methods to allow different types of reads/writes
|
- More methods to allow different types of reads/writes
|
||||||
- More tests (you can never have enough)
|
- 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
|
// a Driver is what is used to interact with the scribble database. It runs
|
||||||
// transactions, and provides log output
|
// transactions, and provides log output
|
||||||
Driver struct {
|
Driver struct {
|
||||||
|
mutex sync.Mutex
|
||||||
mutexes map[string]sync.Mutex
|
mutexes map[string]sync.Mutex
|
||||||
dir string // the directory where scribble will create the database
|
dir string // the directory where scribble will create the database
|
||||||
log hatchet.Logger // the logger scribble will log to
|
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
|
// is being modfied to avoid unsafe operations
|
||||||
func (d *Driver) getOrCreateMutex(collection string) sync.Mutex {
|
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 the mutex doesn't exist make it
|
||||||
if !ok {
|
if !ok {
|
||||||
c = sync.Mutex{}
|
m = sync.Mutex{}
|
||||||
d.mutexes[collection] = c
|
d.mutexes[collection] = m
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
Loading…
Reference in New Issue
Block a user