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.
This commit is contained in:
Conner Fromknecht 2018-07-30 22:25:38 -07:00
parent bd9a6bd625
commit 5af19bb2b4
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

View File

@ -107,22 +107,29 @@ type Ticker interface {
// BatchTicker implements the Ticker interface, and wraps a time.Ticker.
type BatchTicker struct {
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() {
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