# README
redisinit
import "github.com/induzo/gocom/database/redisinit"
This package allows you to init a redis client via go-redis.
Index
- func ClientHealthCheck[T RedisClient[U], U RedisError](cli T) func(ctx context.Context) error
- type RedisClient
- type RedisError
func ClientHealthCheck
func ClientHealthCheck[T RedisClient[U], U RedisError](cli T) func(ctx context.Context) error
ClientHealthCheck returns a health check function for redis.Client that can be used in health endpoint.
Example
Using standard net/http package. We can also simply pass healthCheck as a CheckFn in gocom/transport/http/health/v2.
{
ctx := context.Background()
cli := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
healthCheck := redisinit.ClientHealthCheck(cli)
mux := http.NewServeMux()
mux.HandleFunc("/sys/health", func(rw http.ResponseWriter, _ *http.Request) {
if err := healthCheck(ctx); err != nil {
rw.WriteHeader(http.StatusServiceUnavailable)
}
})
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/sys/health", nil)
nr := httptest.NewRecorder()
mux.ServeHTTP(nr, req)
rr := nr.Result()
defer rr.Body.Close()
fmt.Println(rr.StatusCode)
}
type RedisClient
type RedisClient[T RedisError] interface {
*redis.Client
Ping(context.Context) T
}
type RedisError
type RedisError interface {
*redis.StatusCmd
Err() error
}
Generated by gomarkdoc
# Functions
ClientHealthCheck returns a health check function for redis.Client that can be used in health endpoint.