lua-resty-redis icon indicating copy to clipboard operation
lua-resty-redis copied to clipboard

Add luasocket-based redis interface

Open josegonzalez opened this issue 12 years ago • 12 comments

Useful in contexts where the cosocket api is not available - such as init_by_lua.

I used this to load a script into lua once, set a shared memory variable, and then use evalsha instead of sending the much larger script over the wire constantly.

Implementation here: https://github.com/chaoslawful/lua-nginx-module/issues/206#issuecomment-13041467

josegonzalez avatar Feb 03 '13 02:02 josegonzalez

Hello!

On Sat, Feb 2, 2013 at 6:56 PM, Jose Diaz-Gonzalez <[email protected]

wrote:

Useful in contexts where the cosocket api is not available - such as init_by_lua.

I used this to load a script into lua once, set a shared memory variable, and then use evalsha instead of sending the much larger script over the wire constantly.

For your very case, instead of doing it in init_by_lua, another option is to do this initialization work at the first request and cache the results (or state) in Lua module level variables. See http://wiki.nginx.org/HttpLuaModule#Data_Sharing_within_an_Nginx_Worker :)

Because init_by_lua is running in the Nginx master process which usually runs with root access, it's also more dangerous to do a lot of work in it :)

Best regards, -agentzh

agentzh avatar Feb 03 '13 03:02 agentzh

True, though separating the code to specify this once per nginx server is nice, and init_by_lua seems like the perfect place to do it.

Yes, it is dangerous, but in our case, it is trusted code that is running, not something that an external user is defining.

josegonzalez avatar Feb 03 '13 03:02 josegonzalez

@josegonzalez I'd like to add a simple blocking implementation of the cosocket API for init_by_lua* (it does not matter at all to block there). Are you willing to contribute a patch? ;)

agentzh avatar Sep 27 '13 22:09 agentzh

Sure. I think it was a two line change, but I'll see what I can do :) On Sep 27, 2013 6:49 PM, "Yichun Zhang" [email protected] wrote:

@josegonzalez https://github.com/josegonzalez I'd like to add a simple blocking implementation of the cosocket API for init_by_lua*. Are you willing to contribute a patch? ;)

— Reply to this email directly or view it on GitHubhttps://github.com/agentzh/lua-resty-redis/issues/14#issuecomment-25282740 .

josegonzalez avatar Sep 27 '13 22:09 josegonzalez

@josegonzalez No, not really. The current implementation is nonblocking.

agentzh avatar Sep 27 '13 22:09 agentzh

Oh I meant to make it use lua's socket class, was a trivial change.

josegonzalez avatar Sep 27 '13 23:09 josegonzalez

:+1:

subnetmarco avatar Nov 12 '13 20:11 subnetmarco

For an example, I want pull a stream list from redis in init_by_lua.

XadillaX avatar Feb 23 '17 06:02 XadillaX

Thumbs up for cosocket (be it blocking or not blocking) at init_by_lua context.

yangm97 avatar Apr 30 '17 23:04 yangm97

@agentzh Any progress on this? Connecting to Redis to pull data before the server starts would be great.

rw-rs avatar Oct 04 '17 22:10 rw-rs

Our current plan is to directly run an event loop in the init_by_lua* context and make cosockets directly work there.

agentzh avatar Oct 05 '17 01:10 agentzh

@agentzh Great!

rw-rs avatar Oct 08 '17 20:10 rw-rs