valkey-io / valkey-go

A fast Golang Valkey client that supports Client Side Caching and Auto Pipelining.
Apache License 2.0
157 stars 10 forks source link

[Question] Ping function #14

Closed savi2w closed 1 month ago

savi2w commented 1 month ago

How can I ping a Valkey server just to know if he is alive and healthy? I feel functions like that are extremely useful when you're dealing with microservices and a lot of DBs (especially at the microservice startup)

I know I can set a dumb key or something like, the point here is just to making sure I ain't missing anything

gotama commented 1 month ago

There is a PING command that responds with PONG.

Not sure about valkey documentation but here is redis version

https://redis.io/docs/latest/commands/ping/

rueian commented 1 month ago

You can do PING with valkey-go by:

package main

import (
    "context"
    "github.com/valkey-io/valkey-go"
)

func main() {
    client, err := valkey.NewClient(valkey.ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
    if err != nil {
        panic(err)
    }
    err = client.Do(context.Background(), client.B().Ping().Build()).Error()
}

However, you usually don't need to do that. If the err return by valkey.NewClient is nil, the server is guaranteed to be alive.

savi2w commented 1 month ago

You can do PING with valkey-go by:

package main

import (
  "context"
  "github.com/valkey-io/valkey-go"
)

func main() {
  client, err := valkey.NewClient(valkey.ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
  if err != nil {
      panic(err)
  }
  err = client.Do(context.Background(), client.B().Ping().Build()).Error()
}

However, you usually don't need to do that. If the err return by valkey.NewClient is nil, the server is guaranteed to be alive.

Didn't know the Newclient already do that, thanks!