Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: lua-resty-upstream with lua-resty-redis #17

Open
Blasterdick opened this issue Aug 16, 2017 · 7 comments
Open

Question: lua-resty-upstream with lua-resty-redis #17

Blasterdick opened this issue Aug 16, 2017 · 7 comments

Comments

@Blasterdick
Copy link

Can this be used with lua-resty-redis to connect to an upstream of redis servers? If yes, provide me some example, if you please. Thanks in advance.

@hamishforbes
Copy link
Owner

Yes, you simply pass your 'resty.redis' instance to the connect method.
As in the example on the connect docs https://github.com/hamishforbes/lua-resty-upstream#connect

You must configure your pools and hosts first as per the overview example

@Blasterdick
Copy link
Author

Thank you, I'll try to do as you said.

But for now, I'm facing an error while configuring openresty with this module:

./configure: error: no /var/chef/cache/lua-resty-upstream/config was found

I just cloned it with git and passed the path to the directory to a configure parameters as --add-module=/var/chef/cache/lua-resty-upstream.

What am I doing wrong? Thanks in advance.

@hamishforbes
Copy link
Owner

This is a pure Lua module, it doesn't need compiling into openresty.

You simply put it somewhere that your lua_package_path will find it

@Blasterdick
Copy link
Author

Great thank you, my bad.

Now, I'm using a config like next:

nginx.conf:

lua_package_path "/var/www/lua-resty-upstream/lib/?.lua;/usr/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?.lua;;";

lua_shared_dict upstream_dict 1m;
init_by_lua_file /var/www/app/upstream.lua;

init_worker_by_lua 'upstream:init_background_thread()';

server {
}

upstream.lua:

upstream_socket  = require("resty.upstream.socket")
upstream_api = require("resty.upstream.api")

upstream, configured = upstream_socket:new("upstream_dict")
if not upstream then
    error(configured)
end
api = upstream_api:new(upstream)

if not configured then -- Only reconfigure on start, shared mem persists across a HUP
    api:create_pool({id = "primary", timeout = 5})
    api:set_priority("primary", 0)
    api:set_method("primary", "round_robin")
    api:add_host("primary", { id="a", host = "127.0.0.1", port = "6379", weight = 10 })
    api:add_host("primary", { id="b", host = "127.0.0.1", port = "6380", weight = 10 })
    api:add_host("primary", { id="c", host = "127.0.0.1", port = "6381", weight = 10 })
    api:add_host("primary", { id="d", host = "127.0.0.1", port = "6382", weight = 10 })
end

app.lua:

resty_redis = require('resty.redis')
local redis = resty_redis.new()

redis:set_timeout(5000)

local key = ngx.req.get_headers()["X-Forwarded-For"]

local redis, err = upstream:connect(redis, key)

if not redis then
    ngx.log(ngx.ERR, err)
    ngx.status = 500
    return ngx.exit(ngx.status)
end

ngx.log(ngx.info, 'Connected to ' .. err.host.host .. ':' .. err.host.port)
if string.len(post_args) > 0 then

    local res, err = redis:rpush(ngx.var.redis_key, data)

    if not res then
        ngx.say("RPUSH: failed to read reply: ", err)
        return
    end

else
    -- ERROR
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end

But I'm seeing in nginx log an error like next:

2017/08/18 13:26:00 [error] 26312#0: *4327 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, client: aaa.bbb.ccc.ddd.eee, server: _, request: "POST /app?bla=blabla HTTP/1.1", host: "my.domain.com"

What am I doing wrong? Thanks in advance.

@hamishforbes
Copy link
Owner

You're setting the timeout on the pool to 5ms, that is probably too low...
api:create_pool({id = "primary", timeout = 5})

If that doesn't fix it can you set your log level to debug and post the full log?

I'd also suggest wrapping your code up in a module rather than creating global vars
See https://github.com/openresty/lua-nginx-module#data-sharing-within-an-nginx-worker for details

@Blasterdick
Copy link
Author

Thanks for a suggestion, man.

I set it to 1000, and the error is the same.

How can I set debug level of the Lua logs (I'm a newbie in Lua, and google shows only error_log format recommendations)?
Thanks in advance.

@hamishforbes
Copy link
Owner

I mean the nginx error_log log level :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants