Skip to content

Commit

Permalink
hold read-lock when iterating query results
Browse files Browse the repository at this point in the history
  • Loading branch information
willscott committed Feb 24, 2020
1 parent 8fa3173 commit b383d99
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ func NewDatastore(path string, opts *Options) (*Datastore, error) {
return nil, err
}

closeLk := sync.RWMutex{}
ds := Datastore{
accessor: &accessor{ldb: db, syncWrites: true, closeLk: &closeLk},
accessor: &accessor{ldb: db, syncWrites: true, closeLk: new(sync.RWMutex)},
DB: db,
path: path,
}
Expand Down Expand Up @@ -110,8 +109,6 @@ func (a *accessor) Has(key ds.Key) (exists bool, err error) {
}

func (a *accessor) GetSize(key ds.Key) (size int, err error) {
a.closeLk.RLock()
defer a.closeLk.RUnlock()
return ds.GetBackedSize(a, key)
}

Expand Down Expand Up @@ -151,6 +148,8 @@ func (a *accessor) Query(q dsq.Query) (dsq.Results, error) {
}
r := dsq.ResultsFromIterator(q, dsq.Iterator{
Next: func() (dsq.Result, bool) {
a.closeLk.RLock()
defer a.closeLk.RUnlock()
if !next() {
return dsq.Result{}, false
}
Expand Down

0 comments on commit b383d99

Please sign in to comment.