fixes from review; use mempool.TxsAvailable() directly

This commit is contained in:
Ethan Buchman
2017-07-28 23:42:43 -04:00
parent cf3abe5096
commit 3444bee47f
7 changed files with 51 additions and 72 deletions
+22 -26
View File
@@ -56,14 +56,16 @@ const cacheSize = 100000
type Mempool struct {
config *cfg.MempoolConfig
proxyMtx sync.Mutex
proxyAppConn proxy.AppConnMempool
txs *clist.CList // concurrent linked-list of good txs
counter int64 // simple incrementing counter
height int // the last block Update()'d to
rechecking int32 // for re-checking filtered txs on Update()
recheckCursor *clist.CElement // next expected response
recheckEnd *clist.CElement // re-checking stops here
proxyMtx sync.Mutex
proxyAppConn proxy.AppConnMempool
txs *clist.CList // concurrent linked-list of good txs
counter int64 // simple incrementing counter
height int // the last block Update()'d to
rechecking int32 // for re-checking filtered txs on Update()
recheckCursor *clist.CElement // next expected response
recheckEnd *clist.CElement // re-checking stops here
notifiedTxsAvailable bool // true if fired on txsAvailable for this height
txsAvailable chan int // fires the next height once for each height, when the mempool is not empty
// Keep a cache of already-seen txs.
// This reduces the pressure on the proxyApp.
@@ -72,21 +74,17 @@ type Mempool struct {
// A log of mempool txs
wal *auto.AutoFile
// fires once for each height, when the mempool is not empty
txsAvailable chan struct{}
notifiedTxsAvailable bool
logger log.Logger
}
// NewMempool returns a new Mempool with the given configuration and connection to an application.
func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool) *Mempool {
func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, height int) *Mempool {
mempool := &Mempool{
config: config,
proxyAppConn: proxyAppConn,
txs: clist.New(),
counter: 0,
height: 0,
height: height,
rechecking: 0,
recheckCursor: nil,
recheckEnd: nil,
@@ -98,11 +96,11 @@ func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool) *M
return mempool
}
// FireOnTxsAvailable initializes the TxsAvailable channel,
// EnableTxsAvailable initializes the TxsAvailable channel,
// ensuring it will trigger once every height when transactions are available.
// NOTE: not thread safe - should only be called once, on startup
func (mem *Mempool) FireOnTxsAvailable() {
mem.txsAvailable = make(chan struct{}, 1)
func (mem *Mempool) EnableTxsAvailable() {
mem.txsAvailable = make(chan int, 1)
}
// SetLogger sets the Logger.
@@ -278,20 +276,20 @@ func (mem *Mempool) resCbRecheck(req *abci.Request, res *abci.Response) {
// TxsAvailable returns a channel which fires once for every height,
// and only when transactions are available in the mempool.
// XXX: Will panic if mem.FireOnTxsAvailable() has not been called.
func (mem *Mempool) TxsAvailable() chan struct{} {
if mem.txsAvailable == nil {
panic("mem.txsAvailable is nil")
}
// NOTE: the returned channel may be nil if EnableTxsAvailable was not called.
func (mem *Mempool) TxsAvailable() chan int {
return mem.txsAvailable
}
func (mem *Mempool) notifyIfTxsAvailable() {
if mem.Size() == 0 {
panic("notified txs available but mempool is empty!")
}
if mem.txsAvailable != nil &&
!mem.notifiedTxsAvailable && mem.Size() > 0 {
!mem.notifiedTxsAvailable {
mem.notifiedTxsAvailable = true
mem.txsAvailable <- struct{}{}
mem.txsAvailable <- mem.height + 1
}
}
@@ -339,8 +337,6 @@ func (mem *Mempool) Update(height int, txs types.Txs) {
}
// Set height
// NOTE: the height is not set until Update is first called
// (so it will be wrong after a restart until the next block)
mem.height = height
mem.notifiedTxsAvailable = false
+4 -4
View File
@@ -23,12 +23,12 @@ func newMempoolWithApp(t *testing.T, cc proxy.ClientCreator) *Mempool {
if _, err := appConnMem.Start(); err != nil {
t.Fatalf("Error starting ABCI client: %v", err.Error())
}
mempool := NewMempool(config.Mempool, appConnMem)
mempool := NewMempool(config.Mempool, appConnMem, 0)
mempool.SetLogger(log.TestingLogger())
return mempool
}
func ensureNoFire(t *testing.T, ch chan struct{}, timeoutMS int) {
func ensureNoFire(t *testing.T, ch chan int, timeoutMS int) {
timer := time.NewTimer(time.Duration(timeoutMS) * time.Millisecond)
select {
case <-ch:
@@ -37,7 +37,7 @@ func ensureNoFire(t *testing.T, ch chan struct{}, timeoutMS int) {
}
}
func ensureFire(t *testing.T, ch chan struct{}, timeoutMS int) {
func ensureFire(t *testing.T, ch chan int, timeoutMS int) {
timer := time.NewTimer(time.Duration(timeoutMS) * time.Millisecond)
select {
case <-ch:
@@ -64,7 +64,7 @@ func TestTxsAvailable(t *testing.T) {
app := dummy.NewDummyApplication()
cc := proxy.NewLocalClientCreator(app)
mempool := newMempoolWithApp(t, cc)
mempool.FireOnTxsAvailable()
mempool.EnableTxsAvailable()
timeoutMS := 500