T
T
Toopie2018-08-01 05:27:44
Nginx
Toopie, 2018-08-01 05:27:44

The wait() function does not work in Lua on nginx - what to do?

Hello, I decided to write a script that would give back not immediately, but after a while. In other words, I wanted to make a LongPoll mechanism.
Here is the code:

local key = ngx.md5(ngx.var.remote_addr)
local time = ngx.req.start_time
local json = require('json')
local memcached = require('resty.memcached')
local cache = memcached:new()
local ok, error = cache:connect('127.0.0.1', 11211)

local method = ngx.var.request_method

if (method == 'GET') then
    local args = ngx.req.get_uri_args()
    local wait = tonumber(args['wait']) or 1

    for i = 1, wait do
        wait(1000)
    end
end

ngx.say(wait)

But it throws an error:
[error] 18984#18984: *45 lua entry thread aborted: runtime error: /var/www/html/init.lua:15: attempt to call local 'wait' (a number value)
stack traceback:
coroutine 0:
  /var/www/html/init.lua: in function /www/html/init.lua:1>, client: 46.61.242.35, server: _, request: "GET /lua_init HTTP/1.1", host: "185.159.129.71"

Memcached and JSON are connected here, but they are not used, however, they are needed for further development.
What could be the problem? I'm guessing that in Memcached - maybe it has a work limit, for example, 0.04 seconds? Maybe bullshit, maybe not...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tsvetkov, 2018-08-01
@Toopie

Name your local variable wait something else, it looks like it overrides the global one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question