autopilot/agent: don't block Stop on pending connetion
To ensure a call to ConnectToPeer doesn't block the agent from shutting down, we'll launch it in a non-waitgrouped goroutine, that will signal when a result is returned.
This commit is contained in:
parent
077e1887fa
commit
c19763c03f
@ -667,7 +667,40 @@ func (a *Agent) executeDirective(directive AttachmentDirective) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
alreadyConnected, err := a.cfg.ConnectToPeer(pub, directive.Addrs)
|
connected := make(chan bool)
|
||||||
|
errChan := make(chan error)
|
||||||
|
|
||||||
|
// To ensure a call to ConnectToPeer doesn't block the agent from
|
||||||
|
// shutting down, we'll launch it in a non-waitgrouped goroutine, that
|
||||||
|
// will signal when a result is returned.
|
||||||
|
// TODO(halseth): use DialContext to cancel on transport level.
|
||||||
|
go func() {
|
||||||
|
alreadyConnected, err := a.cfg.ConnectToPeer(
|
||||||
|
pub, directive.Addrs,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
select {
|
||||||
|
case errChan <- err:
|
||||||
|
case <-a.quit:
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case connected <- alreadyConnected:
|
||||||
|
case <-a.quit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var alreadyConnected bool
|
||||||
|
select {
|
||||||
|
case alreadyConnected = <-connected:
|
||||||
|
case err = <-errChan:
|
||||||
|
case <-a.quit:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Unable to connect to %x: %v",
|
log.Warnf("Unable to connect to %x: %v",
|
||||||
pub.SerializeCompressed(), err)
|
pub.SerializeCompressed(), err)
|
||||||
|
Loading…
Reference in New Issue
Block a user