silas / node-consul

Consul client
https://www.npmjs.com/package/consul
MIT License
560 stars 83 forks source link

Question about the usage of "end" function in watch.js #155

Closed xiaoxiaodek closed 1 year ago

xiaoxiaodek commented 1 year ago

Hello,

I have been using the node-consul library for my project in a Kubernetes environment. Recently, I encountered a situation where the service listening did not update as expected. Upon inspecting the network traffic, I noticed that there were no further requests made to Consul. I suspect that this issue might be related to the "end" function being called in the following scenarios:

https://github.com/silas/node-consul/blob/fe75b4ad4ddc15607fd7ece77a1792072d568212/lib/watch.js#L122-L124

I would like to understand the rationale behind calling the "end" function in these cases. Could someone please explain why the "end" function is invoked in these specific scenarios?

Thank you for your assistance!

silas commented 1 year ago

This first line ends the watch when the library raises a validation error. Basically this happens when one of the internal checks are violated (e.g. you pass an object when the library expects a string).

The second line is when Consul returns a 400 status code, which generally indicates that the request as specified is invalid and should not be attempted again without changes to the request. Since watch will not change the request other than parameters related to the watch, it is assumed the request is invalid and stopped. This will is basically the same as the first check but the violation is determined by the consul server.

The 3rd line is the number of consecutive failures the watch will make before giving up (with an exponential backoff). By default this is set to infinity, so unless you're setting maxAttempts this shouldn't affect you.

You can listen for the end event and restart the watch if you think these checks don't apply to your use case.