Skip to content

Commit

Permalink
Simplified realtime fallbacks, Removed use of separate hosts struct a…
Browse files Browse the repository at this point in the history
…nd tests
  • Loading branch information
sacOO7 committed Aug 13, 2024
1 parent 2168f83 commit 2a8d98e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 234 deletions.
34 changes: 0 additions & 34 deletions ably/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,12 @@ import (
"net/http/httptrace"
"net/url"
"time"

"github.com/ably/ably-go/ably/internal/ablyutil"
)

func NewClientOptions(os ...ClientOption) *clientOptions {
return applyOptionsWithDefaults(os...)
}

func NewRealtimeHosts(opts *clientOptions) *realtimeHosts {
return newRealtimeHosts(opts)
}

func (realtimeHosts *realtimeHosts) NextFallbackHost() string {
return realtimeHosts.nextFallbackHost()
}

func (realtimeHosts *realtimeHosts) GetAllRemainingFallbackHosts() []string {
var hosts []string
for true {
fallbackHost := realtimeHosts.NextFallbackHost()
if ablyutil.Empty(fallbackHost) {
break
}
hosts = append(hosts, fallbackHost)
}
return hosts
}

func (realtimeHosts *realtimeHosts) ResetVisitedFallbackHosts() {
realtimeHosts.resetVisitedFallbackHosts()
}

func (realtimeHosts *realtimeHosts) FallbackHostsRemaining() int {
return realtimeHosts.fallbackHostsRemaining()
}

func (realtimeHosts *realtimeHosts) GetPreferredHost() string {
return realtimeHosts.getPreferredHost()
}

func GetEnvFallbackHosts(env string) []string {
return getEnvFallbackHosts(env)
}
Expand Down
70 changes: 0 additions & 70 deletions ably/hosts.go

This file was deleted.

120 changes: 0 additions & 120 deletions ably/hosts_test.go

This file was deleted.

21 changes: 11 additions & 10 deletions ably/realtime_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"
"sync"
"time"

"github.com/ably/ably-go/ably/internal/ablyutil"
)

var (
Expand Down Expand Up @@ -77,7 +79,6 @@ type Connection struct {
// after a reauthorization, to avoid re-reauthorizing.
reauthorizing bool
arg connArgs
hosts *realtimeHosts
client *Realtime

readLimit int64
Expand Down Expand Up @@ -113,7 +114,6 @@ func newConn(opts *clientOptions, auth *Auth, callbacks connCallbacks, client *R
}
auth.onExplicitAuthorize = c.onClientAuthorize
c.queue = newMsgQueue(c)
c.hosts = newRealtimeHosts(c.opts)
if !opts.NoConnect {
c.setState(ConnectionStateConnecting, nil, 0)
go func() {
Expand Down Expand Up @@ -368,7 +368,6 @@ func (c *Connection) connectWithRetryLoop(arg connArgs) (result, error) {
}

func (c *Connection) connectWith(arg connArgs) (result, error) {
defer c.hosts.resetVisitedFallbackHosts()
connectMode := c.getMode()

c.mtx.Lock()
Expand All @@ -388,8 +387,11 @@ func (c *Connection) connectWith(arg connArgs) (result, error) {
}

var conn conn
host := c.hosts.getPreferredHost()
for {
primaryHost := c.opts.getRealtimeHost()
fallbackHosts, _ := c.opts.getFallbackHosts()
// Always try primary host first and then fallback hosts for realtime conn
hosts := append([]string{primaryHost}, ablyutil.Shuffle(fallbackHosts)...)
for hostCounter, host := range hosts {
u, err := url.Parse(c.opts.realtimeURL(host))
if err != nil {
return nil, err
Expand All @@ -407,19 +409,18 @@ func (c *Connection) connectWith(arg connArgs) (result, error) {
// if err is nil, raw connection with server is successful
conn, err = c.dial(proto, u)
if err == nil { // success
if host != c.hosts.getPrimaryHost() { // RTN17e
if host != primaryHost { // RTN17e
c.client.rest.setActiveRealtimeHost(host)
} else if !empty(c.client.rest.activeRealtimeHost) {
c.client.rest.setActiveRealtimeHost("") // reset to default
}
break
}
resp := extractHttpResponseFromConn(c.conn)
if c.hosts.fallbackHostsRemaining() > 0 && canFallBack(err, resp) && c.opts.hasActiveInternetConnection() { // RTN17d, RTN17c
host = c.hosts.nextFallbackHost()
} else {
return nil, err
if hostCounter < len(hosts)-1 && canFallBack(err, resp) && c.opts.hasActiveInternetConnection() { // RTN17d, RTN17c
continue
}
return nil, err
}

c.mtx.Lock()
Expand Down

0 comments on commit 2a8d98e

Please sign in to comment.