libs/db: close batch (#3397)

ClevelDB requires closing when WriteBatch is no longer needed, https://godoc.org/github.com/jmhodges/levigo#WriteBatch.Close

Fixes the memory leak in https://github.com/cosmos/cosmos-sdk/issues/3842
This commit is contained in:
Yumin Xia
2019-03-10 12:46:32 +04:00
committed by Anton Kaliaev
parent 90794260bc
commit b021f1e505
10 changed files with 32 additions and 0 deletions
+5
View File
@@ -187,6 +187,11 @@ func (mBatch *cLevelDBBatch) WriteSync() {
}
}
// Implements Batch.
func (mBatch *cLevelDBBatch) Close() {
mBatch.batch.Close()
}
//----------------------------------------
// Iterator
// NOTE This is almost identical to db/go_level_db.Iterator
+5
View File
@@ -250,3 +250,8 @@ func (dbch debugBatch) WriteSync() {
fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
dbch.bch.WriteSync()
}
// Implements Batch.
func (dbch debugBatch) Close() {
dbch.bch.Close()
}
+4
View File
@@ -184,6 +184,10 @@ func (mBatch *goLevelDBBatch) WriteSync() {
}
}
// Implements Batch.
// Close is no-op for goLevelDBBatch.
func (mBatch *goLevelDBBatch) Close() {}
//----------------------------------------
// Iterator
// NOTE This is almost identical to db/c_level_db.Iterator
+4
View File
@@ -46,6 +46,10 @@ func (mBatch *memBatch) WriteSync() {
mBatch.write(true)
}
func (mBatch *memBatch) Close() {
mBatch.ops = nil
}
func (mBatch *memBatch) write(doSync bool) {
if mtx := mBatch.db.Mutex(); mtx != nil {
mtx.Lock()
+4
View File
@@ -248,6 +248,10 @@ func (pb prefixBatch) WriteSync() {
pb.source.WriteSync()
}
func (pb prefixBatch) Close() {
pb.source.Close()
}
//----------------------------------------
// prefixIterator
+1
View File
@@ -180,6 +180,7 @@ func (s *server) BatchWriteSync(c context.Context, b *protodb.Batch) (*protodb.N
func (s *server) batchWrite(c context.Context, b *protodb.Batch, sync bool) (*protodb.Nothing, error) {
bat := s.db.NewBatch()
defer bat.Close()
for _, op := range b.Ops {
switch op.Type {
case protodb.Operation_SET:
+4
View File
@@ -260,3 +260,7 @@ func (bat *batch) WriteSync() {
panic(fmt.Sprintf("RemoteDB.BatchWriteSync: %v", err))
}
}
func (bat *batch) Close() {
bat.ops = nil
}
+2
View File
@@ -57,10 +57,12 @@ type DB interface {
//----------------------------------------
// Batch
// Batch Close must be called when the program no longer needs the object.
type Batch interface {
SetDeleter
Write()
WriteSync()
Close()
}
type SetDeleter interface {