Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang redis client connection status

After creting a new Redis client, is there a way to check the status of the connection?

As a way to ensure that the Sentinel is in a healthy state, a status check after instantiation would be ideal.

like image 551
TruBlu Avatar asked Sep 07 '25 00:09

TruBlu


2 Answers

Some client libraries offer a Ping() method that executes Redis' PING command to check the status of the connection:

redisClient := redis.NewClient(...)
if err := redisClient.Ping(ctx).Err(); err != nil {
    log.Fatal(err)
}
like image 120
GGP Avatar answered Sep 09 '25 17:09

GGP


If you use go-redis client for connecting to redis then you can try the following to check if redis is connected properly.

    client := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "",
        DB:       0,
    })
    if pong := client.Ping(ctx); pong.String() != "ping: PONG" {
        log.Println("-------------Error connection redis ----------:", pong)
    }
like image 43
Shahriar Ahmed Avatar answered Sep 09 '25 15:09

Shahriar Ahmed