├── .gitattributes ├── .gitignore ├── .travis.yml ├── Makefile ├── README.markdown ├── dist.ini ├── lib └── resty │ └── lock.lua ├── t └── sanity.t └── valgrind.suppress /.gitattributes: -------------------------------------------------------------------------------- 1 | *.t linguist-language=Text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | *~ 4 | go 5 | t/servroot/ 6 | reindex 7 | nginx 8 | ctags 9 | tags 10 | a.lua 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | dist: focal 3 | 4 | branches: 5 | only: 6 | - "master" 7 | 8 | os: linux 9 | 10 | language: c 11 | 12 | compiler: 13 | - gcc 14 | 15 | cache: 16 | directories: 17 | - download-cache 18 | 19 | env: 20 | global: 21 | - JOBS=3 22 | - NGX_BUILD_JOBS=$JOBS 23 | - LUAJIT_PREFIX=/opt/luajit21 24 | - LUAJIT_LIB=$LUAJIT_PREFIX/lib 25 | - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 26 | - LUA_INCLUDE_DIR=$LUAJIT_INC 27 | - LUA_CMODULE_DIR=/lib 28 | - OPENSSL_PREFIX=/opt/ssl 29 | - OPENSSL_LIB=$OPENSSL_PREFIX/lib 30 | - OPENSSL_INC=$OPENSSL_PREFIX/include 31 | - OPENSSL_VER=1.1.1w 32 | - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH 33 | - TEST_NGINX_SLEEP=0.006 34 | matrix: 35 | - NGINX_VERSION=1.27.1 36 | 37 | install: 38 | - if [ ! -d download-cache ]; then mkdir download-cache; fi 39 | - if [ ! -f download-cache/openssl-$OPENSSL_VER.tar.gz ]; then wget -O download-cache/openssl-$OPENSSL_VER.tar.gz https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz; fi 40 | - sudo apt-get install -qq -y axel 41 | - cpanm --sudo --notest Test::Nginx > build.log 2>&1 || (cat build.log && exit 1) 42 | - git clone https://github.com/openresty/openresty.git ../openresty 43 | - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core 44 | - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache 45 | - git clone https://github.com/openresty/nginx-devel-utils.git 46 | - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module 47 | - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module 48 | - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx 49 | - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git 50 | - git clone https://github.com/openresty/mockeagain.git 51 | 52 | script: 53 | - cd luajit2/ 54 | - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1) 55 | - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) 56 | - cd .. 57 | - tar zxf download-cache/openssl-$OPENSSL_VER.tar.gz 58 | - cd openssl-$OPENSSL_VER/ 59 | - ./config shared --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1) 60 | - make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1) 61 | - sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1) 62 | - cd ../mockeagain/ && make CC=$CC -j$JOBS && cd .. 63 | - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH 64 | - export LD_PRELOAD=$PWD/mockeagain/mockeagain.so 65 | - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH 66 | - export TEST_NGINX_RESOLVER=8.8.4.4 67 | - export NGX_BUILD_CC=$CC 68 | - ngx-build $NGINX_VERSION --without-pcre2 --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-cc-opt="-I$OPENSSL_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB" --add-module=../ndk-nginx-module --add-module=../lua-nginx-module --with-debug > build.log 2>&1 || (cat build.log && exit 1) 69 | - nginx -V 70 | - ldd `which nginx`|grep -E 'luajit|ssl|pcre' 71 | - prove -r t 72 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OPENRESTY_PREFIX=/usr/local/openresty 2 | 3 | PREFIX ?= /usr/local 4 | LUA_INCLUDE_DIR ?= $(PREFIX)/include 5 | LUA_LIB_DIR ?= $(PREFIX)/lib/lua/$(LUA_VERSION) 6 | INSTALL ?= install 7 | 8 | .PHONY: all test install 9 | 10 | all: ; 11 | 12 | install: all 13 | $(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/resty/ 14 | $(INSTALL) lib/resty/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/resty/ 15 | 16 | test: all 17 | PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t 18 | 19 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | Name 2 | ==== 3 | 4 | lua-resty-lock - Simple shm-based nonblocking lock API 5 | 6 | Table of Contents 7 | ================= 8 | 9 | * [Name](#name) 10 | * [Status](#status) 11 | * [Synopsis](#synopsis) 12 | * [Description](#description) 13 | * [Methods](#methods) 14 | * [new](#new) 15 | * [lock](#lock) 16 | * [unlock](#unlock) 17 | * [expire](#expire) 18 | * [For Multiple Lua Light Threads](#for-multiple-lua-light-threads) 19 | * [For Cache Locks](#for-cache-locks) 20 | * [Limitations](#limitations) 21 | * [Prerequisites](#prerequisites) 22 | * [Installation](#installation) 23 | * [TODO](#todo) 24 | * [Community](#community) 25 | * [English Mailing List](#english-mailing-list) 26 | * [Chinese Mailing List](#chinese-mailing-list) 27 | * [Bugs and Patches](#bugs-and-patches) 28 | * [Author](#author) 29 | * [Copyright and License](#copyright-and-license) 30 | * [See Also](#see-also) 31 | 32 | Status 33 | ====== 34 | 35 | This library is production ready. 36 | 37 | Synopsis 38 | ======== 39 | 40 | ```lua 41 | # nginx.conf 42 | 43 | http { 44 | # you do not need the following line if you are using the 45 | # OpenResty bundle: 46 | lua_package_path "/path/to/lua-resty-core/lib/?.lua;/path/to/lua-resty-lock/lib/?.lua;;"; 47 | 48 | lua_shared_dict my_locks 100k; 49 | 50 | server { 51 | ... 52 | 53 | location = /t { 54 | content_by_lua ' 55 | local resty_lock = require "resty.lock" 56 | for i = 1, 2 do 57 | local lock, err = resty_lock:new("my_locks") 58 | if not lock then 59 | ngx.say("failed to create lock: ", err) 60 | end 61 | 62 | local elapsed, err = lock:lock("my_key") 63 | ngx.say("lock: ", elapsed, ", ", err) 64 | 65 | local ok, err = lock:unlock() 66 | if not ok then 67 | ngx.say("failed to unlock: ", err) 68 | end 69 | ngx.say("unlock: ", ok) 70 | end 71 | '; 72 | } 73 | } 74 | } 75 | ``` 76 | 77 | Description 78 | =========== 79 | 80 | This library implements a simple mutex lock in a similar way to ngx_proxy module's [proxy_cache_lock directive](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock). 81 | 82 | Under the hood, this library uses [ngx_lua](https://github.com/openresty/lua-nginx-module) module's shared memory dictionaries. The lock waiting is nonblocking because we use stepwise [ngx.sleep](https://github.com/openresty/lua-nginx-module#ngxsleep) to poll the lock periodically. 83 | 84 | [Back to TOC](#table-of-contents) 85 | 86 | Methods 87 | ======= 88 | 89 | To load this library, 90 | 91 | 1. you need to specify this library's path in ngx_lua's [lua_package_path](https://github.com/openresty/lua-nginx-module#lua_package_path) directive. For example, `lua_package_path "/path/to/lua-resty-lock/lib/?.lua;;";`. 92 | 2. you use `require` to load the library into a local Lua variable: 93 | 94 | ```lua 95 | local lock = require "resty.lock" 96 | ``` 97 | 98 | [Back to TOC](#table-of-contents) 99 | 100 | new 101 | --- 102 | `syntax: obj, err = lock:new(dict_name)` 103 | 104 | `syntax: obj, err = lock:new(dict_name, opts)` 105 | 106 | Creates a new lock object instance by specifying the shared dictionary name (created by [lua_shared_dict](http://https://github.com/openresty/lua-nginx-module#lua_shared_dict)) and an optional options table `opts`. 107 | 108 | In case of failure, returns `nil` and a string describing the error. 109 | 110 | The options table accepts the following options: 111 | 112 | * `exptime` 113 | Specifies expiration time (in seconds) for the lock entry in the shared memory dictionary. You can specify up to `0.001` seconds. Default to 30 (seconds). Even if the invoker does not call `unlock` or the object holding the lock is not GC'd, the lock will be released after this time. So deadlock won't happen even when the worker process holding the lock crashes. 114 | * `timeout` 115 | Specifies the maximal waiting time (in seconds) for the [lock](#lock) method calls on the current object instance. You can specify up to `0.001` seconds. Default to 5 (seconds). This option value cannot be bigger than `exptime`. This timeout is to prevent a [lock](#lock) method call from waiting forever. 116 | You can specify `0` to make the [lock](#lock) method return immediately without waiting if it cannot acquire the lock right away. 117 | * `step` 118 | Specifies the initial step (in seconds) of sleeping when waiting for the lock. Default to `0.001` (seconds). When the [lock](#lock) method is waiting on a busy lock, it sleeps by steps. The step size is increased by a ratio (specified by the `ratio` option) until reaching the step size limit (specified by the `max_step` option). 119 | * `ratio` 120 | Specifies the step increasing ratio. Default to 2, that is, the step size doubles at each waiting iteration. 121 | * `max_step` 122 | Specifies the maximal step size (i.e., sleep interval, in seconds) allowed. See also the `step` and `ratio` options). Default to 0.5 (seconds). 123 | 124 | [Back to TOC](#table-of-contents) 125 | 126 | lock 127 | ---- 128 | `syntax: elapsed, err = obj:lock(key)` 129 | 130 | Tries to lock a key across all the Nginx worker processes in the current Nginx server instance. Different keys are different locks. 131 | 132 | The length of the key string must not be larger than 65535 bytes. 133 | 134 | Returns the waiting time (in seconds) if the lock is successfully acquired. Otherwise returns `nil` and a string describing the error. 135 | 136 | The waiting time is not from the wallclock, but rather is from simply adding up all the waiting "steps". A nonzero `elapsed` return value indicates that someone else has just hold this lock. But a zero return value cannot gurantee that no one else has just acquired and released the lock. 137 | 138 | When this method is waiting on fetching the lock, no operating system threads will be blocked and the current Lua "light thread" will be automatically yielded behind the scene. 139 | 140 | It is strongly recommended to always call the [unlock()](#unlock) method to actively release the lock as soon as possible. 141 | 142 | If the [unlock()](#unlock) method is never called after this method call, the lock will get released when 143 | 144 | 1. the current `resty.lock` object instance is collected automatically by the Lua GC. 145 | 2. the `exptime` for the lock entry is reached. 146 | 147 | Common errors for this method call is 148 | * "timeout" 149 | : The timeout threshold specified by the `timeout` option of the [new](#new) method is exceeded. 150 | * "locked" 151 | : The current `resty.lock` object instance is already holding a lock (not necessarily of the same key). 152 | 153 | Other possible errors are from ngx_lua's shared dictionary API. 154 | 155 | It is required to create different `resty.lock` instances for multiple simultaneous locks (i.e., those around different keys). 156 | 157 | [Back to TOC](#table-of-contents) 158 | 159 | unlock 160 | ------ 161 | `syntax: ok, err = obj:unlock()` 162 | 163 | Releases the lock held by the current `resty.lock` object instance. 164 | 165 | Returns `1` on success. Returns `nil` and a string describing the error otherwise. 166 | 167 | If you call `unlock` when no lock is currently held, the error "unlocked" will be returned. 168 | 169 | [Back to TOC](#table-of-contents) 170 | 171 | expire 172 | ------ 173 | `syntax: ok, err = obj:expire(timeout)` 174 | 175 | Sets the TTL of the lock held by the current `resty.lock` object instance. This will reset the 176 | timeout of the lock to `timeout` seconds if it is given, otherwise the `timeout` provided while 177 | calling [new](#new) will be used. 178 | 179 | Note that the `timeout` supplied inside this function is independent from the `timeout` provided while 180 | calling [new](#new). Calling `expire()` will not change the `timeout` value specified inside [new](#new) 181 | and subsequent `expire(nil)` call will still use the `timeout` number from [new](#new). 182 | 183 | Returns `true` on success. Returns `nil` and a string describing the error otherwise. 184 | 185 | If you call `expire` when no lock is currently held, the error "unlocked" will be returned. 186 | 187 | [Back to TOC](#table-of-contents) 188 | 189 | For Multiple Lua Light Threads 190 | ============================== 191 | 192 | It is always a bad idea to share a single `resty.lock` object instance across multiple ngx_lua "light threads" because the object itself is stateful and is vulnerable to race conditions. It is highly recommended to always allocate a separate `resty.lock` object instance for each "light thread" that needs one. 193 | 194 | [Back to TOC](#table-of-contents) 195 | 196 | For Cache Locks 197 | =============== 198 | 199 | One common use case for this library is avoid the so-called "dog-pile effect", that is, to limit concurrent backend queries for the same key when a cache miss happens. This usage is similar to the standard ngx_proxy module's [proxy_cache_lock](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock) directive. 200 | 201 | The basic workflow for a cache lock is as follows: 202 | 203 | 1. Check the cache for a hit with the key. If a cache miss happens, proceed to step 2. 204 | 2. Instantiate a `resty.lock` object, call the [lock](#lock) method on the key, and check the 1st return value, i.e., the lock waiting time. If it is `nil`, handle the error; otherwise proceed to step 3. 205 | 3. Check the cache again for a hit. If it is still a miss, proceed to step 4; otherwise release the lock by calling [unlock](#unlock) and then return the cached value. 206 | 4. Query the backend (the data source) for the value, put the result into the cache, and then release the lock currently held by calling [unlock](#unlock). 207 | 208 | Below is a kinda complete code example that demonstrates the idea. 209 | 210 | ```lua 211 | local resty_lock = require "resty.lock" 212 | local cache = ngx.shared.my_cache 213 | 214 | -- step 1: 215 | local val, err = cache:get(key) 216 | if val then 217 | ngx.say("result: ", val) 218 | return 219 | end 220 | 221 | if err then 222 | return fail("failed to get key from shm: ", err) 223 | end 224 | 225 | -- cache miss! 226 | -- step 2: 227 | local lock, err = resty_lock:new("my_locks") 228 | if not lock then 229 | return fail("failed to create lock: ", err) 230 | end 231 | 232 | local elapsed, err = lock:lock(key) 233 | if not elapsed then 234 | return fail("failed to acquire the lock: ", err) 235 | end 236 | 237 | -- lock successfully acquired! 238 | 239 | -- step 3: 240 | -- someone might have already put the value into the cache 241 | -- so we check it here again: 242 | val, err = cache:get(key) 243 | if val then 244 | local ok, err = lock:unlock() 245 | if not ok then 246 | return fail("failed to unlock: ", err) 247 | end 248 | 249 | ngx.say("result: ", val) 250 | return 251 | end 252 | 253 | --- step 4: 254 | local val = fetch_redis(key) 255 | if not val then 256 | local ok, err = lock:unlock() 257 | if not ok then 258 | return fail("failed to unlock: ", err) 259 | end 260 | 261 | -- FIXME: we should handle the backend miss more carefully 262 | -- here, like inserting a stub value into the cache. 263 | 264 | ngx.say("no value found") 265 | return 266 | end 267 | 268 | -- update the shm cache with the newly fetched value 269 | local ok, err = cache:set(key, val, 1) 270 | if not ok then 271 | local ok, err = lock:unlock() 272 | if not ok then 273 | return fail("failed to unlock: ", err) 274 | end 275 | 276 | return fail("failed to update shm cache: ", err) 277 | end 278 | 279 | local ok, err = lock:unlock() 280 | if not ok then 281 | return fail("failed to unlock: ", err) 282 | end 283 | 284 | ngx.say("result: ", val) 285 | ``` 286 | 287 | Here we assume that we use the ngx_lua shared memory dictionary to cache the Redis query results and we have the following configurations in `nginx.conf`: 288 | 289 | ```nginx 290 | # you may want to change the dictionary size for your cases. 291 | lua_shared_dict my_cache 10m; 292 | lua_shared_dict my_locks 1m; 293 | ``` 294 | 295 | The `my_cache` dictionary is for the data cache while the `my_locks` dictionary is for `resty.lock` itself. 296 | 297 | Several important things to note in the example above: 298 | 299 | 1. You need to release the lock as soon as possible, even when some other unrelated errors happen. 300 | 2. You need to update the cache with the result got from the backend *before* releasing the lock so other threads already waiting on the lock can get cached value when they get the lock afterwards. 301 | 3. When the backend returns no value at all, we should handle the case carefully by inserting some stub value into the cache. 302 | 303 | [Back to TOC](#table-of-contents) 304 | 305 | Limitations 306 | =========== 307 | 308 | Some of this library's API functions may yield. So do not call those functions in `ngx_lua` module contexts where yielding is not supported (yet), like `init_by_lua*`, 309 | `init_worker_by_lua*`, `header_filter_by_lua*`, `body_filter_by_lua*`, `balancer_by_lua*`, and `log_by_lua*`. 310 | 311 | [Back to TOC](#table-of-contents) 312 | 313 | Prerequisites 314 | ============= 315 | 316 | * [LuaJIT](http://luajit.org) 2.0+ 317 | * [ngx_lua](https://github.com/openresty/lua-nginx-module) 0.8.10+ 318 | 319 | [Back to TOC](#table-of-contents) 320 | 321 | Installation 322 | ============ 323 | 324 | It is recommended to use the latest [OpenResty bundle](http://openresty.org) directly where this library 325 | is bundled and enabled by default. At least OpenResty 1.4.2.9 is required. And you need to enable LuaJIT when building your OpenResty 326 | bundle by passing the `--with-luajit` option to its `./configure` script. No extra Nginx configuration is required. 327 | 328 | If you want to use this library with your own Nginx build (with ngx_lua), then you need to 329 | ensure you are using at least ngx_lua 0.8.10. Also, You need to configure 330 | the [lua_package_path](https://github.com/openresty/lua-nginx-module#lua_package_path) directive to 331 | add the path of your lua-resty-lock and lua-resty-core source directories to ngx_lua's Lua module search path, as in 332 | 333 | ```nginx 334 | # nginx.conf 335 | http { 336 | lua_package_path "/path/to/lua-resty-lock/lib/?.lua;/path/to/lua-resty-core/lib/?.lua;;"; 337 | ... 338 | } 339 | ``` 340 | 341 | and then load the library in Lua: 342 | 343 | ```lua 344 | local resty_lock = require "resty.lock" 345 | ``` 346 | 347 | Note that this library depends on the [lua-resty-core](https://github.com/openresty/lua-resty-core) library 348 | which is also enabled by default in the OpenResty bundle. 349 | 350 | [Back to TOC](#table-of-contents) 351 | 352 | TODO 353 | ==== 354 | 355 | * We should simplify the current implementation when LuaJIT 2.1 gets support for `__gc` metamethod on normal Lua tables. Right now we are using an FFI cdata and a ref/unref memo table to work around this, which is rather ugly and a bit inefficient. 356 | 357 | [Back to TOC](#table-of-contents) 358 | 359 | Community 360 | ========= 361 | 362 | [Back to TOC](#table-of-contents) 363 | 364 | English Mailing List 365 | -------------------- 366 | 367 | The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers. 368 | 369 | [Back to TOC](#table-of-contents) 370 | 371 | Chinese Mailing List 372 | -------------------- 373 | 374 | The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers. 375 | 376 | [Back to TOC](#table-of-contents) 377 | 378 | Bugs and Patches 379 | ================ 380 | 381 | Please report bugs or submit patches by 382 | 383 | 1. creating a ticket on the [GitHub Issue Tracker](https://github.com/openresty/lua-resty-lock/issues), 384 | 1. or posting to the [OpenResty community](#community). 385 | 386 | [Back to TOC](#table-of-contents) 387 | 388 | Author 389 | ====== 390 | 391 | Yichun "agentzh" Zhang (章亦春) , OpenResty Inc. 392 | 393 | [Back to TOC](#table-of-contents) 394 | 395 | Copyright and License 396 | ===================== 397 | 398 | This module is licensed under the BSD license. 399 | 400 | Copyright (C) 2013-2019, by Yichun "agentzh" Zhang, OpenResty Inc. 401 | 402 | All rights reserved. 403 | 404 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 405 | 406 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 407 | 408 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 409 | 410 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 411 | 412 | [Back to TOC](#table-of-contents) 413 | 414 | See Also 415 | ======== 416 | * the ngx_lua module: https://github.com/openresty/lua-nginx-module 417 | * OpenResty: http://openresty.org 418 | 419 | [Back to TOC](#table-of-contents) 420 | -------------------------------------------------------------------------------- /dist.ini: -------------------------------------------------------------------------------- 1 | name=lua-resty-lock 2 | abstract=Simple shm-based nonblocking lock API 3 | author=Yichun Zhang (agentzh) 4 | is_original=yes 5 | license=2bsd 6 | lib_dir=lib 7 | doc_dir=lib 8 | repo_link=https://github.com/openresty/lua-resty-lock 9 | main_module=lib/resty/lock.lua 10 | requires = luajit 11 | -------------------------------------------------------------------------------- /lib/resty/lock.lua: -------------------------------------------------------------------------------- 1 | -- Copyright (C) Yichun Zhang (agentzh) 2 | 3 | 4 | require "resty.core.shdict" -- enforce this to avoid dead locks 5 | 6 | local ffi = require "ffi" 7 | local ffi_new = ffi.new 8 | local shared = ngx.shared 9 | local sleep = ngx.sleep 10 | local log = ngx.log 11 | local max = math.max 12 | local min = math.min 13 | local debug = ngx.config.debug 14 | local setmetatable = setmetatable 15 | local tonumber = tonumber 16 | 17 | local _M = { _VERSION = '0.09' } 18 | local mt = { __index = _M } 19 | 20 | local ERR = ngx.ERR 21 | local FREE_LIST_REF = 0 22 | 23 | -- FIXME: we don't need this when we have __gc metamethod support on Lua 24 | -- tables. 25 | local memo = {} 26 | if debug then _M.memo = memo end 27 | 28 | 29 | local function ref_obj(key) 30 | if key == nil then 31 | return -1 32 | end 33 | local ref = memo[FREE_LIST_REF] 34 | if ref and ref ~= 0 then 35 | memo[FREE_LIST_REF] = memo[ref] 36 | 37 | else 38 | ref = #memo + 1 39 | end 40 | memo[ref] = key 41 | 42 | -- print("ref key_id returned ", ref) 43 | return ref 44 | end 45 | if debug then _M.ref_obj = ref_obj end 46 | 47 | 48 | local function unref_obj(ref) 49 | if ref >= 0 then 50 | memo[ref] = memo[FREE_LIST_REF] 51 | memo[FREE_LIST_REF] = ref 52 | end 53 | end 54 | if debug then _M.unref_obj = unref_obj end 55 | 56 | 57 | local function gc_lock(cdata) 58 | local dict_id = tonumber(cdata.dict_id) 59 | local key_id = tonumber(cdata.key_id) 60 | 61 | -- print("key_id: ", key_id, ", key: ", memo[key_id], "dict: ", 62 | -- type(memo[cdata.dict_id])) 63 | if key_id > 0 then 64 | local key = memo[key_id] 65 | unref_obj(key_id) 66 | local dict = memo[dict_id] 67 | -- print("dict.delete type: ", type(dict.delete)) 68 | local ok, err = dict:delete(key) 69 | if not ok then 70 | log(ERR, 'failed to delete key "', key, '": ', err) 71 | end 72 | cdata.key_id = 0 73 | end 74 | 75 | unref_obj(dict_id) 76 | end 77 | 78 | 79 | local ctype = ffi.metatype("struct { int key_id; int dict_id; }", 80 | { __gc = gc_lock }) 81 | 82 | 83 | function _M.new(_, dict_name, opts) 84 | local dict = shared[dict_name] 85 | if not dict then 86 | return nil, "dictionary not found" 87 | end 88 | local cdata = ffi_new(ctype) 89 | cdata.key_id = 0 90 | cdata.dict_id = ref_obj(dict) 91 | 92 | local timeout, exptime, step, ratio, max_step 93 | if opts then 94 | timeout = opts.timeout 95 | exptime = opts.exptime 96 | step = opts.step 97 | ratio = opts.ratio 98 | max_step = opts.max_step 99 | end 100 | 101 | if not exptime then 102 | exptime = 30 103 | end 104 | 105 | if timeout then 106 | timeout = min(timeout, exptime) 107 | 108 | if step then 109 | step = min(step, timeout) 110 | end 111 | end 112 | 113 | local self = { 114 | cdata = cdata, 115 | dict = dict, 116 | timeout = timeout or 5, 117 | exptime = exptime, 118 | step = step or 0.001, 119 | ratio = ratio or 2, 120 | max_step = max_step or 0.5, 121 | } 122 | setmetatable(self, mt) 123 | return self 124 | end 125 | 126 | 127 | function _M.lock(self, key) 128 | if not key then 129 | return nil, "nil key" 130 | end 131 | 132 | local dict = self.dict 133 | local cdata = self.cdata 134 | if cdata.key_id > 0 then 135 | return nil, "locked" 136 | end 137 | local exptime = self.exptime 138 | local ok, err = dict:add(key, true, exptime) 139 | if ok then 140 | cdata.key_id = ref_obj(key) 141 | self.key = key 142 | return 0 143 | end 144 | if err ~= "exists" then 145 | return nil, err 146 | end 147 | -- lock held by others 148 | local step = self.step 149 | local ratio = self.ratio 150 | local timeout = self.timeout 151 | local max_step = self.max_step 152 | local elapsed = 0 153 | while timeout > 0 do 154 | sleep(step) 155 | elapsed = elapsed + step 156 | timeout = timeout - step 157 | 158 | local ok, err = dict:add(key, true, exptime) 159 | if ok then 160 | cdata.key_id = ref_obj(key) 161 | self.key = key 162 | return elapsed 163 | end 164 | 165 | if err ~= "exists" then 166 | return nil, err 167 | end 168 | 169 | if timeout <= 0 then 170 | break 171 | end 172 | 173 | step = min(max(0.001, step * ratio), timeout, max_step) 174 | end 175 | 176 | return nil, "timeout" 177 | end 178 | 179 | 180 | function _M.unlock(self) 181 | local dict = self.dict 182 | local cdata = self.cdata 183 | local key_id = tonumber(cdata.key_id) 184 | if key_id <= 0 then 185 | return nil, "unlocked" 186 | end 187 | 188 | local key = memo[key_id] 189 | unref_obj(key_id) 190 | 191 | local ok, err = dict:delete(key) 192 | if not ok then 193 | return nil, err 194 | end 195 | cdata.key_id = 0 196 | 197 | return 1 198 | end 199 | 200 | 201 | function _M.expire(self, time) 202 | local dict = self.dict 203 | local cdata = self.cdata 204 | local key_id = tonumber(cdata.key_id) 205 | if key_id <= 0 then 206 | return nil, "unlocked" 207 | end 208 | 209 | if not time then 210 | time = self.exptime 211 | end 212 | 213 | local ok, err = dict:replace(self.key, true, time) 214 | if not ok then 215 | return nil, err 216 | end 217 | 218 | return true 219 | end 220 | 221 | 222 | return _M 223 | -------------------------------------------------------------------------------- /t/sanity.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket::Lua; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (blocks() * 3); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "../lua-resty-core/lib/?.lua;lib/?.lua;;"; 14 | lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; 15 | lua_shared_dict cache_locks 100k; 16 | }; 17 | 18 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 19 | $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; 20 | 21 | no_long_string(); 22 | #no_diff(); 23 | 24 | run_tests(); 25 | 26 | __DATA__ 27 | 28 | === TEST 1: lock is subject to garbage collection 29 | --- http_config eval: $::HttpConfig 30 | --- config 31 | location = /t { 32 | content_by_lua_block { 33 | local lock = require "resty.lock" 34 | for i = 1, 2 do 35 | collectgarbage("collect") 36 | local lock = lock:new("cache_locks") 37 | local elapsed, err = lock:lock("foo") 38 | ngx.say("lock: ", elapsed, ", ", err) 39 | end 40 | collectgarbage("collect") 41 | } 42 | } 43 | --- request 44 | GET /t 45 | --- response_body 46 | lock: 0, nil 47 | lock: 0, nil 48 | 49 | --- no_error_log 50 | [error] 51 | 52 | 53 | 54 | === TEST 2: serial lock and unlock 55 | --- http_config eval: $::HttpConfig 56 | --- config 57 | location = /t { 58 | content_by_lua_block { 59 | local lock = require "resty.lock" 60 | for i = 1, 2 do 61 | local lock = lock:new("cache_locks") 62 | local elapsed, err = lock:lock("foo") 63 | ngx.say("lock: ", elapsed, ", ", err) 64 | local ok, err = lock:unlock() 65 | if not ok then 66 | ngx.say("failed to unlock: ", err) 67 | end 68 | ngx.say("unlock: ", ok) 69 | end 70 | } 71 | } 72 | --- request 73 | GET /t 74 | --- response_body 75 | lock: 0, nil 76 | unlock: 1 77 | lock: 0, nil 78 | unlock: 1 79 | 80 | --- no_error_log 81 | [error] 82 | 83 | 84 | 85 | === TEST 3: timed out locks 86 | --- http_config eval: $::HttpConfig 87 | --- config 88 | location = /t { 89 | content_by_lua_block { 90 | local lock = require "resty.lock" 91 | for i = 1, 2 do 92 | local lock1 = lock:new("cache_locks", { timeout = 0.01 }) 93 | local lock2 = lock:new("cache_locks", { timeout = 0.01 }) 94 | 95 | local elapsed, err = lock1:lock("foo") 96 | ngx.say("lock 1: lock: ", elapsed, ", ", err) 97 | 98 | local elapsed, err = lock2:lock("foo") 99 | ngx.say("lock 2: lock: ", elapsed, ", ", err) 100 | 101 | local ok, err = lock1:unlock() 102 | ngx.say("lock 1: unlock: ", ok, ", ", err) 103 | 104 | local ok, err = lock2:unlock() 105 | ngx.say("lock 2: unlock: ", ok, ", ", err) 106 | end 107 | } 108 | } 109 | --- request 110 | GET /t 111 | --- response_body 112 | lock 1: lock: 0, nil 113 | lock 2: lock: nil, timeout 114 | lock 1: unlock: 1, nil 115 | lock 2: unlock: nil, unlocked 116 | lock 1: lock: 0, nil 117 | lock 2: lock: nil, timeout 118 | lock 1: unlock: 1, nil 119 | lock 2: unlock: nil, unlocked 120 | 121 | --- no_error_log 122 | [error] 123 | 124 | 125 | 126 | === TEST 4: waited locks 127 | --- http_config eval: $::HttpConfig 128 | --- config 129 | location = /t { 130 | content_by_lua_block { 131 | local resty_lock = require "resty.lock" 132 | local key = "blah" 133 | local t, err = ngx.thread.spawn(function () 134 | local lock = resty_lock:new("cache_locks") 135 | local elapsed, err = lock:lock(key) 136 | ngx.say("sub thread: lock: ", elapsed, " ", err) 137 | ngx.sleep(0.1) 138 | ngx.say("sub thread: unlock: ", lock:unlock(key)) 139 | end) 140 | 141 | local lock = resty_lock:new("cache_locks") 142 | local elapsed, err = lock:lock(key) 143 | ngx.say("main thread: lock: ", elapsed, " ", err) 144 | ngx.say("main thread: unlock: ", lock:unlock()) 145 | } 146 | } 147 | --- request 148 | GET /t 149 | --- response_body_like chop 150 | ^sub thread: lock: 0 nil 151 | sub thread: unlock: 1 152 | main thread: lock: 0.12[6-9] nil 153 | main thread: unlock: 1 154 | $ 155 | --- no_error_log 156 | [error] 157 | 158 | 159 | 160 | === TEST 5: waited locks (custom step) 161 | --- http_config eval: $::HttpConfig 162 | --- config 163 | location = /t { 164 | content_by_lua_block { 165 | local resty_lock = require "resty.lock" 166 | local key = "blah" 167 | local t, err = ngx.thread.spawn(function () 168 | local lock = resty_lock:new("cache_locks") 169 | local elapsed, err = lock:lock(key) 170 | ngx.say("sub thread: lock: ", elapsed, " ", err) 171 | ngx.sleep(0.1) 172 | ngx.say("sub thread: unlock: ", lock:unlock(key)) 173 | end) 174 | 175 | local lock = resty_lock:new("cache_locks", { step = 0.01 }) 176 | local elapsed, err = lock:lock(key) 177 | ngx.say("main thread: lock: ", elapsed, " ", err) 178 | ngx.say("main thread: unlock: ", lock:unlock()) 179 | } 180 | } 181 | --- request 182 | GET /t 183 | --- response_body_like chop 184 | ^sub thread: lock: 0 nil 185 | sub thread: unlock: 1 186 | main thread: lock: 0.1[4-5]\d* nil 187 | main thread: unlock: 1 188 | $ 189 | --- no_error_log 190 | [error] 191 | 192 | 193 | 194 | === TEST 6: waited locks (custom ratio) 195 | --- http_config eval: $::HttpConfig 196 | --- config 197 | location = /t { 198 | content_by_lua_block { 199 | local resty_lock = require "resty.lock" 200 | local key = "blah" 201 | local t, err = ngx.thread.spawn(function () 202 | local lock = resty_lock:new("cache_locks") 203 | local elapsed, err = lock:lock(key) 204 | ngx.say("sub thread: lock: ", elapsed, " ", err) 205 | ngx.sleep(0.1) 206 | ngx.say("sub thread: unlock: ", lock:unlock(key)) 207 | end) 208 | 209 | local lock = resty_lock:new("cache_locks", { ratio = 3 }) 210 | local elapsed, err = lock:lock(key) 211 | ngx.say("main thread: lock: ", elapsed, " ", err) 212 | ngx.say("main thread: unlock: ", lock:unlock()) 213 | } 214 | } 215 | --- request 216 | GET /t 217 | --- response_body_like chop 218 | ^sub thread: lock: 0 nil 219 | sub thread: unlock: 1 220 | main thread: lock: 0.1[2]\d* nil 221 | main thread: unlock: 1 222 | $ 223 | --- no_error_log 224 | [error] 225 | 226 | 227 | 228 | === TEST 7: waited locks (custom max step) 229 | --- http_config eval: $::HttpConfig 230 | --- config 231 | location = /t { 232 | content_by_lua_block { 233 | local resty_lock = require "resty.lock" 234 | local key = "blah" 235 | local t, err = ngx.thread.spawn(function () 236 | local lock = resty_lock:new("cache_locks") 237 | local elapsed, err = lock:lock(key) 238 | ngx.say("sub thread: lock: ", elapsed, " ", err) 239 | ngx.sleep(0.1) 240 | ngx.say("sub thread: unlock: ", lock:unlock(key)) 241 | end) 242 | 243 | local lock = resty_lock:new("cache_locks", { max_step = 0.05 }) 244 | local elapsed, err = lock:lock(key) 245 | ngx.say("main thread: lock: ", elapsed, " ", err) 246 | ngx.say("main thread: unlock: ", lock:unlock()) 247 | } 248 | } 249 | --- request 250 | GET /t 251 | --- response_body_like chop 252 | ^sub thread: lock: 0 nil 253 | sub thread: unlock: 1 254 | main thread: lock: 0.11[2-4]\d* nil 255 | main thread: unlock: 1 256 | $ 257 | --- no_error_log 258 | [error] 259 | 260 | 261 | 262 | === TEST 8: lock expired by itself 263 | --- http_config eval: $::HttpConfig 264 | --- config 265 | location = /t { 266 | content_by_lua_block { 267 | local resty_lock = require "resty.lock" 268 | local key = "blah" 269 | local t, err = ngx.thread.spawn(function () 270 | local lock = resty_lock:new("cache_locks", { exptime = 0.1 }) 271 | local elapsed, err = lock:lock(key) 272 | ngx.say("sub thread: lock: ", elapsed, " ", err) 273 | ngx.sleep(0.1) 274 | -- ngx.say("sub thread: unlock: ", lock:unlock(key)) 275 | end) 276 | 277 | local lock = resty_lock:new("cache_locks", { max_step = 0.05 }) 278 | local elapsed, err = lock:lock(key) 279 | ngx.say("main thread: lock: ", elapsed, " ", err) 280 | ngx.say("main thread: unlock: ", lock:unlock()) 281 | } 282 | } 283 | --- request 284 | GET /t 285 | --- response_body_like chop 286 | ^sub thread: lock: 0 nil 287 | main thread: lock: 0.11[2-4]\d* nil 288 | main thread: unlock: 1 289 | $ 290 | --- no_error_log 291 | [error] 292 | 293 | 294 | 295 | === TEST 9: ref & unref (1 at most) 296 | --- http_config eval: $::HttpConfig 297 | --- config 298 | location = /t { 299 | content_by_lua_block { 300 | local lock = require "resty.lock" 301 | local memo = lock.memo 302 | local ref = lock.ref_obj("foo") 303 | ngx.say(#memo) 304 | lock.unref_obj(ref) 305 | ngx.say(#memo) 306 | ref = lock.ref_obj("bar") 307 | ngx.say(#memo) 308 | lock.unref_obj(ref) 309 | ngx.say(#memo) 310 | } 311 | } 312 | --- request 313 | GET /t 314 | --- response_body 315 | 1 316 | 0 317 | 1 318 | 0 319 | 320 | --- no_error_log 321 | [error] 322 | --- skip_eval: 3: system("$NginxBinary -V 2>&1 | grep -- '--with-debug'") ne 0 323 | 324 | 325 | 326 | === TEST 10: ref & unref (2 at most) 327 | --- http_config eval: $::HttpConfig 328 | --- config 329 | location = /t { 330 | content_by_lua_block { 331 | local lock = require "resty.lock" 332 | local memo = lock.memo 333 | 334 | for i = 1, 2 do 335 | local refs = {} 336 | 337 | refs[1] = lock.ref_obj("foo") 338 | ngx.say(#memo) 339 | 340 | refs[2] = lock.ref_obj("bar") 341 | ngx.say(#memo) 342 | 343 | lock.unref_obj(refs[1]) 344 | ngx.say(#memo) 345 | 346 | lock.unref_obj(refs[2]) 347 | ngx.say(#memo) 348 | end 349 | } 350 | } 351 | --- request 352 | GET /t 353 | --- response_body 354 | 1 355 | 2 356 | 2 357 | 2 358 | 2 359 | 2 360 | 1 361 | 1 362 | 363 | --- no_error_log 364 | [error] 365 | --- skip_eval: 3: system("$NginxBinary -V 2>&1 | grep -- '--with-debug'") ne 0 366 | 367 | 368 | 369 | === TEST 11: lock on a nil key 370 | --- http_config eval: $::HttpConfig 371 | --- config 372 | location = /t { 373 | content_by_lua_block { 374 | local lock = require "resty.lock" 375 | local lock = lock:new("cache_locks") 376 | local elapsed, err = lock:lock(nil) 377 | if elapsed then 378 | ngx.say("lock: ", elapsed, ", ", err) 379 | local ok, err = lock:unlock() 380 | if not ok then 381 | ngx.say("failed to unlock: ", err) 382 | end 383 | else 384 | ngx.say("failed to lock: ", err) 385 | end 386 | } 387 | } 388 | --- request 389 | GET /t 390 | --- response_body 391 | failed to lock: nil key 392 | 393 | --- no_error_log 394 | [error] 395 | 396 | 397 | 398 | === TEST 12: same shdict, multple locks 399 | --- http_config eval: $::HttpConfig 400 | --- config 401 | location = /t { 402 | content_by_lua_block { 403 | local lock = require "resty.lock" 404 | local memo = lock.memo 405 | local lock1 = lock:new("cache_locks", { timeout = 0.01 }) 406 | for i = 1, 3 do 407 | lock1:lock("lock_key") 408 | lock1:unlock() 409 | collectgarbage("collect") 410 | end 411 | 412 | local lock2 = lock:new("cache_locks", { timeout = 0.01 }) 413 | local lock3 = lock:new("cache_locks", { timeout = 0.01 }) 414 | lock2:lock("lock_key") 415 | lock3:lock("lock_key") 416 | collectgarbage("collect") 417 | 418 | ngx.say(#memo) 419 | 420 | lock2:unlock() 421 | lock3:unlock() 422 | collectgarbage("collect") 423 | } 424 | } 425 | --- request 426 | GET /t 427 | --- response_body 428 | 4 429 | --- no_error_log 430 | [error] 431 | --- skip_eval: 3: system("$NginxBinary -V 2>&1 | grep -- '--with-debug'") ne 0 432 | 433 | 434 | 435 | === TEST 13: timed out locks (0 timeout) 436 | --- http_config eval: $::HttpConfig 437 | --- config 438 | location = /t { 439 | content_by_lua_block { 440 | local lock = require "resty.lock" 441 | for i = 1, 2 do 442 | local lock1 = lock:new("cache_locks", { timeout = 0 }) 443 | local lock2 = lock:new("cache_locks", { timeout = 0 }) 444 | 445 | local elapsed, err = lock1:lock("foo") 446 | ngx.say("lock 1: lock: ", elapsed, ", ", err) 447 | 448 | local elapsed, err = lock2:lock("foo") 449 | ngx.say("lock 2: lock: ", elapsed, ", ", err) 450 | 451 | local ok, err = lock1:unlock() 452 | ngx.say("lock 1: unlock: ", ok, ", ", err) 453 | 454 | local ok, err = lock2:unlock() 455 | ngx.say("lock 2: unlock: ", ok, ", ", err) 456 | end 457 | } 458 | } 459 | --- request 460 | GET /t 461 | --- response_body 462 | lock 1: lock: 0, nil 463 | lock 2: lock: nil, timeout 464 | lock 1: unlock: 1, nil 465 | lock 2: unlock: nil, unlocked 466 | lock 1: lock: 0, nil 467 | lock 2: lock: nil, timeout 468 | lock 1: unlock: 1, nil 469 | lock 2: unlock: nil, unlocked 470 | 471 | --- no_error_log 472 | [error] 473 | 474 | 475 | 476 | === TEST 13: expire() 477 | --- http_config eval: $::HttpConfig 478 | --- config 479 | location = /t { 480 | content_by_lua_block { 481 | local lock = require "resty.lock" 482 | for i = 1, 2 do 483 | local lock1 = lock:new("cache_locks", { timeout = 0, exptime = 0.1 }) 484 | local lock2 = lock:new("cache_locks", { timeout = 0, exptime = 0.1 }) 485 | 486 | local exp, err = lock1:expire() 487 | ngx.say("lock 1: expire: ", exp, ", ", err) 488 | 489 | local elapsed, err = lock1:lock("foo") 490 | ngx.say("lock 1: lock: ", elapsed, ", ", err) 491 | 492 | ngx.sleep(0.06) 493 | 494 | local exp, err = lock1:expire() 495 | ngx.say("lock 1: expire: ", exp, ", ", err) 496 | 497 | ngx.sleep(0.06) 498 | 499 | local elapsed, err = lock2:lock("foo") 500 | ngx.say("lock 2: lock: ", elapsed, ", ", err) 501 | 502 | local exp, err = lock1:expire(0.2) 503 | ngx.say("lock 1: expire: ", exp, ", ", err) 504 | 505 | ngx.sleep(0.15) 506 | 507 | local elapsed, err = lock2:lock("foo") 508 | ngx.say("lock 2: lock: ", elapsed, ", ", err) 509 | 510 | ngx.sleep(0.1) 511 | 512 | local elapsed, err = lock2:lock("foo") 513 | ngx.say("lock 2: lock: ", elapsed, ", ", err) 514 | 515 | local ok, err = lock2:unlock() 516 | ngx.say("lock 2: unlock: ", ok, ", ", err) 517 | 518 | local exp, err = lock2:expire(0.2) 519 | ngx.say("lock 2: expire: ", exp, ", ", err) 520 | end 521 | } 522 | } 523 | --- request 524 | GET /t 525 | --- response_body 526 | lock 1: expire: nil, unlocked 527 | lock 1: lock: 0, nil 528 | lock 1: expire: true, nil 529 | lock 2: lock: nil, timeout 530 | lock 1: expire: true, nil 531 | lock 2: lock: nil, timeout 532 | lock 2: lock: 0, nil 533 | lock 2: unlock: 1, nil 534 | lock 2: expire: nil, unlocked 535 | lock 1: expire: nil, unlocked 536 | lock 1: lock: 0, nil 537 | lock 1: expire: true, nil 538 | lock 2: lock: nil, timeout 539 | lock 1: expire: true, nil 540 | lock 2: lock: nil, timeout 541 | lock 2: lock: 0, nil 542 | lock 2: unlock: 1, nil 543 | lock 2: expire: nil, unlocked 544 | 545 | --- no_error_log 546 | [error] 547 | -------------------------------------------------------------------------------- /valgrind.suppress: -------------------------------------------------------------------------------- 1 | { 2 | 3 | Memcheck:Leak 4 | fun:malloc 5 | fun:ngx_alloc 6 | fun:ngx_event_process_init 7 | } 8 | { 9 | 10 | Memcheck:Param 11 | epoll_ctl(event) 12 | fun:epoll_ctl 13 | fun:ngx_epoll_add_event 14 | } 15 | { 16 | 17 | Memcheck:Cond 18 | fun:index 19 | fun:expand_dynamic_string_token 20 | fun:_dl_map_object 21 | fun:map_doit 22 | fun:_dl_catch_error 23 | fun:do_preload 24 | fun:dl_main 25 | fun:_dl_sysdep_start 26 | fun:_dl_start 27 | } 28 | { 29 | 30 | Memcheck:Param 31 | epoll_ctl(event) 32 | fun:epoll_ctl 33 | fun:ngx_epoll_init 34 | fun:ngx_event_process_init 35 | } 36 | { 37 | 38 | Memcheck:Param 39 | epoll_ctl(event) 40 | fun:epoll_ctl 41 | fun:ngx_epoll_notify_init 42 | fun:ngx_epoll_init 43 | } 44 | { 45 | 46 | Memcheck:Param 47 | epoll_ctl(event) 48 | fun:epoll_ctl 49 | fun:ngx_epoll_test_rdhup 50 | } 51 | { 52 | 53 | Memcheck:Leak 54 | match-leak-kinds: definite 55 | fun:malloc 56 | fun:ngx_alloc 57 | fun:ngx_set_environment 58 | fun:ngx_single_process_cycle 59 | } 60 | { 61 | 62 | Memcheck:Leak 63 | match-leak-kinds: definite 64 | fun:malloc 65 | fun:ngx_alloc 66 | fun:ngx_set_environment 67 | fun:ngx_worker_process_init 68 | fun:ngx_worker_process_cycle 69 | } 70 | --------------------------------------------------------------------------------