leandromoreira / nginx-lua-redis-rate-measuring

A lua library to provide distributed rate measurement using nginx + redis, you can use it to do a throttling system within many nodes.
BSD 3-Clause "New" or "Revised" License
150 stars 16 forks source link

attempt to index local 'slot_item' (a nil value) #25

Open pzystorm opened 5 years ago

pzystorm commented 5 years ago

Hi I have setup everything as described. I call the website f.ex. like this: www.foo.de/ratechecker?token=foo Inside nginx i have defined the location /ratechecker with redis like your example:

        location /ratechecker {
          default_type 'text/plain';
          content_by_lua_block {
            local redis_client = redis_cluster:new(config)
            local rate, err = redis_rate.measure(redis_client, ngx.var.arg_token)
            if err then
              ngx.log(ngx.ERR, "err: ", err)
              ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
            end
            if rate > 10 then
              ngx.exit(ngx.HTTP_FORBIDDEN)
            end
            ngx.say(rate)
          }
        }

Now I get the error

019/05/29 16:42:34 [error] 10383#10383: *26 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/resty-redis-cluster.lua:506: attempt to index local 'slot_item' (a nil value)
stack traceback:
coroutine 0:
        /usr/local/openresty/lualib/resty-redis-cluster.lua: in function 'commit_pipeline'
        /usr/local/openresty/lualib/resty-redis-rate.lua:19: in function 'measure'
        content_by_lua(nginx.conf:160):3: in main chunk, client: 10.51.21.110, server: foo.de, request: "GET /ratechecker?token=foo HTTP/1.1", host: "foo.de:8111"

Line 506 is this last line of this code passage:

        _reqs[i].origin_index = i
        local key = _reqs[i].key
        local slot = redis_slot(tostring(key))
        local slot_item = slots[slot]

        local ip, port, slave, err = pick_node(self, slot_item.serv_list, slot, magicRandomPickupSeed)

Between the empty line I have inserted this:

        ngx.say("ok " .. slot .. " ok " .. key)
        ngx.exit(200)

It outputs: ok 12182 ok ngx_rate_measuring_{foo}_20 But slot_item is nil...

Can someone help me? Whats wrong?

leandromoreira commented 5 years ago

Hi there @pzystorm how are you? are you using redis cluster?

Did you allocated memory for the slots:

 lua_shared_dict redis_cluster_slot_locks 100k;

Can you also make it work through the docker-compose we provided? If yes then it might be related to something missing in your nignx.conf you can look at the example to see what's missing.