lua-resty-lock
lua-resty-lock copied to clipboard
Feature request: semaphores
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 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?
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.
I'm essentially trying to achieve what the upstream queue directive does but in lua.
@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.
any update? how can I achieve the upstream queue directive? Thanks!
@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