diff options
author | Janoš Guljaš <janos@users.noreply.github.com> | 2019-01-29 22:19:54 +0800 |
---|---|---|
committer | Anton Evangelatov <anton.evangelatov@gmail.com> | 2019-01-29 22:19:54 +0800 |
commit | 74c38902ecec507675e1e9033500addc87e4b7b0 (patch) | |
tree | 5a785ee92d6aec0980d9d3ede2bc8dda17a7fef6 | |
parent | a0ac3b6a1a1818285b74d9580c6d331837ec2e31 (diff) | |
download | go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.tar go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.tar.gz go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.tar.bz2 go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.tar.lz go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.tar.xz go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.tar.zst go-tangerine-74c38902ecec507675e1e9033500addc87e4b7b0.zip |
p2p/protocols: fix possible metrics loss in AccountingMetrics (#18956)
-rw-r--r-- | p2p/protocols/reporter.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/p2p/protocols/reporter.go b/p2p/protocols/reporter.go index 215d4fe31..9612b4a4d 100644 --- a/p2p/protocols/reporter.go +++ b/p2p/protocols/reporter.go @@ -36,6 +36,13 @@ type AccountingMetrics struct { //for a graceful cleanup func (am *AccountingMetrics) Close() { close(am.reporter.quit) + // wait for reporter loop to finish saving metrics + // before reporter database is closed + select { + case <-time.After(10 * time.Second): + log.Error("accounting metrics reporter timeout") + case <-am.reporter.done: + } am.reporter.db.Close() } @@ -46,6 +53,7 @@ type reporter struct { interval time.Duration //duration at which the reporter will persist metrics db *leveldb.DB //the actual DB quit chan struct{} //quit the reporter loop + done chan struct{} //signal that reporter loop is done } //NewMetricsDB creates a new LevelDB instance used to persist metrics defined @@ -92,6 +100,7 @@ func NewAccountingMetrics(r metrics.Registry, d time.Duration, path string) *Acc interval: d, db: db, quit: make(chan struct{}), + done: make(chan struct{}), } //run the go routine @@ -106,6 +115,9 @@ func NewAccountingMetrics(r metrics.Registry, d time.Duration, path string) *Acc //run is the goroutine which periodically sends the metrics to the configured LevelDB func (r *reporter) run() { + // signal that the reporter loop is done + defer close(r.done) + intervalTicker := time.NewTicker(r.interval) for { @@ -121,6 +133,9 @@ func (r *reporter) run() { } case <-r.quit: //graceful shutdown + if err := r.save(); err != nil { + log.Error("unable to send metrics to LevelDB", "err", err) + } return } } |