Get rid of the `loopExitHandler` callback
It's safer to call the `cancelLoopContext`
directly from `RunForwarder` than trusting
the `pushingLoop` to do this via a
`loopExitHandler` call.
@@ -46,8 +46,6 @@ type Forwarder struct {
headersQueue chan *btc.Header
errChan chan error
-
- loopExitHandler func()
}
// RunForwarder creates an instance of the block forwarder and runs its
@@ -65,21 +63,19 @@ func RunForwarder(
hostChain: hostChain,
headersQueue: make(chan *btc.Header, headersQueueSize),
errChan: make(chan error, 1),
- loopExitHandler: cancelLoopCtx,
}
- go forwarder.pushingLoop(loopCtx)
+ go func() {
+ forwarder.pushingLoop(loopCtx)
+ cancelLoopCtx() // loop exited, cancel the context
+ }()
return forwarder
}
func (f *Forwarder) pushingLoop(ctx context.Context) {
logger.Infof("running new block pushing loop")
-
- defer func() {
- logger.Infof("stopping current block pushing loop")
- f.loopExitHandler()
- }()
+ defer logger.Infof("stopping current block pushing loop")
for {
select {
