I have a custom plugin that needs to make an outside request to an http based service. I would like this to run asynchronously to not block the request going to the upstream service, and then check the results in the header_filter phase before sending results to the client
This works how I want it, the light thread makes the request and yields, the upstream service is called, and the response handler is called with both the result from the thread and upstream result.
function plugin:access(plugin_conf)
local bg_check_handle = ngx.thread.spawn(function()
local result = httpCallout(plugin_conf, body)
kong.log.warn("Done with request")
end)
end
function plugin:response(plugin_conf)
doSomethingWithResults()
end
Unfortunately, this requires the response to buffer the entire upstream response body before sending back to the client. I would like to instead use the header_filter phase like so
function plugin:access(plugin_conf)
local bg_check_handle = ngx.thread.spawn(function()
local result = httpCallout(plugin_conf, body)
kong.log.warn("Done with request")
end)
end
function plugin:header_filter(plugin_conf)
doSomethingWithResults()
end
But this does not work, as the Nginx worker waits for all light threads to finish in the access phase before sending to upstream. Is there a way to force the access phase to complete while the child light thread is running? I am aware that header_filter does not allow access to the semaphore api and yielding if results are not available yet will not be possible.