@@ -67,10 +67,13 @@ func (p *StunTurnProbe) ProbeAllWaitResult(ctx context.Context, stuns []*stun.UR
6767 }
6868
6969 p .probeInProgress = true
70- p .probeDone = make (chan struct {})
70+ probeDone := make (chan struct {})
71+ p .probeDone = probeDone
7172 p .mu .Unlock ()
7273
7374 p .doProbe (ctx , stuns , turns , cacheKey )
75+ close (p .probeDone )
76+
7477 return p .getCachedResults (cacheKey , stuns , turns )
7578}
7679
@@ -79,22 +82,39 @@ func (p *StunTurnProbe) ProbeAll(ctx context.Context, stuns []*stun.URI, turns [
7982 cacheKey := generateCacheKey (stuns , turns )
8083
8184 p .mu .Lock ()
82- defer p .mu .Unlock ()
8385
8486 if results := p .checkCache (cacheKey ); results != nil {
87+ p .mu .Unlock ()
8588 return results
8689 }
8790
8891 if p .probeInProgress {
92+ p .mu .Unlock ()
8993 return createErrorResults (stuns , turns )
9094 }
9195
9296 p .probeInProgress = true
93- p .probeDone = make (chan struct {})
94- go p .doProbe (ctx , stuns , turns , cacheKey )
95-
97+ probeDone := make (chan struct {})
98+ p .probeDone = probeDone
9699 log .Infof ("started new probe for STUN, TURN servers" )
97- return createErrorResults (stuns , turns )
100+ go func () {
101+ p .doProbe (ctx , stuns , turns , cacheKey )
102+ close (probeDone )
103+ }()
104+
105+ p .mu .Unlock ()
106+
107+ select {
108+ case <- ctx .Done ():
109+ log .Debugf ("Context cancelled while waiting for probe results" )
110+ return createErrorResults (stuns , turns )
111+ case <- probeDone :
112+ // when the probe is return fast, return the results right away
113+ return p .getCachedResults (cacheKey , stuns , turns )
114+ case <- time .After (1300 * time .Millisecond ):
115+ // if the probe takes longer than 1.3s, return error results to avoid blocking
116+ return createErrorResults (stuns , turns )
117+ }
98118}
99119
100120func (p * StunTurnProbe ) checkCache (cacheKey string ) []ProbeResult {
@@ -123,7 +143,6 @@ func (p *StunTurnProbe) doProbe(ctx context.Context, stuns []*stun.URI, turns []
123143 defer func () {
124144 p .mu .Lock ()
125145 p .probeInProgress = false
126- close (p .probeDone )
127146 p .mu .Unlock ()
128147 }()
129148 results := make ([]ProbeResult , len (stuns )+ len (turns ))
0 commit comments