redisinit

package module
v1.0.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 2 Imported by: 0

README

redisinit

import "github.com/induzo/gocom/database/redisinit"

This package allows you to init a redis client via go-redis.

Index

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

Documentation

Overview

This package allows you to init a redis client via go-redis.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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)

Types

type RedisClient

type RedisClient[T RedisError] interface {
	*redis.Client
	Ping(context.Context) T
}

type RedisError

type RedisError interface {
	*redis.StatusCmd
	Err() error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL