Skip to content

Commit 1690add

Browse files
committed
docs: added a warning for ngx.var.VARIABLE that memory is allocated in the per-request memory pool. also made it clear why "return" is recommended to be used with ngx.exit(). thanks Antoine.
1 parent 4d8cbe9 commit 1690add

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

README

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,18 @@ Nginx API for Lua
14161416

14171417
ngx.var.args = nil
14181418

1419+
WARNING When reading from an Nginx variable, Nginx will allocate memory
1420+
in the per-request memory pool which is freed only at request
1421+
termination. So when you need to read from an Nginx variable repeatedly
1422+
in your Lua code, cache the Nginx variable value to your own Lua
1423+
variable, for example,
1424+
1425+
local val = ngx.var.some_var
1426+
--- use the val repeatedly later
1427+
1428+
to prevent (temporary) memory leaking within the current request's
1429+
lifetime.
1430+
14191431
Core constants
14201432
context: *init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*,
14211433
content_by_lua*, header_filter_by_lua*, body_filter_by_lua,
@@ -3031,8 +3043,9 @@ Nginx API for Lua
30313043
Note that while this method accepts all HTTP status constants as input,
30323044
it only accepts "NGX_OK" and "NGX_ERROR" of the core constants.
30333045

3034-
It is strongly recommended to combine the "return" statement with this
3035-
call, i.e., "return ngx.exit(...)".
3046+
It is recommended, though not necessary, to combine the "return"
3047+
statement with this call, i.e., "return ngx.exit(...)", to give a visual
3048+
hint to others reading the code.
30363049

30373050
ngx.eof
30383051
syntax: *ngx.eof()*

README.markdown

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,15 @@ Setting `ngx.var.Foo` to a `nil` value will unset the `$Foo` Nginx variable.
12601260
ngx.var.args = nil
12611261

12621262

1263+
**WARNING** When reading from an Nginx variable, Nginx will allocate memory in the per-request memory pool which is freed only at request termination. So when you need to read from an Nginx variable repeatedly in your Lua code, cache the Nginx variable value to your own Lua variable, for example,
1264+
1265+
1266+
local val = ngx.var.some_var
1267+
--- use the val repeatedly later
1268+
1269+
1270+
to prevent (temporary) memory leaking within the current request's lifetime.
1271+
12631272
Core constants
12641273
--------------
12651274
**context:** *init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, *log_by_lua**
@@ -2791,7 +2800,7 @@ Number literals can be used directly as the argument, for instance,
27912800

27922801
Note that while this method accepts all [HTTP status constants](http://wiki.nginx.org/HttpLuaModule#HTTP_status_constants) as input, it only accepts `NGX_OK` and `NGX_ERROR` of the [core constants](http://wiki.nginx.org/HttpLuaModule#core_constants).
27932802

2794-
It is strongly recommended to combine the `return` statement with this call, i.e., `return ngx.exit(...)`.
2803+
It is recommended, though not necessary, to combine the `return` statement with this call, i.e., `return ngx.exit(...)`, to give a visual hint to others reading the code.
27952804

27962805
ngx.eof
27972806
-------

doc/HttpLuaModule.wiki

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,15 @@ Setting <code>ngx.var.Foo</code> to a <code>nil</code> value will unset the <cod
12121212
ngx.var.args = nil
12131213
</geshi>
12141214
1215+
'''WARNING''' When reading from an Nginx variable, Nginx will allocate memory in the per-request memory pool which is freed only at request termination. So when you need to read from an Nginx variable repeatedly in your Lua code, cache the Nginx variable value to your own Lua variable, for example,
1216+
1217+
<geshi lang="lua">
1218+
local val = ngx.var.some_var
1219+
--- use the val repeatedly later
1220+
</geshi>
1221+
1222+
to prevent (temporary) memory leaking within the current request's lifetime.
1223+
12151224
== Core constants ==
12161225
'''context:''' ''init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, *log_by_lua*''
12171226
@@ -2705,7 +2714,7 @@ Number literals can be used directly as the argument, for instance,
27052714
27062715
Note that while this method accepts all [[#HTTP status constants|HTTP status constants]] as input, it only accepts <code>NGX_OK</code> and <code>NGX_ERROR</code> of the [[#core constants|core constants]].
27072716
2708-
It is strongly recommended to combine the <code>return</code> statement with this call, i.e., <code>return ngx.exit(...)</code>.
2717+
It is recommended, though not necessary, to combine the <code>return</code> statement with this call, i.e., <code>return ngx.exit(...)</code>, to give a visual hint to others reading the code.
27092718
27102719
== ngx.eof ==
27112720
'''syntax:''' ''ngx.eof()''

0 commit comments

Comments
 (0)