Forum OpenACS Q&A: Re: Res: Re: http requests with XoTCL

Collapse
Posted by Tom Jackson on

There is an undocumented API in AOLserver 4.5 [ns_http]. Below is an example of use, although a poor one. I don't think that the code in AOLserver is complete, but I'll describe how it works.

set timeout 20 
set method GET
set body ""
set headerSet [ns_set create]

set id [ns_http queue \
           -timeout $timeout\
           -method $method \
           -body $body \
           -headers $headerSet]

set re [ns_http wait \
            -elapsed elapsedVar \
            -result resultVar \
            -headers headersSetVar \
            -status statusVar \
            $id]

There are a few other commands (cancel and cleanup), but how to use this? Connection threads can queue requests and another worker thread or threads can pull from the list and service them. For instance, nsv_lappend could be used to post the ids and another set of worker threads could pull from the list. If the 'wait' is not successful, the task is not removed from the queue, so it could be canceled, or tried again later. Another nsv could track the number of tries. Also the worker thread could cancel and rewrite the request to extend wait timeout. The worker thread/s could also just be a scheduled proc. The proc could even reschedule itself so you never have two running at once, or you could chain scheduled procs. One master could loop over the list and distribute the load in some way.

But unless you use multiple worker threads this isn't async, except that you can pass off the work to something other than a connection thread, and you can process more than one request per thread, but they are handled in series.

set url http://192.168.1.102:8001/runproc
for {set i 0} {$i < 10} {incr i} {
    if {$i} {
	set action queue
    } else {
	set action run
    }
    set result$i [ns_http $action -timeout 200 http://192.168.1.102:8001/runproc]
    ns_log Notice "queued [set result$i]"
}

for {set i 0} {$i < 10} {incr i} {
    ns_log Notice "waiting for [set result$i]"
    set data$i [ns_http wait [set result$i]]
    ns_log Notice "finished waiting for [set result$i]"
}

for {set i 0} {$i < 10} {incr i} {
    append allData "data$i ([set result$i] = '[set data$i]'\n"
}

ns_return 200 text/plain $allData