Browse Source

add nopTxCache (Nil Object Pattern)

to better handle zero cache size
pull/1764/head
Anton Kaliaev 7 years ago
parent
commit
4f5492c831
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 34 additions and 18 deletions
  1. +33
    -17
      mempool/mempool.go
  2. +1
    -1
      mempool/reactor.go

+ 33
- 17
mempool/mempool.go View File

@ -77,7 +77,7 @@ type Mempool struct {
// Keep a cache of already-seen txs. // Keep a cache of already-seen txs.
// This reduces the pressure on the proxyApp. // This reduces the pressure on the proxyApp.
cache *txCache
cache txCache
// A log of mempool txs // A log of mempool txs
wal *auto.AutoFile wal *auto.AutoFile
@ -97,7 +97,11 @@ func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, he
recheckCursor: nil, recheckCursor: nil,
recheckEnd: nil, recheckEnd: nil,
logger: log.NewNopLogger(), logger: log.NewNopLogger(),
cache: newTxCache(config.CacheSize),
}
if config.CacheSize > 0 {
mempool.cache = newMapTxCache(config.CacheSize)
} else {
mempool.cache = nopTxCache{}
} }
proxyAppConn.SetResponseCallback(mempool.resCb) proxyAppConn.SetResponseCallback(mempool.resCb)
return mempool return mempool
@ -448,41 +452,45 @@ func (memTx *mempoolTx) Height() int64 {
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// txCache maintains a cache of transactions.
type txCache struct {
type txCache interface {
Reset()
Push(tx types.Tx) bool
Remove(tx types.Tx)
}
// mapTxCache maintains a cache of transactions.
type mapTxCache struct {
mtx sync.Mutex mtx sync.Mutex
size int size int
map_ map[string]struct{} map_ map[string]struct{}
list *list.List // to remove oldest tx when cache gets too big list *list.List // to remove oldest tx when cache gets too big
} }
// newTxCache returns a new txCache.
func newTxCache(cacheSize int) *txCache {
return &txCache{
var _ txCache = (*mapTxCache)(nil)
// newMapTxCache returns a new mapTxCache.
func newMapTxCache(cacheSize int) *mapTxCache {
return &mapTxCache{
size: cacheSize, size: cacheSize,
map_: make(map[string]struct{}, cacheSize), map_: make(map[string]struct{}, cacheSize),
list: list.New(), list: list.New(),
} }
} }
// Reset resets the txCache to empty.
func (cache *txCache) Reset() {
// Reset resets the cache to an empty state.
func (cache *mapTxCache) Reset() {
cache.mtx.Lock() cache.mtx.Lock()
cache.map_ = make(map[string]struct{}, cache.size) cache.map_ = make(map[string]struct{}, cache.size)
cache.list.Init() cache.list.Init()
cache.mtx.Unlock() cache.mtx.Unlock()
} }
// Push adds the given tx to the txCache. It returns false if tx is already in the cache.
func (cache *txCache) Push(tx types.Tx) bool {
// Push adds the given tx to the cache and returns true. It returns false if tx
// is already in the cache.
func (cache *mapTxCache) Push(tx types.Tx) bool {
cache.mtx.Lock() cache.mtx.Lock()
defer cache.mtx.Unlock() defer cache.mtx.Unlock()
// if cache size is 0, do nothing
if cache.size == 0 {
return true
}
if _, exists := cache.map_[string(tx)]; exists { if _, exists := cache.map_[string(tx)]; exists {
return false return false
} }
@ -501,8 +509,16 @@ func (cache *txCache) Push(tx types.Tx) bool {
} }
// Remove removes the given tx from the cache. // Remove removes the given tx from the cache.
func (cache *txCache) Remove(tx types.Tx) {
func (cache *mapTxCache) Remove(tx types.Tx) {
cache.mtx.Lock() cache.mtx.Lock()
delete(cache.map_, string(tx)) delete(cache.map_, string(tx))
cache.mtx.Unlock() cache.mtx.Unlock()
} }
type nopTxCache struct{}
var _ txCache = (*nopTxCache)(nil)
func (nopTxCache) Reset() {}
func (nopTxCache) Push(types.Tx) bool { return true }
func (nopTxCache) Remove(types.Tx) {}

+ 1
- 1
mempool/reactor.go View File

@ -6,7 +6,7 @@ import (
"time" "time"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tmlibs/clist" "github.com/tendermint/tmlibs/clist"
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"


Loading…
Cancel
Save