swarm/storage/localstore: simplify DB.Close

This commit is contained in:
Janos Guljas 2019-03-08 15:51:47 +01:00
parent d6c3930ab1
commit 11e62373cc
3 changed files with 4 additions and 13 deletions

View file

@ -136,7 +136,8 @@ func (db *DB) triggerGarbageCollection() {
}
// incGCSizeInBatch changes gcSize field value
// by change which can be negative.
// by change which can be negative. This function
// must be called under batchMu lock.
func (db *DB) incGCSizeInBatch(batch *leveldb.Batch, change int64) (err error) {
if change == 0 {
return nil

View file

@ -294,10 +294,7 @@ func TestDB_gcSize(t *testing.T) {
}
}
// DB.Close writes gc size to disk, so
// Instead calling Close, close the database
// without it.
if err := db.closeWithOptions(false); err != nil {
if err := db.Close(); err != nil {
t.Fatal(err)
}

View file

@ -312,21 +312,14 @@ func New(path string, baseKey []byte, o *Options) (db *DB, err error) {
// Close closes the underlying database.
func (db *DB) Close() (err error) {
return db.closeWithOptions(true)
}
// closeWithOptions provides a more control which part of closing
// is done for tests.
func (db *DB) closeWithOptions(writeGCSize bool) (err error) {
close(db.close)
db.updateGCWG.Wait()
// wait for gc worker to
// return before closing the shed
timeout := time.After(5 * time.Second)
select {
case <-db.collectGarbageWorkerDone:
case <-timeout:
case <-time.After(5 * time.Second):
log.Error("localstore: collect garbage worker did not return after db close")
}
return db.shed.Close()