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

View File

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

View File

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

View File

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