Skip to content

Commit

Permalink
check if we have a successful connection so we do not need to connect…
Browse files Browse the repository at this point in the history
… to other servers
  • Loading branch information
mohamed-essam committed Nov 26, 2024
1 parent 18216ba commit 0dfc17a
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions relay/client/picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ func (sp *ServerPicker) PickServer(parentCtx context.Context) (*Client, error) {
concurrentLimiter := make(chan struct{}, maxConcurrentServers)

log.Debugf("pick server from list: %v", sp.ServerURLs.Load().([]string))
go sp.processConnResults(connResultChan, successChan)
for _, url := range sp.ServerURLs.Load().([]string) {
// todo check if we have a successful connection so we do not need to connect to other servers
concurrentLimiter <- struct{}{}
go func(url string) {
defer func() {
<-concurrentLimiter
}()
sp.startConnection(parentCtx, connResultChan, url)
}(url)
select {
case concurrentLimiter <- struct{}{}:
go func(url string) {
defer func() {
<-concurrentLimiter
}()
sp.startConnection(parentCtx, connResultChan, url)
}(url)
case cr, ok := <-successChan:
if !ok {
return nil, errors.New("failed to connect to any relay server: all attempts failed")
}
log.Infof("chosen home Relay server: %s with latency %s", cr.Url, cr.Latency)
return cr.RelayClient, nil
}
}

go sp.processConnResults(connResultChan, successChan)

select {
case cr, ok := <-successChan:
if !ok {
Expand Down

0 comments on commit 0dfc17a

Please sign in to comment.