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

Feature request: semaphores

Open georgebashi opened this issue 9 years ago • 6 comments

It'd be great if this library provided semaphores as well as mutexes. My use-case is implementing simple request queueing / concurrency limiting in the access phase by waiting on a semaphore.

georgebashi avatar Jul 31 '14 21:07 georgebashi

@georgebashi Yes, this should be useful for its own right. But I'm curious about the reasons that you do not use the standard ngx_limit_req and ngx_limit_conn modules for your use case. Are they not flexible enough for your needs?

agentzh avatar Jul 31 '14 23:07 agentzh

My understanding is that ngx_limit_req limits the rate of requests rather than concurrency. ngx_limit_conn is closer to what I need, but I think it limits incoming connections to nginx, not nginx's active upstream requests. ngx_limit_conn also returns 503s rather than queueing which is undesirable.

I'd like to accept an unlimited requests with nginx, have them wait for one of N available upstream connections, then proxy and return, so as to limit the number of active requests to the upstream to a configurable number.

georgebashi avatar Aug 01 '14 00:08 georgebashi

I'm essentially trying to achieve what the upstream queue directive does but in lua.

georgebashi avatar Aug 01 '14 00:08 georgebashi

@georgebashi I see your point. Will you contribute a patch? It's better to be in a separate Lua class (or module), like resty.semaphore. I think you can just use ngx.sleep as well as the shdict incr and add methods to implement this in pure Lua.

agentzh avatar Aug 01 '14 20:08 agentzh

any update? how can I achieve the upstream queue directive? Thanks!

s7v7nislands avatar Mar 18 '16 05:03 s7v7nislands

@s7v7nislands The ngx.semaphore API has already been released for a couple of months: https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/semaphore.md

agentzh avatar Mar 22 '16 19:03 agentzh