Skip to content

Commit

Permalink
Add go-redis v9
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Feb 4, 2023
1 parent 99179e8 commit 7e5e967
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions redis/v9/health_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package v9

import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"time"
)

type HealthChecker struct {
client *redis.Client
name string
timeout time.Duration
}

func NewRedisHealthChecker(db *redis.Client, name string, timeouts ...time.Duration) *HealthChecker {
var timeout time.Duration
if len(timeouts) >= 1 {
timeout = timeouts[0]
} else {
timeout = 4 * time.Second
}
return &HealthChecker{client: db, name: name, timeout: timeout}
}

func NewHealthChecker(db *redis.Client, options ...string) *HealthChecker {
var name string
if len(options) >= 1 && len(options[0]) > 0 {
name = options[0]
} else {
name = "redis"
}
return &HealthChecker{client: db, name: name, timeout: 4 * time.Second}
}

func (s *HealthChecker) Name() string {
return s.name
}

func (s *HealthChecker) Check(ctx context.Context) (map[string]interface{}, error) {
cancel := func() {}
if s.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, s.timeout)
}
defer cancel()

res := make(map[string]interface{})
checkerChan := make(chan error)
go func() {
_, err := s.client.Ping(ctx).Result()
checkerChan <- err
}()
select {
case err := <-checkerChan:
return res, err
case <-ctx.Done():
return res, fmt.Errorf("timeout")
}
}

func (s *HealthChecker) Build(ctx context.Context, data map[string]interface{}, err error) map[string]interface{} {
if err == nil {
return data
}
if data == nil {
data = make(map[string]interface{}, 0)
}
data["error"] = err.Error()
return data
}

0 comments on commit 7e5e967

Please sign in to comment.