Skip to content

Commit

Permalink
add StopMon to only stop MonAddr but keeps updating blk num (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
i9 authored Apr 15, 2022
1 parent 16ff335 commit 58bb256
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions eth/mon2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (m *Monitor) MonAddr(cfg PerAddrCfg, cbfn EventCallback) {

for {
select {
case <-m.stopMon:
log.Infoln("MonAddr:", key, "stopped")
return

case <-m.quit:
log.Infoln("MonAddr:", key, "quit")
return
Expand Down
9 changes: 9 additions & 0 deletions eth/mon2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Monitor struct {

lock sync.RWMutex // protect blkNum, can be also used to protect ec when later we support replace failed ec
quit chan bool // this ch will close in m.Close so all loops know to exit
// support keep loopUpdateBlkNum running but stop all getLogs, needed for use case that monAddr when first start, then later knows no need, stop to save queries
stopMon chan bool
}

// cfg is not pointer to ensure value copy and avoid unexpected change by other code
Expand All @@ -80,15 +82,22 @@ func NewMonitor(ec EthClient, dal DAL, cfg PerChainCfg) (*Monitor, error) {
chainId: chid.Uint64(),
blkNum: blkNum,
quit: make(chan bool),
stopMon: make(chan bool),
}
go m.loopUpdateBlkNum()
return m, nil
}

// Stops all MonAddr and updating blk num to release all resources. No longer usable after this call
func (m *Monitor) Close() {
close(m.quit)
}

// StopMon will quit all MonAddr loops, but keep updating blk num.
func (m *Monitor) StopMon() {
close(m.stopMon)
}

// return cached block number from last BlockNumber call, no rpc, return in memory value directly
func (m *Monitor) GetBlkNum() uint64 {
m.lock.RLock()
Expand Down

0 comments on commit 58bb256

Please sign in to comment.