Hello,
I am writing an app which requests data from a REST API multiple times a
second. At the moment I am testing in a loop which collects data, sleeps until
the next .1 of second and then continues collecting data.
Below is some slightly modified, abbreviated and cleaned up code which does
what I want.
It works fine most of the time. I am wanting to make the code robust against
network failures.
I am not sure how to do that. When running it on my laptop I will disconnect my
wifi. It produces no output while the wifi is disconnected. I get no output
from the request which as to be expected. But I also get no errors. It just
sits idle. I don't like that. I can imagine there is some code built into
HttpClient that also attempts to be robust. However, I do not know how or when
it will fail.
If there is network failure I want to be able to resume as soon as it is back
up.
The app sometime seems to just go comatose. Even after the network is back up,
it does not resume, nor give any kind of exceptions. Which is why I wrapped
this proc in a try/except and put in many echo statments. How do I debug this
when disconnected? How do I make this robust to failure where I can resume
immediately after any sort of network failure when the network returns?
Any help and wisdom greatly appreciated.
Thanks.
var client = newHttpClient()
proc secondLoop() =
try:
dt = getTime().utc
let pathQuery = "my path and query string"
echo("before secondLoop1: ", getTime().utc)
let result = client.get(pathQuery)
echo("in secondLoop2: ", getTime().utc)
echo("print some of the result")
let diff = getTime().utc - dt
if diff < deciSecondDuration:
sleep(diff.milliseconds.int)
echo("after secondLoop3: ", getTime().utc)
except:
echo("exception message: ", getCurrentExceptionMsg())
Run