8
8
"strconv"
9
9
"sync"
10
10
"time"
11
+
12
+ "github.com/ably/ably-go/ably/internal/ablyutil"
11
13
)
12
14
13
15
var (
@@ -77,7 +79,6 @@ type Connection struct {
77
79
// after a reauthorization, to avoid re-reauthorizing.
78
80
reauthorizing bool
79
81
arg connArgs
80
- hosts * realtimeHosts
81
82
client * Realtime
82
83
83
84
readLimit int64
@@ -113,7 +114,6 @@ func newConn(opts *clientOptions, auth *Auth, callbacks connCallbacks, client *R
113
114
}
114
115
auth .onExplicitAuthorize = c .onClientAuthorize
115
116
c .queue = newMsgQueue (c )
116
- c .hosts = newRealtimeHosts (c .opts )
117
117
if ! opts .NoConnect {
118
118
c .setState (ConnectionStateConnecting , nil , 0 )
119
119
go func () {
@@ -368,7 +368,6 @@ func (c *Connection) connectWithRetryLoop(arg connArgs) (result, error) {
368
368
}
369
369
370
370
func (c * Connection ) connectWith (arg connArgs ) (result , error ) {
371
- defer c .hosts .resetVisitedFallbackHosts ()
372
371
connectMode := c .getMode ()
373
372
374
373
c .mtx .Lock ()
@@ -388,8 +387,11 @@ func (c *Connection) connectWith(arg connArgs) (result, error) {
388
387
}
389
388
390
389
var conn conn
391
- host := c .hosts .getPreferredHost ()
392
- for {
390
+ primaryHost := c .opts .getRealtimeHost ()
391
+ fallbackHosts , _ := c .opts .getFallbackHosts ()
392
+ // Always try primary host first and then fallback hosts for realtime conn
393
+ hosts := append ([]string {primaryHost }, ablyutil .Shuffle (fallbackHosts )... )
394
+ for hostCounter , host := range hosts {
393
395
u , err := url .Parse (c .opts .realtimeURL (host ))
394
396
if err != nil {
395
397
return nil , err
@@ -407,19 +409,18 @@ func (c *Connection) connectWith(arg connArgs) (result, error) {
407
409
// if err is nil, raw connection with server is successful
408
410
conn , err = c .dial (proto , u )
409
411
if err == nil { // success
410
- if host != c . hosts . getPrimaryHost () { // RTN17e
412
+ if host != primaryHost { // RTN17e
411
413
c .client .rest .setActiveRealtimeHost (host )
412
414
} else if ! empty (c .client .rest .activeRealtimeHost ) {
413
415
c .client .rest .setActiveRealtimeHost ("" ) // reset to default
414
416
}
415
417
break
416
418
}
417
419
resp := extractHttpResponseFromConn (c .conn )
418
- if c .hosts .fallbackHostsRemaining () > 0 && canFallBack (err , resp ) && c .opts .hasActiveInternetConnection () { // RTN17d, RTN17c
419
- host = c .hosts .nextFallbackHost ()
420
- } else {
421
- return nil , err
420
+ if hostCounter < len (hosts )- 1 && canFallBack (err , resp ) && c .opts .hasActiveInternetConnection () { // RTN17d, RTN17c
421
+ continue
422
422
}
423
+ return nil , err
423
424
}
424
425
425
426
c .mtx .Lock ()
0 commit comments