11--- @meta
2- resty_lock = {}
3- function resty_lock .expire (self , time ) end
4- function resty_lock .unlock (self ) end
5- function resty_lock .new (_ , dict_name , opts ) end
6- resty_lock ._VERSION = " 0.08"
7- function resty_lock .lock (self , key ) end
8- return resty_lock
2+
3+ --- lua-resty-lock
4+ ---
5+ --- https://github.com/openresty/lua-resty-lock
6+ ---
7+ --- @class resty.lock : table
8+ local lock = {
9+ _VERSION = " 0.08" ,
10+ }
11+
12+
13+ --- @class resty.lock.opts : table
14+ ---
15+ --- Specifies expiration time (in seconds) for the lock entry in the shared memory
16+ --- dictionary. You can specify up to 0.001 seconds. Default to 30 (seconds). Even
17+ --- if the invoker does not call unlock or the object holding the lock is not GC'd,
18+ --- the lock will be released after this time. So deadlock won't happen even when the
19+ --- worker process holding the lock crashes.
20+ --- @field exptime number
21+ ---
22+ --- Specifies the maximal waiting time (in seconds) for the lock method calls on
23+ --- the current object instance. You can specify up to 0.001 seconds. Default to 5
24+ --- (seconds). This option value cannot be bigger than `exptime`. This timeout is
25+ --- to prevent a lock method call from waiting forever. You can specify 0 to make
26+ --- the lock method return immediately without waiting if it cannot acquire the
27+ --- lock right away.
28+ --- @field timeout number
29+ ---
30+ --- Specifies the initial step (in seconds) of sleeping when waiting for the lock.
31+ --- Default to 0.001 (seconds). When the lock method is waiting on a busy lock, it
32+ --- sleeps by steps. The step size is increased by a ratio (specified by the ratio
33+ --- option) until reaching the step size limit (specified by the max_step option).
34+ --- @field step number
35+ ---
36+ --- Specifies the step increasing ratio. Default to 2, that is, the step size
37+ --- doubles at each waiting iteration.
38+ --- @field ratio number
39+ ---
40+ --- Specifies the maximal step size (i.e., sleep interval, in seconds) allowed.
41+ --- See also the step and ratio options). Default to 0.5 (seconds).
42+ --- @field max_step number
43+
44+
45+ --- Creates a new lock object instance by specifying the shared dictionary name
46+ --- (created by `lua_shared_dict`) and an optional options table `opts`.
47+ ---
48+ --- @param dict_name string
49+ --- @param opts ? resty.lock.opts
50+ --- @return resty.lock ? lock
51+ --- @return string ? err
52+ function lock .new (_ , dict_name , opts ) end
53+
54+ --- Tries to lock a key across all the Nginx worker processes in the current
55+ --- NGINX server instance. Different keys are different locks.
56+ ---
57+ --- The length of the key string must not be larger than 65535 bytes.
58+ ---
59+ --- Returns the waiting time (in seconds) if the lock is successfully acquired.
60+ --- Otherwise returns `nil` and a string describing the error.
61+ ---
62+ --- The waiting time is not from the wallclock, but rather is from simply adding
63+ --- up all the waiting "steps". A nonzero elapsed return value indicates that
64+ --- someone else has just hold this lock. But a zero return value cannot gurantee
65+ --- that no one else has just acquired and released the lock.
66+ ---
67+ --- When this method is waiting on fetching the lock, no operating system threads
68+ --- will be blocked and the current Lua "light thread" will be automatically yielded
69+ --- behind the scene.
70+ ---
71+ --- It is strongly recommended to always call the unlock() method to actively
72+ --- release the lock as soon as possible.
73+ ---
74+ --- If the `unlock()` method is never called after this method call, the lock
75+ --- will get released when
76+ ---
77+ --- The current `resty.lock` object instance is collected automatically by the Lua GC.
78+ --- OR
79+ --- The exptime for the lock entry is reached.
80+ ---
81+ --- Common errors for this method call is
82+ ---
83+ --- "timeout" : The timeout threshold specified by the timeout option of the new method is exceeded.
84+ --- "locked" : The current `resty.lock` object instance is already holding a lock (not necessarily of the same key).
85+ ---
86+ --- Other possible errors are from ngx_lua's shared dictionary API.
87+ ---
88+ --- It is required to create different `resty.lock` instances for multiple
89+ --- simultaneous locks (i.e., those around different keys).
90+ ---
91+ --- @param key string
92+ --- @return number ? elapsed
93+ --- @return string ? error
94+ function lock :lock (key ) end
95+
96+ --- Releases the lock held by the current `resty.lock` object instance.
97+ ---
98+ --- Returns 1 on success. Returns `nil` and a string describing the error otherwise.
99+ ---
100+ --- If you call unlock when no lock is currently held, the error "unlocked" will
101+ --- be returned.
102+ ---
103+ --- @return boolean ok
104+ --- @return string ? error
105+ function lock :unlock () end
106+
107+ --- Sets the TTL of the lock held by the current `resty.lock` object instance.
108+ --- This will reset the timeout of the lock to timeout seconds if it is given,
109+ --- otherwise the timeout provided while calling new will be used.
110+ ---
111+ --- Note that the timeout supplied inside this function is independent from the
112+ --- timeout provided while calling new. Calling `expire()` will not change the
113+ --- timeout value specified inside new and subsequent `expire(nil)` call will
114+ --- still use the timeout number from new.
115+ ---
116+ --- Returns true on success. Returns `nil` and a string describing the error
117+ --- otherwise.
118+ ---
119+ --- If you call expire when no lock is currently held, the error "unlocked" will
120+ --- be returned.
121+ ---
122+ --- @param timeout ? number
123+ --- @return boolean ok
124+ --- @return string ? error
125+ function lock :expire (timeout ) end
126+
127+ return lock
0 commit comments