Skip to content

Commit 381adf7

Browse files
authored
Add health check to Redis (#203)
1 parent fd5b2fe commit 381adf7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

cache/redis/redis.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
var ErrNotFound = fmt.Errorf("not found")
1515

16+
const healthCheckTimeout = 2 * time.Second
17+
1618
type Redis struct {
1719
client *redis.Client
1820
}
@@ -175,3 +177,18 @@ func (r *Redis) Reconnect(ctx context.Context, host string) error {
175177
func (r *Redis) Close() error {
176178
return r.client.Close()
177179
}
180+
181+
func (r *Redis) HealthCheck() error {
182+
if r == nil {
183+
return nil
184+
}
185+
186+
ctx, cancel := context.WithTimeout(context.Background(), healthCheckTimeout)
187+
defer cancel()
188+
189+
if !r.IsAvailable(ctx) {
190+
return errors.New("redis is not available")
191+
}
192+
193+
return nil
194+
}

0 commit comments

Comments
 (0)