-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4823 from oasisprotocol/kostko/fix/tm-db-cleanup
go/consensus/tendermint: Make sure DBs are only closed during cleanup
- Loading branch information
Showing
6 changed files
with
85 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go/consensus/tendermint: Make sure DBs are only closed during cleanup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package db | ||
|
||
import ( | ||
"sync" | ||
|
||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
// Closer manages closing of multiple Tendermint Core databases. | ||
type Closer struct { | ||
l sync.Mutex | ||
dbs []dbm.DB | ||
} | ||
|
||
// Close closes all the managed databases. | ||
func (c *Closer) Close() { | ||
c.l.Lock() | ||
defer c.l.Unlock() | ||
|
||
for _, db := range c.dbs { | ||
_ = db.Close() | ||
} | ||
} | ||
|
||
// NewCloser creates a new empty database closer. | ||
func NewCloser() *Closer { | ||
return &Closer{} | ||
} | ||
|
||
type dbWithCloser struct { | ||
dbm.DB | ||
} | ||
|
||
func (d *dbWithCloser) Close() error { | ||
// Do nothing unless explicitly closed via the closer. | ||
return nil | ||
} | ||
|
||
// WithCloser wraps a Tendermint Core database instance so that it can only be closed by the given | ||
// closer instance. Direct attempts to close the returned database instance will be ignored. | ||
func WithCloser(db dbm.DB, closer *Closer) dbm.DB { | ||
closer.l.Lock() | ||
defer closer.l.Unlock() | ||
|
||
closer.dbs = append(closer.dbs, db) | ||
|
||
return &dbWithCloser{db} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters