From e3253a4f3bef61001890453335de2fe67bfebcf2 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Tue, 31 Jul 2018 17:01:19 -0700 Subject: [PATCH] htlcswitch/switch: add log/fwd event tickers to Config --- htlcswitch/switch.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index d1473ec7..e2ee14b0 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -21,6 +21,17 @@ import ( "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/ticker" +) + +const ( + // DefaultFwdEventInterval is the duration between attempts to flush + // pending forwarding events to disk. + DefaultFwdEventInterval = 15 * time.Second + + // DefaultLogInterval is the duration between attempts to log statistics + // about forwarding events. + DefaultLogInterval = 10 * time.Second ) var ( @@ -146,6 +157,14 @@ type Config struct { // Notifier is an instance of a chain notifier that we'll use to signal // the switch when a new block has arrived. Notifier chainntnfs.ChainNotifier + + // FwdEventTicker is a signal that instructs the htlcswitch to flush any + // pending forwarding events. + FwdEventTicker ticker.Ticker + + // LogEventTicker is a signal instructing the htlcswitch to log + // aggregate stats about it's forwarding during the last interval. + LogEventTicker ticker.Ticker } // Switch is the central messaging bus for all incoming/outgoing HTLCs. @@ -1390,13 +1409,13 @@ func (s *Switch) htlcForwarder() { totalSatSent btcutil.Amount totalSatRecv btcutil.Amount ) - logTicker := time.NewTicker(10 * time.Second) - defer logTicker.Stop() + s.cfg.LogEventTicker.Resume() + defer s.cfg.LogEventTicker.Stop() // Every 15 seconds, we'll flush out the forwarding events that // occurred during that period. - fwdEventTicker := time.NewTicker(15 * time.Second) - defer fwdEventTicker.Stop() + s.cfg.FwdEventTicker.Resume() + defer s.cfg.FwdEventTicker.Stop() out: for { @@ -1474,7 +1493,7 @@ out: // When this time ticks, then it indicates that we should // collect all the forwarding events since the last internal, // and write them out to our log. - case <-fwdEventTicker.C: + case <-s.cfg.FwdEventTicker.Ticks(): s.wg.Add(1) go func() { defer s.wg.Done() @@ -1488,7 +1507,7 @@ out: // The log ticker has fired, so we'll calculate some forwarding // stats for the last 10 seconds to display within the logs to // users. - case <-logTicker.C: + case <-s.cfg.LogEventTicker.Ticks(): // First, we'll collate the current running tally of // our forwarding stats. prevSatSent := totalSatSent