From 5af19bb2b409895c61a3f41cb69a8507592317d2 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 30 Jul 2018 22:25:38 -0700 Subject: [PATCH] htlcswitch/link: reusable BatchTicker This commit modifies the default BatchTicker implementation such that it will generate a new ticker with each call to Start(). This allows us to create a new ticker after releasing an old one due to the batch being empty. --- htlcswitch/link.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 2914f9f2..543a2a8f 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -107,22 +107,29 @@ type Ticker interface { // BatchTicker implements the Ticker interface, and wraps a time.Ticker. type BatchTicker struct { - ticker *time.Ticker + duration time.Duration + ticker *time.Ticker } // NewBatchTicker returns a new BatchTicker that wraps the passed time.Ticker. -func NewBatchTicker(t *time.Ticker) *BatchTicker { - return &BatchTicker{t} +func NewBatchTicker(d time.Duration) *BatchTicker { + return &BatchTicker{ + duration: d, + } } // Start returns the tick channel for the underlying time.Ticker. func (t *BatchTicker) Start() <-chan time.Time { + t.ticker = time.NewTicker(t.duration) return t.ticker.C } // Stop stops the underlying time.Ticker. func (t *BatchTicker) Stop() { - t.ticker.Stop() + if t.ticker != nil { + t.ticker.Stop() + t.ticker = nil + } } // ChannelLinkConfig defines the configuration for the channel link. ALL @@ -794,6 +801,7 @@ func (l *channelLink) fwdPkgGarbager() { // NOTE: This MUST be run as a goroutine. func (l *channelLink) htlcManager() { defer func() { + l.cfg.BatchTicker.Stop() l.wg.Done() log.Infof("ChannelLink(%v) has exited", l) }() @@ -843,9 +851,6 @@ func (l *channelLink) htlcManager() { l.wg.Add(1) go l.fwdPkgGarbager() - batchTick := l.cfg.BatchTicker.Start() - defer l.cfg.BatchTicker.Stop() - // We'll only need the batch ticker if we have outgoing updates that are // not covered by our last signature. This value will be nil unless a // downstream packet forces the batchCounter to be positive. After the @@ -938,6 +943,7 @@ out: // here. We also disable the batch ticker from waking up // the htlcManager while the batch is empty. if l.batchCounter == 0 { + l.cfg.BatchTicker.Stop() maybeBatchTick = nil continue } @@ -967,8 +973,8 @@ out: // If the downstream packet resulted in a non-empty // batch, reinstate the batch ticker so that it can be // cleared. - if l.batchCounter > 0 { - maybeBatchTick = batchTick + if l.batchCounter > 0 && maybeBatchTick == nil { + maybeBatchTick = l.cfg.BatchTicker.Start() } // A message from the switch was just received. This indicates @@ -996,8 +1002,8 @@ out: // If the downstream packet resulted in a non-empty // batch, reinstate the batch ticker so that it can be // cleared. - if l.batchCounter > 0 { - maybeBatchTick = batchTick + if l.batchCounter > 0 && maybeBatchTick == nil { + maybeBatchTick = l.cfg.BatchTicker.Start() } // A message from the connected peer was just received. This