openresty / echo-nginx-module

An Nginx module for bringing the power of "echo", "sleep", "time" and more to Nginx's config file
http://wiki.nginx.org/NginxHttpEchoModule
BSD 2-Clause "Simplified" License
1.17k stars 255 forks source link

How to use echo to delay a request before passing it via proxy_pass ? #60

Closed Fr3DBr closed 7 years ago

Fr3DBr commented 7 years ago

I've tried this:

          location /proxy_pass {
          proxy_pass http://svr_backend;
          include proxy.inc;
          }

          location / {
          echo_sleep 5.0;
          echo_location /proxy_pass;
          }

The problem is that I always get a "file being downloaded" when any request is performed, instead of the website being show in the browser when echo module is not used... ?

What I want to achieve, is unless the client wait 5 seconds, the request won't be processed by the upstream server.

agentzh commented 7 years ago

@Fr3DBr In your case, the upstream request won't be sent until 5 sec is elapsed. Your web browser's "file being downloaded" is just its best guess and it's definitely untrue for your case here.

agentzh commented 7 years ago

@Fr3DBr BTW, a much more efficient way of delaying your upstream request is using ngx_lua's access_by_lua_block directive:

location / {
    access_by_lua_block { ngx.sleep(5) }
    proxy_pass http://svr_backend;
    include proxy.inc;
}

It's much much more efficient than your current echo_sleep + echo_location combination, especially for large upstream responses.

agentzh commented 7 years ago

BTW, sorry for the delay on my side.

Fr3DBr commented 7 years ago

Hey @agentzh but, will this stop/pause any other requests while sleeping (parallelism) ?

agentzh commented 7 years ago

@Fr3DBr No, it's nonblocking. Read the documentation.

Fr3DBr commented 7 years ago

@agentzh Ah, then this is fine, also do you have a way to detect in lua, if a connection was closed or not ? Or in case this is closed by the client, will it abort the current code execution just fine ?

agentzh commented 7 years ago

@Fr3DBr See

https://github.com/openresty/lua-nginx-module/#lua_check_client_abort

Read the document.

Fr3DBr commented 7 years ago

@agentzh Thank you very much. 👍

agentzh commented 7 years ago

@Fr3DBr BTW, you may find the lua-resty-limit-traffic library useful:

https://github.com/openresty/lua-resty-limit-traffic

It's shipped with OpenResty by default.