Skip to content

Commit

Permalink
Add health check to Redis (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
vminkobin authored Jan 15, 2023
1 parent fd5b2fe commit 381adf7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

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

const healthCheckTimeout = 2 * time.Second

type Redis struct {
client *redis.Client
}
Expand Down Expand Up @@ -175,3 +177,18 @@ func (r *Redis) Reconnect(ctx context.Context, host string) error {
func (r *Redis) Close() error {
return r.client.Close()
}

func (r *Redis) HealthCheck() error {
if r == nil {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), healthCheckTimeout)
defer cancel()

if !r.IsAvailable(ctx) {
return errors.New("redis is not available")
}

return nil
}

0 comments on commit 381adf7

Please sign in to comment.