Golang Client - how to send a 'ping' to keep connection to server open?

393 views
Skip to first unread message

Shmuel C

unread,
Apr 9, 2020, 3:22:20 PM4/9/20
to nats
Services running inside k8s -- want to have a healthcheck that will mark the service as unhealthy if communication with nats is down, but I don't see a ping() call -- what would be a good approach?

Derek Collison

unread,
Apr 9, 2020, 5:40:57 PM4/9/20
to nat...@googlegroups.com
Need a bit more information. NATS will automatically find a receiver, and for services like this we recommend using queue subscribers such that you can run as many or as little service responders as you like. NATS will automatically reconnect if it gets disconnected from a NATS server. It understands all topology changes even after the application has been started, so it will find all new servers as well. If it can not connect at all it will report that the connection is closed and the app can exit and have k8s restart if needed.

On Thu, Apr 9, 2020 at 12:22 PM Shmuel C <scohen...@gmail.com> wrote:
Services running inside k8s -- want to have a healthcheck that will mark the service as unhealthy if communication with nats is down, but I don't see a ping() call -- what would be a good approach?

--
You received this message because you are subscribed to the Google Groups "nats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to natsio+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/natsio/773d8838-1f51-4533-a5de-03c2714b6c7b%40googlegroups.com.

Shmuel C

unread,
Apr 9, 2020, 8:18:42 PM4/9/20
to nats
So then, I could probably just use this in my LivenessCheck for k8s:  (where nc is the *nats.Conn)


func natsIsOkay() bool {
   return !nc.Closed()
}
   

On Thursday, April 9, 2020 at 5:40:57 PM UTC-4, derek wrote:
Need a bit more information. NATS will automatically find a receiver, and for services like this we recommend using queue subscribers such that you can run as many or as little service responders as you like. NATS will automatically reconnect if it gets disconnected from a NATS server. It understands all topology changes even after the application has been started, so it will find all new servers as well. If it can not connect at all it will report that the connection is closed and the app can exit and have k8s restart if needed.

On Thu, Apr 9, 2020 at 12:22 PM Shmuel C <scohen...@gmail.com> wrote:
Services running inside k8s -- want to have a healthcheck that will mark the service as unhealthy if communication with nats is down, but I don't see a ping() call -- what would be a good approach?

--
You received this message because you are subscribed to the Google Groups "nats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nat...@googlegroups.com.

Derek Collison

unread,
Apr 9, 2020, 10:36:51 PM4/9/20
to nat...@googlegroups.com
You could. In general NATS applications will self heal themselves in terms of the NATS connections themselves. If the only health check is regarding if the application is connected I would just have the application exit when it got a closed event. No need for health checks imo if that is all your checking.


To unsubscribe from this group and stop receiving emails from it, send an email to natsio+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/natsio/30e78d18-7605-490f-a24b-d66437323c95%40googlegroups.com.

R.I.Pienaar

unread,
Apr 10, 2020, 11:55:07 AM4/10/20
to natsio
Isn't a call to nc.FlushTimeout(time.Second) a round trip that errors if it fails, so possibly a good end2end health check

Derek Collison

unread,
Apr 10, 2020, 12:02:27 PM4/10/20
to nat...@googlegroups.com
Yes, but again in normal situations when we get disconnected the app will do the right thing with reconnecting and avoid needing to restart. If the health check is called while disconnected will timeout or error and that might not be what you really want. Would watch for close connection callback and exit myself, letting k8s reschedule and not do an health checks if they are only concerned with NATS, imo.

Reply all
Reply to author
Forward
0 new messages