Skip to content

Commit 8ac6cc7

Browse files
rainingmasteragentzh
authored andcommitted
doc: made the code examples more realistic (better for direct copy&paste use).
1 parent d278043 commit 8ac6cc7

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

README.markdown

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,13 +1512,17 @@ This hook is often used to create per-worker reoccurring timers (via the [ngx.ti
15121512
return
15131513
end
15141514
end
1515+
1516+
-- do something in timer
15151517
end
15161518
15171519
local hdl, err = new_timer(delay, check)
15181520
if not hdl then
15191521
log(ERR, "failed to create timer: ", err)
15201522
return
15211523
end
1524+
1525+
-- other job in init_worker_by_lua
15221526
';
15231527
```
15241528

@@ -6292,13 +6296,13 @@ When the `replace` is a string, then it is treated as a special template for str
62926296
```lua
62936297

62946298
local newstr, n, err = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
6295-
if newstr then
6296-
-- newstr == "hello, [12][1]34"
6297-
-- n == 1
6298-
else
6299+
if not newstr then
62996300
ngx.log(ngx.ERR, "error: ", err)
63006301
return
63016302
end
6303+
6304+
-- newstr == "hello, [12][1]34"
6305+
-- n == 1
63026306
```
63036307

63046308
where `$0` referring to the whole substring matched by the pattern and `$1` referring to the first parenthesized capturing substring.
@@ -6308,17 +6312,17 @@ Curly braces can also be used to disambiguate variable names from the background
63086312
```lua
63096313

63106314
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
6311-
-- newstr == "hello, 100234"
6312-
-- n == 1
6315+
-- newstr == "hello, 100234"
6316+
-- n == 1
63136317
```
63146318

63156319
Literal dollar sign characters (`$`) in the `replace` string argument can be escaped by another dollar sign, for instance,
63166320

63176321
```lua
63186322

63196323
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "$$")
6320-
-- newstr == "hello, $234"
6321-
-- n == 1
6324+
-- newstr == "hello, $234"
6325+
-- n == 1
63226326
```
63236327

63246328
Do not use backlashes to escape dollar signs; it will not work as expected.
@@ -6330,9 +6334,10 @@ When the `replace` argument is of type "function", then it will be invoked with
63306334
local func = function (m)
63316335
return "[" .. m[0] .. "][" .. m[1] .. "]"
63326336
end
6337+
63336338
local newstr, n, err = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
6334-
-- newstr == "hello, [12][1]34"
6335-
-- n == 1
6339+
-- newstr == "hello, [12][1]34"
6340+
-- n == 1
63366341
```
63376342

63386343
The dollar sign characters in the return value of the `replace` function argument are not special at all.
@@ -6357,13 +6362,13 @@ Here is some examples:
63576362
```lua
63586363

63596364
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
6360-
if newstr then
6361-
-- newstr == "[hello,h], [world,w]"
6362-
-- n == 2
6363-
else
6365+
if not newstr then
63646366
ngx.log(ngx.ERR, "error: ", err)
63656367
return
63666368
end
6369+
6370+
-- newstr == "[hello,h], [world,w]"
6371+
-- n == 2
63676372
```
63686373

63696374
```lua
@@ -6372,8 +6377,8 @@ Here is some examples:
63726377
return "[" .. m[0] .. "," .. m[1] .. "]"
63736378
end
63746379
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
6375-
-- newstr == "[hello,h], [world,w]"
6376-
-- n == 2
6380+
-- newstr == "[hello,h], [world,w]"
6381+
-- n == 2
63776382
```
63786383

63796384
This method requires the PCRE library enabled in Nginx ([Known Issue With Special Escaping Sequences](#special-escaping-sequences)).
@@ -7078,6 +7083,9 @@ Since the `v0.7.18` release, connecting to a datagram unix domain socket file is
70787083
ngx.say("failed to connect to the datagram unix domain socket: ", err)
70797084
return
70807085
end
7086+
7087+
-- do something after connect
7088+
-- such as sock:send or sock:receive
70817089
```
70827090

70837091
assuming the datagram service is listening on the unix domain socket file `/tmp/some-datagram-service.sock` and the client socket will use the "autobind" feature on Linux.
@@ -7282,6 +7290,9 @@ Connecting to a Unix Domain Socket file is also possible:
72827290
ngx.say("failed to connect to the memcached unix domain socket: ", err)
72837291
return
72847292
end
7293+
7294+
-- do something after connect
7295+
-- such as sock:send or sock:receive
72857296
```
72867297

72877298
assuming memcached (or something else) is listening on the unix domain socket file `/tmp/memcached.sock`.
@@ -8160,6 +8171,8 @@ Here is a simple example:
81608171
ngx.log(ngx.ERR, "failed to create timer: ", err)
81618172
return
81628173
end
8174+
8175+
-- other job in log_by_lua_block
81638176
}
81648177
}
81658178
```
@@ -8180,13 +8193,17 @@ One can also create infinite re-occurring timers, for instance, a timer getting
81808193
ngx.log(ngx.ERR, "failed to create the timer: ", err)
81818194
return
81828195
end
8196+
8197+
-- do something in timer
81838198
end
81848199

81858200
local ok, err = ngx.timer.at(delay, handler)
81868201
if not ok then
81878202
ngx.log(ngx.ERR, "failed to create the timer: ", err)
81888203
return
81898204
end
8205+
8206+
-- do other jobs
81908207
```
81918208

81928209
It is recommended, however, to use the [ngx.timer.every](#ngxtimerevery) API function

doc/HttpLuaModule.wiki

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,13 +1224,17 @@ This hook is often used to create per-worker reoccurring timers (via the [[#ngx.
12241224
return
12251225
end
12261226
end
1227+
1228+
-- do something in timer
12271229
end
12281230
12291231
local hdl, err = new_timer(delay, check)
12301232
if not hdl then
12311233
log(ERR, "failed to create timer: ", err)
12321234
return
12331235
end
1236+
1237+
-- other job in init_worker_by_lua
12341238
';
12351239
</geshi>
12361240
@@ -5296,13 +5300,13 @@ When the <code>replace</code> is a string, then it is treated as a special templ
52965300
52975301
<geshi lang="lua">
52985302
local newstr, n, err = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
5299-
if newstr then
5300-
-- newstr == "hello, [12][1]34"
5301-
-- n == 1
5302-
else
5303+
if not newstr then
53035304
ngx.log(ngx.ERR, "error: ", err)
53045305
return
53055306
end
5307+
5308+
-- newstr == "hello, [12][1]34"
5309+
-- n == 1
53065310
</geshi>
53075311
53085312
where <code>$0</code> referring to the whole substring matched by the pattern and <code>$1</code> referring to the first parenthesized capturing substring.
@@ -5311,16 +5315,16 @@ Curly braces can also be used to disambiguate variable names from the background
53115315
53125316
<geshi lang="lua">
53135317
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
5314-
-- newstr == "hello, 100234"
5315-
-- n == 1
5318+
-- newstr == "hello, 100234"
5319+
-- n == 1
53165320
</geshi>
53175321
53185322
Literal dollar sign characters (<code>$</code>) in the <code>replace</code> string argument can be escaped by another dollar sign, for instance,
53195323
53205324
<geshi lang="lua">
53215325
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "$$")
5322-
-- newstr == "hello, $234"
5323-
-- n == 1
5326+
-- newstr == "hello, $234"
5327+
-- n == 1
53245328
</geshi>
53255329
53265330
Do not use backlashes to escape dollar signs; it will not work as expected.
@@ -5331,9 +5335,10 @@ When the <code>replace</code> argument is of type "function", then it will be in
53315335
local func = function (m)
53325336
return "[" .. m[0] .. "][" .. m[1] .. "]"
53335337
end
5338+
53345339
local newstr, n, err = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
5335-
-- newstr == "hello, [12][1]34"
5336-
-- n == 1
5340+
-- newstr == "hello, [12][1]34"
5341+
-- n == 1
53375342
</geshi>
53385343
53395344
The dollar sign characters in the return value of the <code>replace</code> function argument are not special at all.
@@ -5354,22 +5359,22 @@ Here is some examples:
53545359
53555360
<geshi lang="lua">
53565361
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
5357-
if newstr then
5358-
-- newstr == "[hello,h], [world,w]"
5359-
-- n == 2
5360-
else
5362+
if not newstr then
53615363
ngx.log(ngx.ERR, "error: ", err)
53625364
return
53635365
end
5366+
5367+
-- newstr == "[hello,h], [world,w]"
5368+
-- n == 2
53645369
</geshi>
53655370
53665371
<geshi lang="lua">
53675372
local func = function (m)
53685373
return "[" .. m[0] .. "," .. m[1] .. "]"
53695374
end
53705375
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
5371-
-- newstr == "[hello,h], [world,w]"
5372-
-- n == 2
5376+
-- newstr == "[hello,h], [world,w]"
5377+
-- n == 2
53735378
</geshi>
53745379
53755380
This method requires the PCRE library enabled in Nginx ([[#Special Escaping Sequences|Known Issue With Special Escaping Sequences]]).
@@ -5988,6 +5993,9 @@ Since the <code>v0.7.18</code> release, connecting to a datagram unix domain soc
59885993
ngx.say("failed to connect to the datagram unix domain socket: ", err)
59895994
return
59905995
end
5996+
5997+
-- do something after connect
5998+
-- such as sock:send or sock:receive
59915999
</geshi>
59926000
59936001
assuming the datagram service is listening on the unix domain socket file <code>/tmp/some-datagram-service.sock</code> and the client socket will use the "autobind" feature on Linux.
@@ -6167,6 +6175,9 @@ Connecting to a Unix Domain Socket file is also possible:
61676175
ngx.say("failed to connect to the memcached unix domain socket: ", err)
61686176
return
61696177
end
6178+
6179+
-- do something after connect
6180+
-- such as sock:send or sock:receive
61706181
</geshi>
61716182
61726183
assuming memcached (or something else) is listening on the unix domain socket file <code>/tmp/memcached.sock</code>.
@@ -6976,6 +6987,8 @@ Here is a simple example:
69766987
ngx.log(ngx.ERR, "failed to create timer: ", err)
69776988
return
69786989
end
6990+
6991+
-- other job in log_by_lua_block
69796992
}
69806993
}
69816994
</geshi>
@@ -6995,13 +7008,17 @@ One can also create infinite re-occurring timers, for instance, a timer getting
69957008
ngx.log(ngx.ERR, "failed to create the timer: ", err)
69967009
return
69977010
end
7011+
7012+
-- do something in timer
69987013
end
69997014
70007015
local ok, err = ngx.timer.at(delay, handler)
70017016
if not ok then
70027017
ngx.log(ngx.ERR, "failed to create the timer: ", err)
70037018
return
70047019
end
7020+
7021+
-- do other jobs
70057022
</geshi>
70067023
70077024
It is recommended, however, to use the [[#ngx.timer.every|ngx.timer.every]] API function

0 commit comments

Comments
 (0)