Skip to content

Commit 0dfc17a

Browse files
committed
check if we have a successful connection so we do not need to connect to other servers
1 parent 18216ba commit 0dfc17a

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

relay/client/picker.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,25 @@ func (sp *ServerPicker) PickServer(parentCtx context.Context) (*Client, error) {
4545
concurrentLimiter := make(chan struct{}, maxConcurrentServers)
4646

4747
log.Debugf("pick server from list: %v", sp.ServerURLs.Load().([]string))
48+
go sp.processConnResults(connResultChan, successChan)
4849
for _, url := range sp.ServerURLs.Load().([]string) {
49-
// todo check if we have a successful connection so we do not need to connect to other servers
50-
concurrentLimiter <- struct{}{}
51-
go func(url string) {
52-
defer func() {
53-
<-concurrentLimiter
54-
}()
55-
sp.startConnection(parentCtx, connResultChan, url)
56-
}(url)
50+
select {
51+
case concurrentLimiter <- struct{}{}:
52+
go func(url string) {
53+
defer func() {
54+
<-concurrentLimiter
55+
}()
56+
sp.startConnection(parentCtx, connResultChan, url)
57+
}(url)
58+
case cr, ok := <-successChan:
59+
if !ok {
60+
return nil, errors.New("failed to connect to any relay server: all attempts failed")
61+
}
62+
log.Infof("chosen home Relay server: %s with latency %s", cr.Url, cr.Latency)
63+
return cr.RelayClient, nil
64+
}
5765
}
5866

59-
go sp.processConnResults(connResultChan, successChan)
60-
6167
select {
6268
case cr, ok := <-successChan:
6369
if !ok {

0 commit comments

Comments
 (0)