pkg/sorted/sqlkv: don't release gate until iterator is closed

Change-Id: I4c3f5406b89294c09fb1d64aaeffc3c581664acd
This commit is contained in:
Brad Fitzpatrick 2016-02-13 13:20:10 -08:00
parent b3ba9aa7cd
commit 8cf4f297eb
1 changed files with 11 additions and 8 deletions

View File

@ -204,12 +204,10 @@ func (kv *KeyValue) Wipe() error {
func (kv *KeyValue) Close() error { return kv.DB.Close() }
func (kv *KeyValue) Find(start, end string) sorted.Iterator {
var releaseGate func() // nil if unused
if kv.Gate != nil {
kv.Gate.Start()
// TODO(mpl): looks like sqlite considers the db locked until we've closed
// the iterator, so we can't do anything else until then. We should probably
// move that Unlock to the closing of the iterator. Investigating.
defer kv.Gate.Done()
releaseGate = kv.Gate.Done
}
var rows *sql.Rows
var err error
@ -224,9 +222,10 @@ func (kv *KeyValue) Find(start, end string) sorted.Iterator {
}
it := &iter{
kv: kv,
rows: rows,
closeCheck: leak.NewChecker(),
kv: kv,
rows: rows,
closeCheck: leak.NewChecker(),
releaseGate: releaseGate,
}
return it
}
@ -239,7 +238,8 @@ type iter struct {
end string // optional end bound
err error // accumulated error, returned at Close
closeCheck *leak.Checker
closeCheck *leak.Checker
releaseGate func() // if non-nil, called on Close
rows *sql.Rows // if non-nil, the rows we're reading from
@ -276,6 +276,9 @@ func (t *iter) Close() error {
t.rows.Close()
t.rows = nil
}
if t.releaseGate != nil {
t.releaseGate()
}
err := t.err
t.err = errClosed
return err