Skip to content

Commit df1b019

Browse files
spacewanderagentzh
authored andcommitted
optimize: corrected the initial table size of req socket objects.
The req socket uses two slots in array and three slots in hash at most. At first it uses one array slot and one hash slot: array part: upstream udata hash part: __index After calling settimeouts, it uses two array slots and three hash slots: array part: [1] upstream udata [2] connect timeout hash part: __index [4] send timeout [5] read timeout Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 9887569 commit df1b019

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/ngx_http_lua_socket_tcp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4390,7 +4390,7 @@ ngx_http_lua_req_socket(lua_State *L)
43904390
r->request_body = rb;
43914391
}
43924392

4393-
lua_createtable(L, 3 /* narr */, 1 /* nrec */); /* the object */
4393+
lua_createtable(L, 2 /* narr */, 3 /* nrec */); /* the object */
43944394

43954395
if (raw) {
43964396
lua_pushlightuserdata(L, &ngx_http_lua_raw_req_socket_metatable_key);

t/062-count.t

+74
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,77 @@ GET /test
494494
n = 6
495495
--- no_error_log
496496
[error]
497+
498+
499+
500+
=== TEST 22: entries under the req raw sockets
501+
--- config
502+
location = /test {
503+
content_by_lua_block {
504+
local narr = 0
505+
local nrec = 0
506+
ngx.req.read_body()
507+
local sock, err = ngx.req.socket(true)
508+
if not sock then
509+
ngx.log(ngx.ERR, "server: failed to get raw req socket: ", err)
510+
return
511+
end
512+
sock:settimeouts(1, 2, 3)
513+
for k, v in ipairs(sock) do
514+
narr = narr + 1
515+
end
516+
for k, v in pairs(sock) do
517+
nrec = nrec + 1
518+
end
519+
-- include '__index'
520+
nrec = nrec - narr + 1
521+
522+
local ok, err = sock:send("HTTP/1.1 200 OK\r\n\r\nnarr = "..narr.."\nnrec = "..nrec.."\n")
523+
if not ok then
524+
ngx.log(ngx.ERR, "failed to send: ", err)
525+
return
526+
end
527+
}
528+
}
529+
--- request
530+
GET /test
531+
--- response_body
532+
narr = 2
533+
nrec = 3
534+
--- no_error_log
535+
[error]
536+
537+
538+
539+
=== TEST 23: entries under the req sockets
540+
--- config
541+
location = /test {
542+
content_by_lua_block {
543+
local narr = 0
544+
local nrec = 0
545+
local sock, err = ngx.req.socket()
546+
if not sock then
547+
ngx.log(ngx.ERR, "server: failed to get req socket: ", err)
548+
return
549+
end
550+
sock:settimeouts(1, 2, 3)
551+
for k, v in ipairs(sock) do
552+
narr = narr + 1
553+
end
554+
for k, v in pairs(sock) do
555+
nrec = nrec + 1
556+
end
557+
-- include '__index'
558+
nrec = nrec - narr + 1
559+
560+
ngx.say("narr = "..narr.."\nnrec = "..nrec)
561+
}
562+
}
563+
--- request
564+
POST /test
565+
hello world
566+
--- response_body
567+
narr = 2
568+
nrec = 3
569+
--- no_error_log
570+
[error]

0 commit comments

Comments
 (0)