skip to Main Content

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.

4

Answers


  1. 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.)

    Login or Signup to reply.
  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)
    }
    
    Login or Signup to reply.
  3. redisClient := redis.NewClient(...)
    if err := redisClient.Ping(ctx).Err(); err != nil {
        panic(err)
    }
    
    Login or Signup to reply.
  4. 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)
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search