Taymindis/nginx-link-function

how to make an async call before proxy to another server?

Taymindis opened this issue · 1 comments

Top picked emailed question:

I have a bunch of requests need to call at async and proxy the result to another server How can I do that?

For this answer:

Currently link function module subrequest is only supporting sequential call (sync), and it is still under beta version.

I will recommend you try to use aio threads to pass request into your apps layer, and make async(future) call in your apps. After that add the result into headers input for proxy request purpose.

Assume that you are using c++ application linking, there are many async http client in c/c++. such as
Boost.Asio
backcurl
restclient-cpp.

void bunchOfRequests(ngx_link_func_ctx_t* ctx) {
   //  ( "Async calling");
	http.AsyncCall(.........);
 // Request done, add to header for next request
   ngx_link_func_add_header_in(ctx, "myResult", strlen("myResult"), result->data, result->len);
   ngx_link_func_add_header_in(ctx, "myResult2", strlen("myResult2"), result2->data, result2->len);
   ngx_link_func_add_header_in(ctx, "myResult3", strlen("myResult3"), result3->data, result3->len);

  //   if you are proxy route to another, do not make response content, that's all !!
}
aio threads=myThreadPools;
location /oneBunchRequestAndRoute {
 ngx_link_func_call 'bunchOfRequests';  
 proxy_pass http://xxxxxxxxxx;
}