32 lines
708 B
Go
32 lines
708 B
Go
package redisconn
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// Ping bounds client.Ping under timeout and returns a wrapped error so
|
|
// startup failures are easy to spot in service logs.
|
|
//
|
|
// timeout is typically taken from Config.OperationTimeout.
|
|
func Ping(ctx context.Context, client *redis.Client, timeout time.Duration) error {
|
|
if client == nil {
|
|
return errors.New("ping redis: nil client")
|
|
}
|
|
if timeout <= 0 {
|
|
return errors.New("ping redis: timeout must be positive")
|
|
}
|
|
|
|
pingCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
if err := client.Ping(pingCtx).Err(); err != nil {
|
|
return fmt.Errorf("ping redis: %w", err)
|
|
}
|
|
return nil
|
|
}
|