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:
parent
bd9a6bd625
commit
5af19bb2b4
@ -107,22 +107,29 @@ type Ticker interface {
|
|||||||
|
|
||||||
// BatchTicker implements the Ticker interface, and wraps a time.Ticker.
|
// BatchTicker implements the Ticker interface, and wraps a time.Ticker.
|
||||||
type BatchTicker struct {
|
type BatchTicker struct {
|
||||||
ticker *time.Ticker
|
duration time.Duration
|
||||||
|
ticker *time.Ticker
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBatchTicker returns a new BatchTicker that wraps the passed time.Ticker.
|
// NewBatchTicker returns a new BatchTicker that wraps the passed time.Ticker.
|
||||||
func NewBatchTicker(t *time.Ticker) *BatchTicker {
|
func NewBatchTicker(d time.Duration) *BatchTicker {
|
||||||
return &BatchTicker{t}
|
return &BatchTicker{
|
||||||
|
duration: d,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start returns the tick channel for the underlying time.Ticker.
|
// Start returns the tick channel for the underlying time.Ticker.
|
||||||
func (t *BatchTicker) Start() <-chan time.Time {
|
func (t *BatchTicker) Start() <-chan time.Time {
|
||||||
|
t.ticker = time.NewTicker(t.duration)
|
||||||
return t.ticker.C
|
return t.ticker.C
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop stops the underlying time.Ticker.
|
// Stop stops the underlying time.Ticker.
|
||||||
func (t *BatchTicker) Stop() {
|
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
|
// 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.
|
// NOTE: This MUST be run as a goroutine.
|
||||||
func (l *channelLink) htlcManager() {
|
func (l *channelLink) htlcManager() {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
l.cfg.BatchTicker.Stop()
|
||||||
l.wg.Done()
|
l.wg.Done()
|
||||||
log.Infof("ChannelLink(%v) has exited", l)
|
log.Infof("ChannelLink(%v) has exited", l)
|
||||||
}()
|
}()
|
||||||
@ -843,9 +851,6 @@ func (l *channelLink) htlcManager() {
|
|||||||
l.wg.Add(1)
|
l.wg.Add(1)
|
||||||
go l.fwdPkgGarbager()
|
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
|
// 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
|
// not covered by our last signature. This value will be nil unless a
|
||||||
// downstream packet forces the batchCounter to be positive. After the
|
// 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
|
// here. We also disable the batch ticker from waking up
|
||||||
// the htlcManager while the batch is empty.
|
// the htlcManager while the batch is empty.
|
||||||
if l.batchCounter == 0 {
|
if l.batchCounter == 0 {
|
||||||
|
l.cfg.BatchTicker.Stop()
|
||||||
maybeBatchTick = nil
|
maybeBatchTick = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -967,8 +973,8 @@ out:
|
|||||||
// If the downstream packet resulted in a non-empty
|
// If the downstream packet resulted in a non-empty
|
||||||
// batch, reinstate the batch ticker so that it can be
|
// batch, reinstate the batch ticker so that it can be
|
||||||
// cleared.
|
// cleared.
|
||||||
if l.batchCounter > 0 {
|
if l.batchCounter > 0 && maybeBatchTick == nil {
|
||||||
maybeBatchTick = batchTick
|
maybeBatchTick = l.cfg.BatchTicker.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A message from the switch was just received. This indicates
|
// A message from the switch was just received. This indicates
|
||||||
@ -996,8 +1002,8 @@ out:
|
|||||||
// If the downstream packet resulted in a non-empty
|
// If the downstream packet resulted in a non-empty
|
||||||
// batch, reinstate the batch ticker so that it can be
|
// batch, reinstate the batch ticker so that it can be
|
||||||
// cleared.
|
// cleared.
|
||||||
if l.batchCounter > 0 {
|
if l.batchCounter > 0 && maybeBatchTick == nil {
|
||||||
maybeBatchTick = batchTick
|
maybeBatchTick = l.cfg.BatchTicker.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A message from the connected peer was just received. This
|
// A message from the connected peer was just received. This
|
||||||
|
Loading…
Reference in New Issue
Block a user