jonathonmcmurray / reQ

HTTP requests library in kdb+/q
http://jmcmurray.co.uk/reQ/
MIT License
38 stars 9 forks source link

Add support for timeouts #16

Open jonathonmcmurray opened 5 years ago

jonathonmcmurray commented 5 years ago

Most HTTP clients implement timeout functionality for slow responding servers. In q, the -T cmd line arg or \T command can set a timeout for remotely executed commands, which we can use to timeout a local command by sending it to ourselves over handle 0 e.g.

q)\T 1
q)0(`.req.g;"http://httpbin.org/delay/10")
'stop
  [2]  /home/jmcmurray/miniconda3/q/packages/req/req.q:164: .req.send:
  d:headers[us;pr;hd;p];                                                            //get dictionary of HTTP headers for request
  r:hs d:buildquery[m;pr;nu;h;d;p];                                                 //build query and execute
    ^
  if[v;-1"-- REQUEST --\n",string[hs],"\n",d];                                      //if verbose, log request
q.req))

As an initial version, I'll implement a wrapper that

(i) sets timeout (ii) performs request (with error trap) over handle 0 (iii) resets timeout (iv) signals if timeout was hit

Future rewrites may allow a more sophisticated approach.

jonathonmcmurray commented 5 years ago

Needs to be added to documentation as well

n1koo commented 5 years ago

@jonathonmcmurray did you get this going in https://github.com/jonathonmcmurray/reQ/commit/8c9305446c5fcb859dfbefd825c30ef20b8ce1fa ? Any blockers? Would be highly interested in seeing this ship

jonathonmcmurray commented 5 years ago

@n1koo yes, initial version of this is in 8c93054 - haven't had time to work on this further, or document it properly but in essence args are similar to http://jmcmurray.co.uk/reQ/internal/#reqsend except added "timeout" arg at start (& no "verbose" flag at end)

Note that this is a pretty hacky workaround using the 0 handle, which will probably break on any process using slaves.

I don't plan on making a new "release" in the short term (there are a few other features I want in place before next release), but the changes are on master branch if you wish to use that?

n1koo commented 5 years ago

Perfect, will give it a go 👍