1

I am making a call limiter based on api key and I am keeping counter of calls in redis where key is my api-key and value is a counter. After threshold or allowed call for that key is reached I throw a message stating your quota reached or something.It works fine and response is also quick, but the problem is I cannot access response status of api-server so even if the response is 500 , the call counter gets increased. Now, what I need to do is, increase counter only if I get status code 2.x.x , but I can only capture response ngx.status in body_filter_by_lua_file but in this directive I cannot make connection to redis or postgres. It throws failed to run body_filter_by_lua*: attempt to yield across C-call boundary error , I can, however connect to redis in content_by_lua_file but cannot access response status. Hope I made it clear.

PS: I am using latest openresty and lua-resty-redis

Saurab
  • 1,931
  • 5
  • 20
  • 33

1 Answers1

0

One workaround I can suggest is to move the code that connects to Redis. to a separated function and schedule this function with ngx.timer.at.

It works for me, but with one disadvantage compared to using ngx.shared.DICT. Of course, the next request may come before the Redis data has been updated in the timer.

Bill
  • 409
  • 1
  • 6
  • 6