File tree 1 file changed +16
-10
lines changed
1 file changed +16
-10
lines changed Original file line number Diff line number Diff line change @@ -45,19 +45,25 @@ func (sp *ServerPicker) PickServer(parentCtx context.Context) (*Client, error) {
45
45
concurrentLimiter := make (chan struct {}, maxConcurrentServers )
46
46
47
47
log .Debugf ("pick server from list: %v" , sp .ServerURLs .Load ().([]string ))
48
+ go sp .processConnResults (connResultChan , successChan )
48
49
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
+ }
57
65
}
58
66
59
- go sp .processConnResults (connResultChan , successChan )
60
-
61
67
select {
62
68
case cr , ok := <- successChan :
63
69
if ! ok {
You can’t perform that action at this time.
0 commit comments