Fix close delay collecting STUN candidates

This was causing a 5s delay when closing the agent.
This commit is contained in:
Kyle Carberry
2022-04-18 13:41:35 -04:00
committed by Sean DuBois
parent e6cfc5103e
commit 1bbdb40185
+13 -1
View File
@@ -402,7 +402,7 @@ func (a *Agent) gatherCandidatesSrflxUDPMux(ctx context.Context, urls []*URL, ne
}
}
func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkTypes []NetworkType) {
func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkTypes []NetworkType) { //nolint:gocognit
var wg sync.WaitGroup
defer wg.Wait()
@@ -428,6 +428,18 @@ func (a *Agent) gatherCandidatesSrflx(ctx context.Context, urls []*URL, networkT
closeConnAndLog(conn, a.log, fmt.Sprintf("Failed to listen for %s: %v\n", serverAddr.String(), err))
return
}
// If the agent closes midway through the connection
// we end it early to prevent close delay.
cancelCtx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
go func() {
select {
case <-cancelCtx.Done():
return
case <-a.done:
_ = conn.Close()
}
}()
xoraddr, err := getXORMappedAddr(conn, serverAddr, stunGatherTimeout)
if err != nil {