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

resty-lock inoperative in init_worker_by_lua_file

Open chopper-poet opened this issue 8 years ago • 5 comments

hi,agentzh I used lock:lock on init_worker_by_lua_file,but inoperative. here is my code `local resty_lock = require "resty.lock" local lockOpts = {["exptime"]="5",["timeout"]="0"} local lock, err = resty_lock:new("MY_LOCKS",lockOpts) if not lock then ngx.log(ngx.ERR,"failed to create lock:"..tostring(err)) return end

local elapsed, err = lock:lock('lock_key') if not elapsed then ngx.log(ngx.DEBUG,"failed to acquire the lock: "..tostring(err)) return else ngx.log(ngx.DEBUG,"get lock success") return end )`

My nginx has 4 workers and i set timeout=0 for get lock operation,but still display 4 'get lock success'

chopper-poet avatar Feb 01 '18 03:02 chopper-poet

@panjunDev This is a known limitation. See https://github.com/openresty/lua-resty-lock/#limitations

agentzh avatar Feb 02 '18 22:02 agentzh

hi, agentzh i have 4 worker process, i need one to load data, before this worker completed load data, other workers should not work(can't process request). how can i do it?

fengshenyun avatar Apr 09 '18 08:04 fengshenyun

@fengshenyun Sounds like you should really load the data in the init_by_lua* handler instead (i.e., in the master process before forking off workers).

agentzh avatar Apr 09 '18 19:04 agentzh

because of i need load data from a http server, init_by_lua* is not allow do this.

fengshenyun avatar Apr 10 '18 02:04 fengshenyun

@fengshenyun You can try using the blocking LuaSocket library to do this as long as its timeout protection is good enough. It does not matter to block in the master process for a bit (just not too long). But yeah, we'll eventually enable the cosocket API in the context of init_by_lua* context. Before that, you can use the blocking approach as a workaround.

agentzh avatar Apr 10 '18 21:04 agentzh