Skip to content

Commit

Permalink
Added simplified hostcache implementation for rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Aug 8, 2024
1 parent 9eb3acf commit 4723195
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ably/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,3 +931,29 @@ func decodeResp(resp *http.Response, out interface{}) error {

return decode(typ, bytes.NewReader(b), out)
}

// hostCache caches a successful fallback host for 10 minutes.
// Only used by REST client while making requests RSC15f
type hostCache struct {
duration time.Duration

sync.RWMutex
deadline time.Time
host string
}

func (c *hostCache) put(host string) {
c.Lock()
defer c.Unlock()
c.host = host
c.deadline = time.Now().Add(c.duration)
}

func (c *hostCache) get() string {
c.RLock()
defer c.RUnlock()
if ablyutil.Empty(c.host) || time.Until(c.deadline) <= 0 {
return ""
}
return c.host
}

0 comments on commit 4723195

Please sign in to comment.