I try to implement the rate limit 2 pattern from https://redis.io/commands/INCR#pattern-rate-limiter-2.
local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect("unix:/run/redis.sock")
if not ok then
ngx.say("failed to connect: ", err)
return
end
red.eval('local current = redis.call("incr",KEYS[1]) if current == 1 then redis.call("expire",KEYS[1],100) end', 1,
'foobar')
What's wrong with the syntax of my red.eval statement? In the error.log I see following line:
2022/03/12 15:11:46 [error] 55519#0: *55 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:334: bad argument #1 to 'rawget' (table expected, got string)
stack traceback:
coroutine 0:
[C]: in function 'rawget'
/usr/local/openresty/lualib/resty/redis.lua:334: in function 'eval'
rewrite_by_lua(nginx.conf:141):13: in main chunk, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost", referrer: "http://localhost/"
Any ideas?
P.S. the lua part inside the eval is working as running it directly on redis CLI works as expected
redis /run/redis.sock> EVAL 'local current = redis.call("incr",KEYS[1]) if current == 1 then redis.call("expire",KEYS[1],100) end' 1 foobar