prevent potential memory leaks

This commit is contained in:
Dan Laine 2020-06-22 16:35:42 -04:00
parent 2e13c33f23
commit fc15e3cfe6
4 changed files with 21 additions and 6 deletions

View File

@ -17,6 +17,10 @@ import (
"github.com/ava-labs/gecko/utils/hashing"
)
const (
minBatchSize = 32
)
// Database encrypts all values that are provided
type Database struct {
lock sync.RWMutex
@ -201,7 +205,7 @@ func (b *batch) Write() error {
// Reset resets the batch for reuse.
func (b *batch) Reset() {
b.writes = b.writes[:0]
b.writes = make([]keyValue, 0, minBatchSize)
b.Batch.Reset()
}

View File

@ -13,8 +13,11 @@ import (
"github.com/ava-labs/gecko/utils"
)
// DefaultSize is the default initial size of the memory database
const DefaultSize = 1 << 10
const (
// DefaultSize is the default initial size of the memory database
DefaultSize = 1 << 10
minBatchSize = 32
)
// Database is an ephemeral key-value store that implements the Database
// interface.
@ -191,7 +194,7 @@ func (b *batch) Write() error {
// Reset implements the Batch interface
func (b *batch) Reset() {
b.writes = b.writes[:0]
b.writes = make([]keyValue, 0, minBatchSize)
b.size = 0
}

View File

@ -12,6 +12,10 @@ import (
"github.com/ava-labs/gecko/utils/hashing"
)
const (
minBatchSize = 32
)
// Database partitions a database into a sub-database by prefixing all keys with
// a unique value.
type Database struct {
@ -199,7 +203,7 @@ func (b *batch) Write() error {
// Reset resets the batch for reuse.
func (b *batch) Reset() {
b.writes = b.writes[:0]
b.writes = make([]keyValue, 0, minBatchSize)
b.Batch.Reset()
}

View File

@ -14,6 +14,10 @@ import (
"github.com/ava-labs/gecko/utils"
)
const (
minBatchSize = 32
)
var (
errClosed = fmt.Sprintf("rpc error: code = Unknown desc = %s", database.ErrClosed)
errNotFound = fmt.Sprintf("rpc error: code = Unknown desc = %s", database.ErrNotFound)
@ -180,7 +184,7 @@ func (b *batch) Write() error {
}
func (b *batch) Reset() {
b.writes = b.writes[:0]
b.writes = make([]keyValue, 0, minBatchSize)
b.size = 0
}