A question about the polling methods
Dmitry1987 opened this issue · 4 comments
Hey I thought I'll ask here because you've invited us all here but here's a bit quiet in the issues! :D
So I'll be the first.
I'm trying to create a K8s operator, that will poll the key-value in consul. But the code listed on homepage seems like an infinite loop, I mean this one:
# poll a key for updates
index = None
while True:
index, data = c.kv.get('foo', index=index)
print data['Value']
How does it work? If I run such thing, it will query my Consul cluster a million times per second or it has some underlying magic going on? (would that "index/data" pair be received only once per modification in Consul itself, and the rest of time it will just sleep on that line waiting for next event somehow?)
https://www.consul.io/api/kv.html
this index is last index that modified this key. This index corresponds to the X-Consul-Index header value that is returned in responses, and it can be used to establish blocking queries by setting the ?index query parameter. You can even perform blocking queries against entire subtrees of the KV store: if ?recurse is provided, the returned X-Consul-Index corresponds to the latest ModifyIndex within the prefix, and a blocking query using that ?index will wait until any key within that prefix is updated.
also you can use like :
index = None
while True:
index, data = c.kv.get('foo', index=index, wait='5s')
print data['Value']
waiting 5s for next index
@Dmitry1987 thankyou give me first issues
oh, I see.. so if I want to poll consul for KV changes all the time (like consul-template does for example) I need to create such infinite loop in another thread, and set the 'wait' interval, so it'll poll each N seconds basically?
Thanks for the explanation, that was my issue :) I was unable to understand this method based on the docs.
So until the index doesn't change - this query will return the same data each time? Which means I need additional app logic, to compare whether the index I got is same as before or not, and by that decide to render the received data or not?
Q1: oh, I see.. so if I want to poll consul for KV changes all the time (like consul-template does for example) I need to create such infinite loop in another thread, and set the 'wait' interval, so it'll poll each N seconds basically?
A1: yes
Q2: So until the index doesn't change - this query will return the same data each time? Which means I need additional app logic, to compare whether the index I got is same as before or not, and by that decide to render the received data or not?
A2: yes
If you're pursuing performance you can judge index。 if not, you can pull it every times, index is None every time you pull the current one
you can see the implementation of poll and fetch on consul-template