S
S
Sergey Sokolov2021-11-14 15:34:12
Nginx
Sergey Sokolov, 2021-11-14 15:34:12

How to pass off asynchrony as synchronous under NGINX?

Web service, NGINX, php-fpm - generating, for example, pictures.
Infrequent requests to /createcreate an asynchronous task that falls into an (empty) queue and is quickly executed by a separate worker (1 or more).

In a normal scenario, the request instantly returns the issue number. And then by another method /result, you can request the result of this task - get the answer "not ready yet" or get the URL of the generated image.

How would it be possible in such a scenario to delay the instant response to a certain timeout, say, 500ms. If at the end of this time the task has already been completed, immediately return its result (URL). And if still not, then the task number for subsequent requests for the result?

So far I've only thought about this possibility. There is an "echo" module for NGINX that allows you to "sleep" without blocking N seconds before sending a response with the echo_sleep directive . Maybe I'll try it. Has anyone done similar mechanics?

Ideally, of course, do not sleep for exactly 500ms, but when the task is ready, somehow return the response through the event.

How would NGINX have to accept the request and immediately pass it to php-fpm, but then not immediately give a response, but wait (for an event or timeout) and return the response from another url.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2021-11-14
@karabanov

Nginx has nothing to do with it, this functionality must be implemented in the application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question