Skip to content

Commit

Permalink
Tweak the reconnect interval
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek committed Aug 5, 2024
1 parent 9c1e13b commit e648cc0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions httprateredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *redisCounter) IncrementBy(key string, currentWindow time.Time, amount i
// On redis network error, fallback to local in-memory counter.
var netErr net.Error
if errors.As(err, &netErr) || errors.Is(err, redis.ErrClosed) {
go c.fallback()
c.fallback()
err = c.fallbackCounter.IncrementBy(key, currentWindow, amount)
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c *redisCounter) Get(key string, currentWindow, previousWindow time.Time)
// On redis network error, fallback to local in-memory counter.
var netErr net.Error
if errors.As(err, &netErr) || errors.Is(err, redis.ErrClosed) {
go c.fallback()
c.fallback()
curr, prev, err = c.fallbackCounter.Get(key, currentWindow, previousWindow)
}
}
Expand Down Expand Up @@ -191,19 +191,23 @@ func (c *redisCounter) IsFallbackActivated() bool {

func (c *redisCounter) fallback() {
// Activate the in-memory counter fallback, unless activated by some other goroutine.
wasAlreadyActive := c.fallbackActivated.Swap(true)
if wasAlreadyActive {
fallbackAlreadyActivated := c.fallbackActivated.Swap(true)
if fallbackAlreadyActivated {
return
}

// Try to re-connect to redis every 50ms.
go c.reconnect()
}

func (c *redisCounter) reconnect() {
// Try to re-connect to redis every 200ms.
for {
err := c.client.Ping(context.Background()).Err()
if err == nil {
c.fallbackActivated.Store(false)
return
}
time.Sleep(50 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
}
}

Expand Down

0 comments on commit e648cc0

Please sign in to comment.