0

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.

TruBlu
  • 441
  • 1
  • 4
  • 15
  • 1
    Some client libraries offer a `Ping()` method that executes Redis' `PING` command (https://redis.io/commands/ping) to check the status of the connection: `err := redisClient.Ping(ctx)` – GGP Aug 05 '22 at 22:20
  • 1
    Thank you @GastónPalomeque. This is exactly what I was looking for. If you submit an answer, I will mark it as solved. – TruBlu Aug 06 '22 at 15:29

4 Answers4

2

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 != nil {
    log.Fatal(err)
}
GGP
  • 314
  • 5
  • 6
  • 1
    The error is ping: PONG in which is should be the state of OK. am I wrong about that? – temple Dec 08 '22 at 21:10
  • I dont understand how this can be the correct answer as Ping() returns PONG and thus the error wont be nil either way? – Philip Mar 21 '23 at 12:49
  • 2
    The naming of the return var here is confusing. `err` is actually a `StatusCmd`. In order to check the error you'd call `redisClient.Ping(ctx).Err()` – jmoney Apr 28 '23 at 19:28
0

I think it depends on the client you use.

For example, the radix client (https://github.com/mediocregopher/radix) support a function to monitor for checking the connection (error, reused, created and so on.)

nimdrak
  • 72
  • 8
0
redisClient := redis.NewClient(...)
if err := redisClient.Ping(ctx).Err(); err != nil {
    panic(err)
}
beer
  • 42
  • 5
0

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)
    }
Shahriar Ahmed
  • 502
  • 4
  • 11