Fix race when opening DB for first time (#1691)
This commit is contained in:
parent
86dfcb819b
commit
aec27be9b4
@ -105,7 +105,6 @@ func (f *fileStore) getDB(database, table string) (*fileHandle, error) {
|
||||
}
|
||||
|
||||
k := key(database, table)
|
||||
|
||||
f.RLock()
|
||||
fd, ok := f.handles[k]
|
||||
f.RUnlock()
|
||||
@ -115,6 +114,13 @@ func (f *fileStore) getDB(database, table string) (*fileHandle, error) {
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
// double check locking
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
if fd, ok := f.handles[k]; ok {
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
// create a directory /tmp/micro
|
||||
dir := filepath.Join(DefaultDir, database)
|
||||
// create the database handle
|
||||
@ -125,18 +131,16 @@ func (f *fileStore) getDB(database, table string) (*fileHandle, error) {
|
||||
dbPath := filepath.Join(dir, fname)
|
||||
|
||||
// create new db handle
|
||||
// Bolt DB only allows one process to open the file R/W so make sure we're doing this under a lock
|
||||
db, err := bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f.Lock()
|
||||
fd = &fileHandle{
|
||||
key: k,
|
||||
db: db,
|
||||
}
|
||||
f.handles[k] = fd
|
||||
f.Unlock()
|
||||
|
||||
return fd, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user