-
Notifications
You must be signed in to change notification settings - Fork 56
Any way to make nginx return empty_gif of result with "return ..;" instead of memcache result? #18
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
Comments
@Profforgr It sounds trivial if you use the ngx_lua module with the (nonblocking) lua-resty-memcached library. BTW, I think it's much more efficient and simpler to use ngx_lua's shm-backed lua_shared_dictionary to store your counters instead of using memcached. See BTW, you're welcome to join the openresty-en mailing list to discuss further. Please see http://openresty.org/#Community Thank you! |
OK, i see you like lua a lot -) Well, i believe you always. I see lua is used in most highloaded environments. I can't fight anymore -) I decided to wrote that in lua... I would love if you can comment my code and answer few noob questions. location ~ ^/app/counter/(\d+)$ {
more_clear_headers "Date" "Content-Type"; # disables CloudFlare gzip & less traffic
set $id $1;
access_by_lua '
ngx.eof()
-- ngx.sleep(10) -- sec
-- simple abuse prevention (assuming we have no more than 100 million ids)
if tonumber(ngx.var.id) > 100000000 then
ngx.say("id should be less than 100 million")
return
end
local memcached = require "resty.memcached"
local memc, err = memcached:new()
if not memc then
ngx.say("failed to instantiate memc: ", err)
return
end
memc:set_timeout(100000) -- 100 sec
-- local ok, err = memc:connect("127.0.0.1", 11211)
local ok, err = memc:connect("unix:/tmp/memcached.sock")
if not ok then
ngx.say("failed to connect: ", err)
return
end
local key = ngx.var.lower_host .. "|post" .. ngx.var.id
local ok, err = memc:incr(key, 1)
if not ok then
ngx.say("failed to incr " .. key, err)
local ok, err = memc:add(key, "0")
if not ok then
ngx.say("failed to add " .. key, err)
else
ngx.say("add ok")
local ok, err = memc:incr(key, 1)
if not ok then
ngx.say("failed to incr " .. key .. " after add", err)
return
else
ngx.say("incr ok after add")
end
end
else
ngx.say("incr" .. key .. "ok")
end
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = memc:set_keepalive(10000, 100)
if not ok then
ngx.say("cannot set keepalive: ", err)
return
end
';
} My questions:
I thought about it and came to a conclusion that i'll not able to make external application extract the keys from lua_shared_dictinary without issues. I need external application to get keys from here periodically (once a minute). But ngx.shared.DICT.get_keys will block entire nginx, this is not acceptable. And ngx.shared.DICT.get for all the possible keys (~1 million and growing) i assume will be slower than memcached because of HTTP and some nginx logic.
I know, thanks. But i find google groups style not comfortable. |
@Profforgr Please don't use github issues for such general lengthy discussions, which makes me uncomfortable. Regarding the mailing lists, they are mailing lists. You can use emails to read, post and reply in your own favourite mail client (be it gmail, thunderbird, outlook, or whatever). See https://openresty.org/#Community |
Hello! On Fri, Jun 19, 2015 at 7:11 AM, Profforgr wrote:
To quote the related documentation for ngx.eof: "When you disable the HTTP 1.1 keep-alive feature for your downstream https://github.com/openresty/lua-nginx-module#ngxeof Pay special attention to the last sentence if you do need HTTP 1.1
Use of such long timeout may accumulate pending handlers and open
Everything is nonblocking if you use ngx_lua's Lua API and Lua
You already use Use of content_by_lua has no measurable difference as compared to
For concurrent client requests. If the idle connections in the pool
Keep-alive and nonblocking are two separate unrelated things.
I'd assume you mean all the idle connections in the pool are taken and
Yes.
I don't think a single memcached instance can support this many
Maybe you can store key names to a single value yourself under a
Another option is to use Redis, which supports persistency. And you BTW, this discussion is already OT for this memc-nginx-module project. |
@Profforgr Oh, forgot to mention that you cannot call |
Hi.
Is there any way to make nginx return empty_gif or result with "return ...;" instead of returning result received from memcache?
If this read by "agentzh", i was inspired by your slides at http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html .
I just basically use the example from http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#45 slightly modified to adopt for my case.
I want to do a simple counter here.
The only problem is that in result to /app/counter/1 and so on it return a number (the result of SET query to memcache). But i want it simply return empty_gif. I've tried various settings but can't get it working. It completely ignores empty_gif and return directivities. Can you give me an idea please?
If possible, it would be also to cool to know how to make it really async. Because now, when it returns result, it basically waits for it (otherwise it should not return anything). I want this operation to happen in background after connection close (or better request end, not sure does that really possible - to stay keepalive connection but end current request).
The text was updated successfully, but these errors were encountered: