Giter Club home page Giter Club logo

lua-resty-redis's Introduction

Name

lua-resty-redis - Lua redis client driver for the ngx_lua based on the cosocket API

Table of Contents

Status

This library is considered production ready.

Description

This Lua library is a Redis client driver for the ngx_lua nginx module:

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

This Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior.

Note that at least ngx_lua 0.5.14 or OpenResty 1.2.1.14 is required.

Synopsis

    # you do not need the following line if you are using
    # the OpenResty bundle:
    lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";

    server {
        location /test {
            # need to specify the resolver to resolve the hostname
            resolver 8.8.8.8;

            content_by_lua_block {
                local redis = require "resty.redis"
                local red = redis:new()

                red:set_timeouts(1000, 1000, 1000) -- 1 sec

                -- or connect to a unix domain socket file listened
                -- by a redis server:
                --     local ok, err = red:connect("unix:/path/to/redis.sock")

                -- connect via ip address directly
                local ok, err = red:connect("127.0.0.1", 6379)

                -- or connect via hostname, need to specify resolver just like above
                local ok, err = red:connect("redis.openresty.com", 6379)

                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

                ok, err = red:set("dog", "an animal")
                if not ok then
                    ngx.say("failed to set dog: ", err)
                    return
                end

                ngx.say("set result: ", ok)

                local res, err = red:get("dog")
                if not res then
                    ngx.say("failed to get dog: ", err)
                    return
                end

                if res == ngx.null then
                    ngx.say("dog not found.")
                    return
                end

                ngx.say("dog: ", res)

                red:init_pipeline()
                red:set("cat", "Marry")
                red:set("horse", "Bob")
                red:get("cat")
                red:get("horse")
                local results, err = red:commit_pipeline()
                if not results then
                    ngx.say("failed to commit the pipelined requests: ", err)
                    return
                end

                for i, res in ipairs(results) do
                    if type(res) == "table" then
                        if res[1] == false then
                            ngx.say("failed to run command ", i, ": ", res[2])
                        else
                            -- process the table value
                        end
                    else
                        -- process the scalar value
                    end
                end

                -- put it into the connection pool of size 100,
                -- with 10 seconds max idle time
                local ok, err = red:set_keepalive(10000, 100)
                if not ok then
                    ngx.say("failed to set keepalive: ", err)
                    return
                end

                -- or just close the connection right away:
                -- local ok, err = red:close()
                -- if not ok then
                --     ngx.say("failed to close: ", err)
                --     return
                -- end
            }
        }
    }

Back to TOC

Methods

All of the Redis commands have their own methods with the same name except all in lower case.

You can find the complete list of Redis commands here:

http://redis.io/commands

You need to check out this Redis command reference to see what Redis command accepts what arguments.

The Redis command arguments can be directly fed into the corresponding method call. For example, the "GET" redis command accepts a single key argument, then you can just call the "get" method like this:

    local res, err = red:get("key")

Similarly, the "LRANGE" redis command accepts three arguments, then you should call the "lrange" method like this:

    local res, err = red:lrange("nokey", 0, 1)

For example, "SET", "GET", "LRANGE", and "BLPOP" commands correspond to the methods "set", "get", "lrange", and "blpop".

Here are some more examples:

    -- HMGET myhash field1 field2 nofield
    local res, err = red:hmget("myhash", "field1", "field2", "nofield")
    -- HMSET myhash field1 "Hello" field2 "World"
    local res, err = red:hmset("myhash", "field1", "Hello", "field2", "World")

All these command methods returns a single result in success and nil otherwise. In case of errors or failures, it will also return a second value which is a string describing the error.

A Redis "status reply" results in a string typed return value with the "+" prefix stripped.

A Redis "integer reply" results in a Lua number typed return value.

A Redis "error reply" results in a false value and a string describing the error.

A non-nil Redis "bulk reply" results in a Lua string as the return value. A nil bulk reply results in a ngx.null return value.

A non-nil Redis "multi-bulk reply" results in a Lua table holding all the composing values (if any). If any of the composing value is a valid redis error value, then it will be a two element table {false, err}.

A nil multi-bulk reply returns in a ngx.null value.

See http://redis.io/topics/protocol for details regarding various Redis reply types.

In addition to all those redis command methods, the following methods are also provided:

Back to TOC

new

syntax: red, err = redis:new()

Creates a redis object. In case of failures, returns nil and a string describing the error.

Back to TOC

connect

syntax: ok, err = red:connect(host, port, options_table?)

syntax: ok, err = red:connect("unix:/path/to/unix.sock", options_table?)

Attempts to connect to the remote host and port that the redis server is listening to or a local unix domain socket file listened by the redis server.

Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method.

The optional options_table argument is a Lua table holding the following keys:

  • ssl

    If set to true, then uses SSL to connect to redis (defaults to false).

  • ssl_verify

    If set to true, then verifies the validity of the server SSL certificate (defaults to false). Note that you need to configure the lua_ssl_trusted_certificate to specify the CA (or server) certificate used by your redis server. You may also need to configure lua_ssl_verify_depth accordingly.

  • server_name

    Specifies the server name for the new TLS extension Server Name Indication (SNI) when connecting over SSL.

  • pool

    Specifies a custom name for the connection pool being used. If omitted, then the connection pool name will be generated from the string template <host>:<port> or <unix-socket-path>.

  • pool_size

    Specifies the size of the connection pool. If omitted and no backlog option was provided, no pool will be created. If omitted but backlog was provided, the pool will be created with a default size equal to the value of the lua_socket_pool_size directive. The connection pool holds up to pool_size alive connections ready to be reused by subsequent calls to connect, but note that there is no upper limit to the total number of opened connections outside of the pool. If you need to restrict the total number of opened connections, specify the backlog option. When the connection pool would exceed its size limit, the least recently used (kept-alive) connection already in the pool will be closed to make room for the current connection. Note that the cosocket connection pool is per Nginx worker process rather than per Nginx server instance, so the size limit specified here also applies to every single Nginx worker process. Also note that the size of the connection pool cannot be changed once it has been created. Note that at least ngx_lua 0.10.14 is required to use this options.

  • backlog

    If specified, this module will limit the total number of opened connections for this pool. No more connections than pool_size can be opened for this pool at any time. If the connection pool is full, subsequent connect operations will be queued into a queue equal to this option's value (the "backlog" queue). If the number of queued connect operations is equal to backlog, subsequent connect operations will fail and return nil plus the error string "too many waiting connect operations". The queued connect operations will be resumed once the number of connections in the pool is less than pool_size. The queued connect operation will abort once they have been queued for more than connect_timeout, controlled by set_timeout, and will return nil plus the error string "timeout". Note that at least ngx_lua 0.10.14 is required to use this options.

Back to TOC

set_timeout

syntax: red:set_timeout(time)

Sets the timeout (in ms) protection for subsequent operations, including the connect method.

Since version v0.28 of this module, it is advised that set_timeouts be used in favor of this method.

Back to TOC

set_timeouts

syntax: red:set_timeouts(connect_timeout, send_timeout, read_timeout)

Respectively sets the connect, send, and read timeout thresholds (in ms), for subsequent socket operations. Setting timeout thresholds with this method offers more granularity than set_timeout. As such, it is preferred to use set_timeouts over set_timeout.

This method was added in the v0.28 release.

Back to TOC

set_keepalive

syntax: ok, err = red:set_keepalive(max_idle_timeout, pool_size)

Puts the current Redis connection immediately into the ngx_lua cosocket connection pool.

You can specify the max idle timeout (in ms) when the connection is in the pool and the maximal size of the pool every nginx worker process.

In case of success, returns 1. In case of errors, returns nil with a string describing the error.

Only call this method in the place you would have called the close method instead. Calling this method will immediately turn the current redis object into the closed state. Any subsequent operations other than connect() on the current object will return the closed error.

Back to TOC

get_reused_times

syntax: times, err = red:get_reused_times()

This method returns the (successfully) reused times for the current connection. In case of error, it returns nil and a string describing the error.

If the current connection does not come from the built-in connection pool, then this method always returns 0, that is, the connection has never been reused (yet). If the connection comes from the connection pool, then the return value is always non-zero. So this method can also be used to determine if the current connection comes from the pool.

Back to TOC

close

syntax: ok, err = red:close()

Closes the current redis connection and returns the status.

In case of success, returns 1. In case of errors, returns nil with a string describing the error.

Back to TOC

init_pipeline

syntax: red:init_pipeline()

syntax: red:init_pipeline(n)

Enable the redis pipelining mode. All subsequent calls to Redis command methods will automatically get cached and will send to the server in one run when the commit_pipeline method is called or get cancelled by calling the cancel_pipeline method.

This method always succeeds.

If the redis object is already in the Redis pipelining mode, then calling this method will discard existing cached Redis queries.

The optional n argument specifies the (approximate) number of commands that are going to add to this pipeline, which can make things a little faster.

Back to TOC

commit_pipeline

syntax: results, err = red:commit_pipeline()

Quits the pipelining mode by committing all the cached Redis queries to the remote server in a single run. All the replies for these queries will be collected automatically and are returned as if a big multi-bulk reply at the highest level.

This method returns nil and a Lua string describing the error upon failures.

Back to TOC

cancel_pipeline

syntax: red:cancel_pipeline()

Quits the pipelining mode by discarding all existing cached Redis commands since the last call to the init_pipeline method.

This method always succeeds.

If the redis object is not in the Redis pipelining mode, then this method is a no-op.

Back to TOC

hmset

syntax: res, err = red:hmset(myhash, field1, value1, field2, value2, ...)

syntax: res, err = red:hmset(myhash, { field1 = value1, field2 = value2, ... })

Special wrapper for the Redis "hmset" command.

When there are only three arguments (including the "red" object itself), then the last argument must be a Lua table holding all the field/value pairs.

Back to TOC

array_to_hash

syntax: hash = red:array_to_hash(array)

Auxiliary function that converts an array-like Lua table into a hash-like table.

This method was first introduced in the v0.11 release.

Back to TOC

read_reply

syntax: res, err = red:read_reply()

Reading a reply from the redis server. This method is mostly useful for the Redis Pub/Sub API, for example,

    local cjson = require "cjson"
    local redis = require "resty.redis"

    local red = redis:new()
    local red2 = redis:new()

    red:set_timeouts(1000, 1000, 1000) -- 1 sec
    red2:set_timeouts(1000, 1000, 1000) -- 1 sec

    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("1: failed to connect: ", err)
        return
    end

    ok, err = red2:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("2: failed to connect: ", err)
        return
    end

    local res, err = red:subscribe("dog")
    if not res then
        ngx.say("1: failed to subscribe: ", err)
        return
    end

    ngx.say("1: subscribe: ", cjson.encode(res))

    res, err = red2:publish("dog", "Hello")
    if not res then
        ngx.say("2: failed to publish: ", err)
        return
    end

    ngx.say("2: publish: ", cjson.encode(res))

    res, err = red:read_reply()
    if not res then
        ngx.say("1: failed to read reply: ", err)
        return
    end

    ngx.say("1: receive: ", cjson.encode(res))

    red:close()
    red2:close()

Running this example gives the output like this:

1: subscribe: ["subscribe","dog",1]
2: publish: 1
1: receive: ["message","dog","Hello"]

The following class methods are provieded:

Back to TOC

add_commands

syntax: hash = redis.add_commands(cmd_name1, cmd_name2, ...)

WARNING this method is now deprecated since we already do automatic Lua method generation for any redis commands the user attempts to use and thus we no longer need this.

Adds new redis commands to the resty.redis class. Here is an example:

    local redis = require "resty.redis"

    redis.add_commands("foo", "bar")

    local red = redis:new()

    red:set_timeouts(1000, 1000, 1000) -- 1 sec

    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    local res, err = red:foo("a")
    if not res then
        ngx.say("failed to foo: ", err)
    end

    res, err = red:bar()
    if not res then
        ngx.say("failed to bar: ", err)
    end

Back to TOC

Redis Authentication

Redis uses the AUTH command to do authentication: http://redis.io/commands/auth

There is nothing special for this command as compared to other Redis commands like GET and SET. So one can just invoke the auth method on your resty.redis instance. Here is an example:

    local redis = require "resty.redis"
    local red = redis:new()

    red:set_timeouts(1000, 1000, 1000) -- 1 sec

    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    local res, err = red:auth("foobared")
    if not res then
        ngx.say("failed to authenticate: ", err)
        return
    end

where we assume that the Redis server is configured with the password foobared in the redis.conf file:

requirepass foobared

If the password specified is wrong, then the sample above will output the following to the HTTP client:

failed to authenticate: ERR invalid password

Back to TOC

Redis Transactions

This library supports the Redis transactions. Here is an example:

    local cjson = require "cjson"
    local redis = require "resty.redis"
    local red = redis:new()

    red:set_timeouts(1000, 1000, 1000) -- 1 sec

    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    local ok, err = red:multi()
    if not ok then
        ngx.say("failed to run multi: ", err)
        return
    end
    ngx.say("multi ans: ", cjson.encode(ok))

    local ans, err = red:set("a", "abc")
    if not ans then
        ngx.say("failed to run sort: ", err)
        return
    end
    ngx.say("set ans: ", cjson.encode(ans))

    local ans, err = red:lpop("a")
    if not ans then
        ngx.say("failed to run sort: ", err)
        return
    end
    ngx.say("set ans: ", cjson.encode(ans))

    ans, err = red:exec()
    ngx.say("exec ans: ", cjson.encode(ans))

    red:close()

Then the output will be

multi ans: "OK"
set ans: "QUEUED"
set ans: "QUEUED"
exec ans: ["OK",[false,"ERR Operation against a key holding the wrong kind of value"]]

Back to TOC

Redis Module

This library supports the Redis module. Here is an example with RedisBloom module:

    local cjson = require "cjson"
    local redis = require "resty.redis"
    -- register the module prefix "bf" for RedisBloom
    redis.register_module_prefix("bf")

    local red = redis:new()

    local ok, err = red:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("failed to connect: ", err)
        return
    end

    -- call BF.ADD command with the prefix 'bf'
    res, err = red:bf():add("dog", 1)
    if not res then
        ngx.say(err)
        return
    end
    ngx.say("receive: ", cjson.encode(res))

    -- call BF.EXISTS command
    res, err = red:bf():exists("dog")
    if not res then
        ngx.say(err)
        return
    end
    ngx.say("receive: ", cjson.encode(res))

Load Balancing and Failover

You can trivially implement your own Redis load balancing logic yourself in Lua. Just keep a Lua table of all available Redis backend information (like host name and port numbers) and pick one server according to some rule (like round-robin or key-based hashing) from the Lua table at every request. You can keep track of the current rule state in your own Lua module's data, see https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker

Similarly, you can implement automatic failover logic in Lua at great flexibility.

Back to TOC

Debugging

It is usually convenient to use the lua-cjson library to encode the return values of the redis command methods to JSON. For example,

    local cjson = require "cjson"
    ...
    local res, err = red:mget("h1234", "h5678")
    if res then
        print("res: ", cjson.encode(res))
    end

Back to TOC

Automatic Error Logging

By default the underlying ngx_lua module does error logging when socket errors happen. If you are already doing proper error handling in your own Lua code, then you are recommended to disable this automatic error logging by turning off ngx_lua's lua_socket_log_errors directive, that is,

    lua_socket_log_errors off;

Back to TOC

Check List for Issues

  1. Ensure you configure the connection pool size properly in the set_keepalive. Basically if your Redis can handle n concurrent connections and your NGINX has m workers, then the connection pool size should be configured as n/m. For example, if your Redis usually handles 1000 concurrent requests and you have 10 NGINX workers, then the connection pool size should be 100. Similarly if you have p different NGINX instances, then connection pool size should be n/m/p.
  2. Ensure the backlog setting on the Redis side is large enough. For Redis 2.8+, you can directly tune the tcp-backlog parameter in the redis.conf file (and also tune the kernel parameter SOMAXCONN accordingly at least on Linux). You may also want to tune the maxclients parameter in redis.conf.
  3. Ensure you are not using too short timeout setting in the set_timeout or set_timeouts methods. If you have to, try redoing the operation upon timeout and turning off automatic error logging (because you are already doing proper error handling in your own Lua code).
  4. If your NGINX worker processes' CPU usage is very high under load, then the NGINX event loop might be blocked by the CPU computation too much. Try sampling a C-land on-CPU Flame Graph and Lua-land on-CPU Flame Graph for a typical NGINX worker process. You can optimize the CPU-bound things according to these Flame Graphs.
  5. If your NGINX worker processes' CPU usage is very low under load, then the NGINX event loop might be blocked by some blocking system calls (like file IO system calls). You can confirm the issue by running the epoll-loop-blocking-distr tool against a typical NGINX worker process. If it is indeed the case, then you can further sample a C-land off-CPU Flame Graph for a NGINX worker process to analyze the actual blockers.
  6. If your redis-server process is running near 100% CPU usage, then you should consider scale your Redis backend by multiple nodes or use the C-land on-CPU Flame Graph tool to analyze the internal bottlenecks within the Redis server process.

Back to TOC

Limitations

  • This library cannot be used in code contexts like init_by_lua*, set_by_lua*, log_by_lua*, and header_filter_by_lua* where the ngx_lua cosocket API is not available.
  • The resty.redis object instance cannot be stored in a Lua variable at the Lua module level, because it will then be shared by all the concurrent requests handled by the same nginx worker process (see https://github.com/openresty/lua-nginx-module/#data-sharing-within-an-nginx-worker ) and result in bad race conditions when concurrent requests are trying to use the same resty.redis instance (you would see the "bad request" or "socket busy" error to be returned from the method calls). You should always initiate resty.redis objects in function local variables or in the ngx.ctx table. These places all have their own data copies for each request.

Back to TOC

Installation - Build from source

# Clone latest release , assuming v0.29
wget https://github.com/openresty/lua-resty-redis/archive/refs/tags/v0.29.tar.gz

# Extract
tar -xvzf v0.29.tar.gz

# go into directory
cd lua-resty-redis-0.29

export LUA_LIB_DIR=/usr/local/openresty/site/lualib

# Compile and Install
make install

# Now compiled path will be outputted
# /usr/local/lib/lua/resty = lua_package_path in nginx conf

Installation Notes

If you are using the OpenResty bundle (http://openresty.org ), then you do not need to do anything because it already includes and enables lua-resty-redis by default. And you can just use it in your Lua code, as in

    local redis = require "resty.redis"
    ...

If you are using your own nginx + ngx_lua build, then you need to configure the lua_package_path directive to add the path of your lua-resty-redis source tree to ngx_lua's LUA_PATH search path, as in

    # nginx.conf
    http {
        lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";
        ...
    }

Ensure that the system account running your Nginx ''worker'' proceses have enough permission to read the .lua file.

Back to TOC

TODO

Back to TOC

Community

Back to TOC

English Mailing List

The openresty-en mailing list is for English speakers.

Back to TOC

Chinese Mailing List

The openresty mailing list is for Chinese speakers.

Back to TOC

Bugs and Patches

Please report bugs or submit patches by

  1. creating a ticket on the GitHub Issue Tracker,
  2. or posting to the OpenResty community.

Back to TOC

Author

Yichun "agentzh" Zhang (章亦春) [email protected], OpenResty Inc.

Back to TOC

Copyright and License

This module is licensed under the BSD license.

Copyright (C) 2012-2017, by Yichun Zhang (agentzh) [email protected], OpenResty Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Back to TOC

See Also

Back to TOC

lua-resty-redis's People

Contributors

ad7six avatar agentzh avatar bhanuvrat avatar bjoe2k4 avatar bungle avatar chipitsine avatar chronolaw avatar cppcoffee avatar doujiang24 avatar elvinefendi avatar janithcooray avatar moonming avatar nmbakfm avatar oycw avatar php-cpm avatar pintsized avatar rainingmaster avatar runnningpig avatar shaneutt avatar spacewander avatar stone-wind avatar thibaultcha avatar vinayakhulawale avatar xiaocang avatar zhuizhuhaomeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-resty-redis's Issues

"attempt to yield across C-call boundary" occur while connect()

I package redis functions into a module testdb.lua, and include it in another module testm.lua.

testm.lua is required in test.lua, which is running by ngx_lua:

content_by_lua_file luas/test.lua;

Finally, the error occur:

2012/04/07 22:10:32 [error] 24794#0: *185113 1, client: 127.0.0.1, server: localhost, request: "GET /test HTTP/1.1"
2012/04/07 22:10:32 [error] 24794#0: *185113 lua handler aborted: runtime error: attempt to yield across C-call boundary, client: 127.0.0.1, server: localhost, request: "GET /test HTTP/1.1"

The first ngx.log() is excuted, but second not. So is there anything wrong in redis:connect()?

All the files could be found in: https://gist.github.com/2329313

thanks.

How to install it via luarocks?

I'm tring install this lib via luarocks. It desn't work. But i see lots of lib named 'lua-resty-*'. Why not include this? I'm sorry my english is poor.

Share redis instance between workers

I'm trying to share the same Redis client between workers:

 --- script/red.lua
 local redis = require "resty.redis"
 local red = redis:new()
 local host = "127.0.0.1"
 local port = 6379

 red:set_timeout(1000) -- 1 sec

 local ok, err = red:connect(host, port)
 if not ok then
     ngx.exit(ngx.HTTP_FORBIDDEN)
     return
 end
 return red
--- script/auth.lua
local red = require "script.red"

However it throws the following error. Am I doing something wrong? Is this impossible to achieve?

 2014/01/17 00:05:34 [error] 7716#0: *1 lua entry thread aborted: runtime error: attempt to yield across C-call boundary
 stack traceback:
 coroutine 0:
     [C]: in function 'require'
     /home/radu/stor/script/auth.lua:2: in function </home/radu/stor/script/auth.lua:1>, client: 127.0.0.1, server: , request: "GET /nolua?                              AUTH_TOKEN=AAAAAAAAAAAAAAAA_BBBBBBBBBBBBBBBB_MGMGMGMGMGMGMGMG HTTP/1.1", host: "localhost:8888"

cannot connect while providing hostname

Given the following code:

local redis = require "resty.redis"

REDIS_HOST = "127.0.0.1"
REDIS_PORT = 6379

redis_client = redis:new()

local ok, err = redis_client:connect(REDIS_HOST, REDIS_PORT)

If REDIS_HOST is "localhost" instead of 127.0.0.1, then it failed to connect.

Although, in the scope of localhost vs. 127.0.0.1 it makes no difference, in a real world dynamic production environment, an IP is not likely to be used, rather, a nameserver instead.

使用smembers的问题

当我使用redis的smembers的时候会出现下面的error:

2012/06/27 18:08:57 [error] 10960#0: *2564615 lua handler aborted: runtime error: /data/www/m/script/vendor/resty/redis.lua:132: bad argument #1 to 'receive' (bad pattern argument), client: 172.23.11.103, server: m.openapi.cn, request: "GET /app/index?session=222222250f52c5874fc441a8b2596206db@desktop5c03225e3d1a83f875f173a594d25280 HTTP/1.1", host: "..."

是在调用recieve的时候出错....不知道是什么问题导致...寻帮助...

How to know if is a redis key exits

hi, maybe it's a simple question, but it's a real question of mine and i get nothing from readme about this question. The question is how to know if is a redis key exists.

example:

local key = "not_exists"
local val, err = red:get(key)

if not val then
    ngx.say("the value is not exists.")
else 
    ngx.say("val: "..val)
end

when the key is not exits, I will get a error of [error] 26052#0: *1678 lua entry thread aborted: runtime error: /opt/code/lua/redis.lua:22: attempt to concatenate local 'val' (a userdata value)

I know that it's probably that the value is not exists, so i can not use ngx.say() to print it.
but I do not know how to figure out it with lua code, that I used if not val, if val ~= nil, which neither can not work correctly.

Thank you!

intermittent connection refused

Hi
I'm getting this error intermittently on a busy server:

2013/04/08 10:23:33 [error] 52774#0: *752776 connect() to unix://tmp/redis.sock failed (61: Connection refused), client: 46.14.44.217, server: , request: "GET /css/popup.css?v=3 HTTP/1.1"

I have maxclients 100000 in my redis conf and ulimit is 200000 on the server itself. Requests per second is around 1000 and redis connections is peaking around 3000..

What are the possible causes for this?

Many thanks
Richard

ngx.timer.at - connection failed

The lua-resty-redis module has a problem with connection, when connect() is called in ngx.timer.at callback's function

connect() to unix:/var/lib/redis/redis.sock failed (11: Resource temporarily unavailable), context: ngx.timer

Search logs with grep shows that the problem only occurs in ngx.timer context

grep -o 'context: .*' /var/log/nginx/error.log|sort |uniq -c 
  5701 context: ngx.timer

I've tested it via http_load tool

for i in `seq 0 3`; do ./http_load -proxy 0:80 -parallel 2000 -seconds 20 url 2>/dev/null & done

I'm also using mongo module (https://github.com/bigplum/lua-resty-mongol) the same way I'm using redis module and it works fine.

Simple config example (one i've tested on):
nginx.conf

user nginx nginx;
worker_processes 12;
events {
    worker_connections  2048;
}
pid        /var/run/nginx.pid;
http {
    include       mime.types;
    default_type  text/html;

    keepalive_timeout 0;

    init_by_lua '
        package.path = package.path .. ";/usr/local/nginx/conf/lua/?.lua"
        package.path = package.path .. ";/usr/local/nginx/conf/lua/resty/?.lua"

        redis = require "resty.redis.redis"
        r = require "r"
    ';

    server {
        listen 80 deferred default_server;

        server_name .*;

        resolver 127.0.0.1;

      location / {
            access_by_lua_file '/usr/local/nginx/conf/lua/access.lua';

            proxy_set_header    Host $host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_pass          http://$host$request_uri;

            log_by_lua_file '/usr/local/nginx/conf/lua/log.lua';
        }
    }
}

access.lua

local c = r.open()
if c then
    c:incr('n')
end
r.close(c)

log.lua

function foo(premature)
    if premature then
        return
    end

    local c = r.open()
    if c then
        c:decr('n')
    end
    r.close(c)
end

ngx.timer.at(0, foo)

r.lua

local M = {}

function M.open()
    local c = redis:new()
    c:set_timeout(1000)

    local ok, err = c:connect('unix:/var/lib/redis/redis.sock')
    if not ok then
        ngx.log(ngx.ERR, err)
        return nil 
    end 

    return c
end

function M.close(c)
    if not c then
        return
    end 

    local ok, err = c:set_keepalive(1000*10, 32)
    if not ok then
        c:close()
        ngx.log(ngx.ERR, err)
    end 
end

return M

Redis connection issues

Hi,

I'm doing some redis lookups in my lua script and I'm getting some weird connection problems which I don't know how to properly debug to actually get to the source of the problem.

Here is snip from config

- Connect to Redis
local redis = require "resty.redis"
local red = redis:new()

red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379)

if not ok then
    ngx.log(ngx.ERR, "Redis connection failure: " .. err)
    return
end

- rest of the code ...

On a random basis I get a lot of Redis connection failure: timeout in nginx error log. While debug loging redis server I don't see any connections at that same moment.

I'm using latest lua-resty-redis and 0.8.6 ngx_lua module. I also try to upgrade to 0.9.2 but then I get even more strange errors

2013/11/10 23:24:53 [error] 52514#0: *15 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, 
2013/11/10 23:24:53 [error] 52514#0: *15 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, 
2013/11/10 23:24:53 [error] 52514#0: *15 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, 
2013/11/10 23:24:53 [error] 52514#0: *15 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, 

I don't know if this one is actually related. Is there anything I can try to get more detail info where things actually broke. Server is FreeBSD with pf enabled, nginx 1.5.x.

Regards,

Uros

about hmget & hmset

local res, err hmget(mykey, "one", "two", "three")

then
res.one (not worked)

when mykey not exists,the below code not worked

if (res[2] ~= nil) then
    ngx.say('exists') -- when mykey not exists, this line would output
end

and the type of mykey is userdata, how can i convert it to table?

Bad request in function 'send'

Greetings, I'm absolutely positive this is my fault but I can't figure out what I'm doing wrong.

Seems to be randomly spitting back a "bad request"

I went uber paranoid with error handling and made it not call keepalive unless there were no errors...

hostmap.lua

local module = {}

local REDIS_TIMEOUT   = 250
local REDIS_HOST      = '127.0.0.1'
local REDIS_PORT      = 6379
local REDIS_POOLSIZE  = 200
local REDIS_IDLETIME  = 60
local MAP_EXPIRES     = 3660

function module.init()
    local redis = require "resty.redis"
    module.red = redis:new()
    module.err = false

    module.red:set_timeout(REDIS_TIMEOUT)

    local ok, err = module.red:connect(REDIS_HOST, REDIS_PORT)
    if not ok then
        ngx.log(ngx.ERR, "Absolute failure of redis: " .. err)
        return nil
    end

    return true
end

function module.fetch()
    local res, err = module.red:get(ngx.var.uid_got)
    if not res then
        ngx.log(ngx.ERR, "Failed to get value for key (" .. ngx.var.uid_got .. "): " .. err)
        module.err = true
        return
    end

    if type(res) == "string" then
        ngx.var.httphost = res
ngx.log(ngx.ERR, "Why so bad request? " .. ngx.var.uid_got .. " : " .. MAP_EXPIRES)
        local res, err = module.red:expire(ngx.var.uid_got, MAP_EXPIRES)
        if not res then
            ngx.log(ngx.ERR, "Failed to reset expiry for (" .. ngx.var.uid_got ..") to " .. MAP_EXPIRES .. ": " .. err)
            module.err = true
            return
        end
    end
    return true
end

function module.store()
    local res, err = module.red:setex(ngx.var.uid_got, MAP_EXPIRES, ngx.var.httphost)
    if not res then
        ngx.log(ngx.ERR, "Failed to store value (" .. ngx.var.httphost .. ") for key (" .. ngx.var.uid_got .. "): " .. err)
        module.err = true
        return
    end

    if ngx.var.args then
        ngx.log(ngx.ERR, "Not redirecting...")
        return true
    end

    return "/" .. ngx.var.redirecturl
end

function module.clear()
    local res, err = module.red:del(ngx.var.uid_got)
    if not res then
        ngx.log(ngx.ERR, "Failed to clear value for key (" .. ngx.var.uid_got .. "): " .. err)
        module.err = true
        return
    end
end

function module.pool()
    if module.err then
        ngx.log(ngx.ERR, "Not sending closed connection back to pool")
    else
        module.red:set_keepalive(REDIS_IDLETIME, REDIS_POOLSIZE)
    end
end

return module

select_host.lua

if ngx.var.uid_got then
    local map = require "hostmap"
    if map then
        if map.init() and map.fetch() then
            map.pool()
        end
    end
end

Exception - the "Why so bad request?" lines are me debugging to make sure that values are actually being passed.

2014/05/12 01:39:37 [error] 1002#0: *4 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET / HTTP/1.1", host: "www.yoda.lan"
2014/05/12 01:39:38 [error] 1002#0: *4 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/css_menu.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *4 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/footer.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *24 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /css/jquery-ui-1.7.3.custom.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *23 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /css/jquery.simpledialog.0.1.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *25 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/common-account.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *29 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/content.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *29 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'expire'
    /etc/nginx/conf.d/lua/hostmap.lua:37: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /css/content.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *27 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/odds_and_rules.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *30 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/index.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *38 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /css/dev_server.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *39 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/print.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *40 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/notification.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *41 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /css/navigation-primary.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *42 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /css/navigation-secondary.css HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *30 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery-1.3.2.min.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *39 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery-ui-1.7.3.custom.min.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *66 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery.corner.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *71 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery-placeholder-0.1.min.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *72 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /js/global.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *73 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/notifications.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *74 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/dev_server.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *77 lua entry thread aborted: runtime error: /usr/share/lua/5.1/resty/redis.lua:292: bad request
stack traceback:
coroutine 0:
    [C]: in function 'send'
    /usr/share/lua/5.1/resty/redis.lua:292: in function 'get'
    /etc/nginx/conf.d/lua/hostmap.lua:27: in function 'fetch'
    /etc/nginx/conf.d/lua/select_host.lua:4: in function </etc/nginx/conf.d/lua/select_host.lua:1>, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery.bgiframe.min.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *66 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery.simpledialog.0.1.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"
2014/05/12 01:39:38 [error] 1002#0: *84 [lua] hostmap.lua:36: fetch(): Why so bad request? vid=020011AC320D70539B01419A02030303 : 3660, client: 172.17.42.1, server: www.example.com, request: "GET /js/jquery.qtip-1.0.0-rc3.min.js HTTP/1.1", host: "www.yoda.lan", referrer: "https://www.yoda.lan/"

openresty 1.4.3.6为什么nginx日志中的中文不能正常显示?

您好:
我用set_unescape_uri解出来的变量(中文)放入nginx日志,日志名字为变量名,结果在centos 6中日志名中文显示正常,但是日志中的中文显示是“\xE5\xBA\x94\xE7\x94\xA8\xE7\xB1\xBB\xE5\x9E\x8B”这种,请问该如何解决呢?
备注:我的系统和nginx编码都是utf-8

how to use lua-redis zrangebyscore limit?

lua+redis
i use zrangebysocre,how to use limit?
--local aSetId = instance:zrangebyscore('jsonjjmatch_ware_dict_usually',iTimeLine, iTime)
local aSetId = instance:zrangebyscore('jsonjjmatch_ware_dict_usually', iTimeLine, iTime, {limit={0,10}})
2013/07/04 15:44:20 [error] 19003#0: *2004 lua handler aborted: runtime error: /usr/local/openresty/lualib/resty/redis.lua:196: bad argument #1 to 'len' (string expected, got table), client: 192.168.20.115, server: localhost, request: "GET /get_ware_dict?waretype=1&id=33&wareid= HTTP/1.1", host: "localhost:85"

Could you provide some method about the lua get data from redis?

It prompts error msg “set_redis_keepalive(): Limit Failed to set keepalive: closed" when called set_keepalive after last set_keepalive call.

The function "set_keepalive" call almost next to the last, the code just as below:


function set_redis_keepalive(client)
    local kp_ok, kp_err = client:set_keepalive(0, 100)
    if not kp_ok then
        ngx.log(ngx.ERR, "Limit ", "   Failed to set keepalive: ", kp_err)
    end
end
    local client = redis:new()
    client:set_timeout(1000) -- 1 sec
  ......
    set_redis_keepalive(client)
.....
    set_redis_keepalive(client)

Mixed responses in SSI

I have page with SSI includes,

 <!--# include /widget/user/1 -->
 <!--# include /widget/advert/37884 -->

Problem is nginx randomly mixes ssi content, so in user-block you can see advert and user info in advertisement block =)

ssi catches by location

location /widget/ {
    internal;
    lua_code_cache off;
    content_by_lua_file '/etc/nginx/lua/widget_redis.lua';
    error_page 404 502 500 503 = @generate-widget;
}
redis = require "resty.redis"
red = redis:new()
red:set_timeout(500)

ok, err = red:connect("127.0.0.1", 6379)
if not ok then
    ngx.log(ngx.ERR, "Connection error")
    ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
    return
end

content, err = red:get(ngx.var.uri .. "/")
if not content then
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
    return
end

if content == ngx.null then
    ngx.exit(ngx.HTTP_NOT_FOUND)
    return
end

ngx.header["Content-Type"] = "text/html; charset=utf-8"
ngx.print(content)

If Redis-key /widget/user/1/ exists, lua prints it contents.
Else @generate-widget (php-fpm) render and store it in redis.
All is ok when all widgets rendered by @generate-widget, but when some widgets already stored in redis (and other slowly generated by php) they appears in first ssi blocks.

How can I fix it?

About redis:subscribe

Thanks so much for your great contribution. @agentzh
And there is a problem with redis:subscribe.
Can I subscribe multiple channels by such code?

redis:subscribe('channel:first', 'channel:second')
or
redis:subscribe({'channel:first', 'channel:second'})

ngx.exit(ngx.HTTP_NOT_FOUND) still returns 200

Hi Yichun,

Thanks a lot for all those modules !

I tried to run your example and can't figure how to actually returns a 404 if the key is not found (hence res == null in local res, err = red:get(ngx.var.key_that_does_not_exist).

Snippet:

    if res == ngx.null then
      ngx.status = ngx.HTTP_NOT_FOUND
      ngx.say("key not found.")
      return ngx.status
    end

    ngx.say("result: ", res)

Also tried:

    if res == ngx.null then
      ngx.status = ngx.HTTP_NOT_FOUND
      ngx.say("key not found.")
      ngx.exit(ngx.status)
    end

    ngx.say("result: ", res)

Output:

$> curl -I redis/key_exists
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.7.1
result: "the key exists!"

$> curl -I redis/key_does_not_exist
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.7.1
key not found.

Any idea by any chance ?

lua tcp socket read timed out

Hi,
I have to track a change in a state of user and trigger a http post to another service based on http request from client.

In my request method I am using nginx timer to create a subrequest and using redis pubsub to track state change.

ngx.timer.at(0, subscribe, params.tracker_user_uuid, target_user_uuid)
local function subscribe(premature, user_uuid, channel_id)
  local redis_x = require("redis.redis").new()
  local res, err = redis_x:subscribe("channel" .. channel_id)
  if not res then
      ngx.say("1: failed to subscribe: ", err)
      return
  end
  while true do
    local erver_url = "http://example.com"
    local res, err = redis_x:read_reply()
    if res then
      local req_body = res[3]
      local result, respcode, respheaders, respstatus = http.request({

      })
    end
    redis_x:set_keepalive(250, 200)
  end
end

I see following error every minute in my nginx log.

[error] 9938#0: lua tcp socket read timed out, context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:8080

Is this the best way to do this ?

Does not work in rewrite_by_lua_file

Hello. At first, thanks for your work:)

After this commit 00e493e lua-resty starts yelling

2014/10/29 19:40:21 [error] 25299#0: *2 lua entry thread aborted: runtime error: /usr/local/lib/lua-resty-redis/lib/resty/redis.lua:22: attempt to call local 'new_tab' (a table value)
stack traceback:
coroutine 0:
        [C]: in function 'require'
        /etc/nginx/lua/hosts.wlua:1: in function </etc/nginx/lua/hosts.wlua:1>,

hosts.wlua contains only 'local redis = require "resty.redis"'

The redis command ‘blpop’ can't block the programme

The environment is ngx_openresty/1.4.3.6 on centos 6.5. Version of redis is 2.6.14.
The test code is as follow:

function Testblpop()
    local count=1;
    local msg=nil;
    local err='';
    local redis_handle, err=InitializeRedis(redis_host, redis_port);

    while true
    do
        ngx.log(ngx.NOTICE, string.format("count: %d", count));

        msg, err=redis_handle:blpop("list1", '0');

        ngx.log(ngx.NOTICE, string.format("msg: %s", cjson.encode(msg)));

        count=count+1;

        ngx.sleep(1);
    end

    KeepAliveRedis(redis_handle);
end

I put this test code in content_by_lua_file, and call it using "curl".
If the "list1" is empty, the "blpop" won't block the current programme. The loop will go on, and the msg is "nil".

Is this a bug or by design?

Thanks

Add eval/evalsha abstration

Inspired by https://github.com/FGRibreau/node-redis-lua

Abstract the eval/evalsha so that user does not have to check if script has been loaded or not. Also, allow the scripts to be loaded into the module. Something like:

redis.import_script('my_special_function', string_with_lua_script)
-- later with red is a redis connection
red.scripts['my_special_function'](1, key, arg, arg, arg)

Not sure about the public interface, the above is given as a horrible example.

Thoughts? If it looks like something that would be generally useful, I can write some code to get it started.

Connect to redis using upstream

Intro

Now we are using http_redis2_module and load-balancing read-only connections to the redis cluster with http_upstream_module. But It's annoying to write tons of locations for each kind of request, so we are thinking about migration to lua-resty-redis or another Lua-based redis client, but I am sure that It is not good idea to re-implement load-balancing (including work with failed servers in upstream) in pure Lua.

The question

So, Is it possible to use the library to connect to Redis using upstreams?

Pub/Sub caching last value

Is the following a proper implementation to serve clients the last published value from a Redis PubSub? Or is it possible that it misses some values?

 init_worker_by_lua '
         local delay = 0;
         local handler;
         handler = function()
             local redis = require "resty.redis"
             local red = redis:new()
             local ok, err = red:connect("127.0.0.1", 6379)
 
             local ok, err = red:subscribe("streampic")
             local res, err = red:read_reply()
             if not res then
                 ngx.log(ngx.ERR, "No response", err)
             else
                 local item = res[3]
                 ngx.log(ngx.ERR, "Setting value : ", item)
                 local data = ngx.shared.data
                 data:set("last", item)
             end
             local ok, err = ngx.timer.at(delay, handler)
         end
         local ok, err = ngx.timer.at(delay, handler)
     ';

then just output the global value:

 location  /async {
             keepalive_timeout 0;
             content_by_lua '
                 local data = ngx.shared.data
                 ngx.say(data:get("last"));
            ';
         }

EDIT:

Looks to me that I'm dropping a lot of values. When publishing 100 numbers (1-100) in a loop
I only catch about 5-6.

Pub/Sub with websockets

I'm trying to stream data from a Redis Pub/Sub channel to the websockets. Problem is I don't understand how to wrap the read_reply method. How can I "listen" to the Redis channel in a loop?

The following snippet works, but there's a big delay between server pushes, I think I'm blocking something. Here is my code:

server {
        listen       8881;
        server_name  localhost;

        location /stream {
            lua_socket_log_errors on;
            lua_check_client_abort on;
            content_by_lua '
  
            local redis = require "resty.redis"
            local cjson = require "cjson"
            local red = redis:new()
            red:set_timeout(1000) -- 1 sec
            local ok, err = red:connect("127.0.0.1", 6379)
            if not ok then
                ngx.say("failed to connect: ", err)
                return
            end

            local res, err = red:subscribe("streampic")
            if not res then
                ngx.say("1: failed to subscribe: ", err)
                return
            end
 
            local clients = {}
            local server = require "resty.websocket.server"
            local wb, err = server:new{
                timeout = 5000,
                max_payload_len = 65535
            }
            if not wb then
                ngx.log(ngx.ERR, "failed to new websocket: ", err)
                return ngx.exit(444)
            end
        
            while true do
                local data, typ, err = wb:recv_frame()
                -- Websocket related code block (ping/close etc.)
                --read the pubsub and wire it 
                res, err = red:read_reply()
                if res then
                    local f = io.open(res[3], "rb")
                    if f then
                        local data = f:read("*all")
                        wb:send_binary(data)
                    end
                end
            end
            wb:send_close()';
        }

How to set lua-resty-redis keepalive when using ngx.redirect?

I encountered a problem when I set keepalive to redis while using ngx.redirect. My intent is to keep the connection between nginx and redis always established so that I can reduce the cost on TCP connection while I can do query in redis and judge which sites would be chosen to redirect to. Here is my configuration:

server {
listen 80;
set $lua_country us;

        rewrite_by_lua '

        local  redis      = require "resty.redis"
        local  testdemo   = redis:new()
        local  luacountry = ngx.var.lua_country
        local  luauri     = ngx.var.request_uri

        testdemo:set_timeout(1000)

        local  ok,err = testdemo:connect("127.0.0.1",6379)
        if not ok then
              ngx.say("faild to connect",err)
              return
        end

        local exist = testdemo:exists(luauri)
        if exist==1 then
            local remote_host=testdemo:hget(luauri,luacountry)
            if remote_host then
       --           ngx.say("hello")
                       ngx.redirect("http://"..remote_host..luauri,ngx.HTTP_MOVED_TEMPORARILY)
            else
                 ngx.redirect("http://".."www.google.com.hk",302)
            end
        end
        testdemo:set_keepalive(0,100)
        ';
}

Latency problem with 1k req/sec

I'm using 2.8.17 redis server.
Send requests throw unix.sock by nginx+lua. Each request is one SMEMBERS command. Now I have about 1k req per sec. In the nginx access log I see mostly next

[09/Oct/2014:18:04:49 +0300] [ 1412867089.670 : 0.002 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.670 : 0.002 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.670 : 0.002 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.670 : 0.002 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.671 : 0.001 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.672 : 0.000 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.673 : 0.001 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.673 : 0.001 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.674 : 0.000 : 198 ] 212.1.66.241 - - "POST     /API/0.1/url/getCategory/ HTTP/1.1" 200 37 
[09/Oct/2014:18:04:49 +0300] [ 1412867089.681 : 0.000 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 

You can see that request time is not more than 2ms. But almost every second I have a few requests with request time about 100ms

[09/Oct/2014:18:05:22 +0300] [ 1412867122.843 : 0.131 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:05:23 +0300] [ 1412867123.212 : 0.100 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29 
[09/Oct/2014:18:05:23 +0300] [ 1412867123.613 : 0.101 : 190 ] 212.1.66.241 - - "POST /API/0.1/url/getCategory/ HTTP/1.1" 200 29

I can't find the problem place.
I checked everything from the latency guide
Slowlog has no entries
System is not swapping
I don't use AOF

I read a lot of posts on stackoverflow like this
http://stackoverflow.com/questions/16841469/redis-performance-tuning/23719773#23719773
and I can't find the solution.

redis-benchmark gives me next

redis-benchmark -s /tmp/redis.sock -q -n 10000 -c 1 -d 50
PING_INLINE: 51282.05 requests per second
PING_BULK: 64935.07 requests per second
SET: 39840.64 requests per second
GET: 28901.73 requests per second
INCR: 26525.20 requests per second
LPUSH: 29411.76 requests per second
LPOP: 23255.81 requests per second
SADD: 21551.72 requests per second
SPOP: 23041.47 requests per second
LPUSH (needed to benchmark LRANGE): 45045.04 requests per second
LRANGE_100 (first 100 elements): 10101.01 requests per second
LRANGE_300 (first 300 elements): 4196.39 requests per second
LRANGE_500 (first 450 elements): 3337.78 requests per second
LRANGE_600 (first 600 elements): 7320.64 requests per second
MSET (10 keys): 62111.80 requests per second

How do I skip/ignore errors generated under "content by lua"?

How do I skip/ignore errors generated under "content by lua"?

1.i'm using
local zlib = require "zlib"

sometimes the zlib variable returned will be empty or non compatible gzip format.

how do i let it continue to run?

  1. how do i

location ~ /cached/ {
content_by_lua '
local zlib = require "zlib"
...
...
if (string.len(inflated) >= 10) then
ngx.say(inflated)
return
end';
}

how do i make it continue running the rest of the other status if it does not match string.len(inflated) ???

simple benchmark, is there something wrong with my code?

Hi,
I'm really intrigued in your work of using nginx as app server. I've tried simple benchmark of nginx and lua duo, and it gave me about 100k requests per second, but when I added redis call to code, it dropped to about 18k , is it normal or there is something wrong with my code?

Many thanks,
Tadek

worker_processes 8;

events {

worker_connections 16384;
}
http {
server {

access_log off;

    listen 8080;
    location / {
        default_type text/html;
        content_by_lua '
            local redis = require "resty.redis"
        local red = redis:new()

        local ok, err = red:connect("127.0.0.1", 6379)
        if not ok then
            ngx.say("failed to connect: ", err)
            return
        end

        res, err = red:set("dog", "an aniaml")
        if not ok then
            ngx.say("failed to set dog: ", err)
            return
        ngx.say("dog: ", res)
        end
        ';
    }
}

}

init_worker_by_lua + redis - how?

Hi there,

Is there any "legal" way to connect to redis from a worker created via the new init_worker_by_lua* directives?

Anything I try gives me the "API disabled in the context of init_worker_by_lua*" error.

Closed socket error

I'm trying to understand what is the best practice for handling Redis connections.

The doc suggests to avoid having a global connection to the Redis server in order to avoid any concurrency problems. This means opening a new connection to Redis for every incoming connection to Nginx, isn't this very inefficient?

Also, I'm trying to reuse the same connection in both access_by_lua and log_by_lua, but I get a attempt to send data on a closed socket error in the log_by_lua timer callback.

init_by_lua:

redis = require "resty.redis"

access_by_lua:

ngx.ctx.redis = redis:new()
local ok, err = ngx.ctx.redis:connect("127.0.0.1", 6379)
if not ok then
  ngx.log(ngx.ERR, "Cant connect to Redis")
end

ngx.ctx.redis:incr("incoming_requests")

log_by_lua:

local function log(premature, redis)
  redis:incr("requests_completed")
end

local ok, err = ngx.timer.at(0, log, ngx.ctx.redis)
if not ok then
    ngx.log(ngx.ERR, "failed to create timer: ", err)
    return
end

And I'm getting the following error in log_by_lua:

[error] 16644#0: attempt to send data on a closed socket: u:00007FCA22600B78, c:0000000000000000, ft:0 eof:0, context: ngx.timer

can't load module resty.redis

I am trying to use the resty.redis module same as the example of the synopsis.

lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";

server {
    location /test {
        content_by_lua '
            local redis = require "resty.redis"

But I am getting the error:
[string "content_by_lua"]:2: module 'resty.redis' not found

the lua_package_path is set correctly to the lua-resty-redis lib

Do you have an idea what might be the problem?

Thanks!

Cluster

Hi! Is it possible to implement cluster functionality to core of this library? I'm not sure if I am able to do it myself, I tried, but code looks bad and it fails sometimes.

你好,请问下下面的redis的set的配置写法是否准确?

主要是变量的写法无法确定是否准确。

location /toredis {
    default_type text/html;
    set $key $url;
    set $body $echo_request_body;
    set $exptime 300;
    content_by_lua '
        local redis = require "resty.redis"
        local red = redis:new()

        red:set_timeout(1000)

        local ok, err = red:connect("127.0.0.1", 6379)
        if not ok then
            return nil
        end

        local ok, err = red:set(ngx.var.key,ngx.var.body,ngx.var.exptime)
        if not ok then
            return nil
        end

        red:close()
    ';
}

Disable error logging to nginx error log

I'm trying to cache errors on connect to a redis instance both by using
ok, err = ....
and wrapping up the code with a pcall

In both cases still an error line goes to nginx error log. I have a high load application and if a redis instance goes down for a few secs my error log gets flooded with messages. Is there a way around it?

关于Select库问题

楼主,你好。菜鸟学生一枚,想请教下该驱动下,如何select不同的库,官方驱动如下:

package.path = "../src/?.lua;src/?.lua;" .. package.path
pcall(require, "luarocks.require")
local redis = require 'redis'
local params = {
host = '10.20.17.123', --Host
port = 6379, --Port
}
local client = redis.connect(params)
client:select(1) --例如这里
client:set('foo', 'bar')
local value = client:get('foo')
local test1=client:get('pwd');
print('get from redis :',value,test1)

如果使用lua-resty-redis,该如何实现?

Missing nginx config file

This file is necessary to install lua-resty-redis into a custom nginx compilation. Is it available somewhere, or is this meant to be part of another module somehow?

zrangebyscore limit problem

hello, @agentzh! first of all thank you for this great lib!
have tihs problem:

zrangebyscore ip 92724334 +inf limit 0 1

works fine in redis-cli

can't run this from lua:

res, err = redis:zrangebyscore('ip', 92724334, '+inf', 'limit 0 1')

it returns ERR syntax error in err and false in res

tried many variations but no luck at all, can you suggest something?
thank you in advance!

how to implement a redis connection pool in lua.

i am trying to use lua-resty-redis in my web server which for a game.
it works very nice when there are few requests per second.
but the cost of close the sokect is too expensive when request frequently.
so i want to implement a redis connection pool for each nginx worker.
we cannot use upstream in the lua code to access redis server ,am i right?
how to do it?

Resolv hostname problem

Hi everyone,

When set the ip to create a connect with my redis server, it work right. But if i set the hostname of my redis server, i get follow message:

2015/05/18 19:33:13 [error] 12297#0: *2592 attempt to send data on a closed socket: u:000000000243ABA8, c:0000000000000000, ft:8 eof:0, client: 179.189.133.35, server

The code that work right:

_REDIS_HOST = "10.254.222.23"
-- _REDIS_HOST = "my-server.8cxqzw.0001.sae1.cache.amazonaws.com"
_REDIS_PORT = "6379"
_REDIS_TIMEOUT = 2000 -- set 2 sec
_REDIS_DATABASE = 8

local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect(_REDIS_HOST, _REDIS_PORT)
red:set_timeout(_REDIS_TIMEOUT)
red:select(_REDIS_DATABASE)

The code that break down:

-- _REDIS_HOST = "10.254.222.23"
_REDIS_HOST = "my-server.8cxqzw.0001.sae1.cache.amazonaws.com"
_REDIS_PORT = "6379"
_REDIS_TIMEOUT = 2000 -- set 2 sec
_REDIS_DATABASE = 8

local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect(_REDIS_HOST, _REDIS_PORT)
red:set_timeout(_REDIS_TIMEOUT)
red:select(_REDIS_DATABASE)

hmget should return a hash table?

Should hmset return a hash table rather than an array table? Or, we could provided a utility function that converts it. I know I have that code in several of my projects.

Connect to database non-zero

It seems to me (unless I'm missing something) that lua-resty-redis only allows you to connect to database 0 of redis. You cannot specify another number.

ie this uri connects to database 7

redis://[email protected]:6379/7

Main request hangs with multiple SSI subrequests

Hello,

After reading the issue #23 I have updated my configuration to the following:
https://gist.github.com/frozenminds/5a1f0b9047c7af69ec61

Each SSI request will try to get the content from Redis, if key is not saved in cache yet, it will fallback to a PHP route which will generate the content, save it to Redis and return the output. The second time, Nginx will retrieve the content from Redis because it was cached.

On a page I have 4 SSI calls. The last one hangs on the very first request before the page is also stored in Nginx's FastCgi cache.

<!-- include virtual="/ssi?action=a -->
<!-- include virtual="/ssi?action=b -->
<!-- include virtual="/ssi?action=c&product_id=123 -->
<!-- include virtual="/ssi?action=c&category_id=999 -->

I am aware of this:
http://wiki.nginx.org/HttpLuaModule#Mixing_with_SSI_Not_Supported
But what I do not understand, do I have to completely turn off the SSI module and write my own module that will parse the SSI tags () and then use ngx.location.capture or ngx.location.capture_multi the get the content?

If I do have to parse the SSI tags, do you have any idea where do I start from?
Could you please guide me further?

Thank you in advance!
Boby

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.