Skip to content

Commit

Permalink
feature: cosockets are now full duplex: a reader "light thread" and a…
Browse files Browse the repository at this point in the history
… writer "light thread" can operate on the same cosocket simultaneously. thanks shun zhang and aviramc for the original patches in #367 and #290, respectively.
  • Loading branch information
agentzh committed Jun 26, 2014
1 parent 6885462 commit 934e33e
Show file tree
Hide file tree
Showing 13 changed files with 1,289 additions and 231 deletions.
706 changes: 537 additions & 169 deletions src/ngx_http_lua_socket_tcp.c

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/ngx_http_lua_socket_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ typedef struct {


struct ngx_http_lua_socket_tcp_upstream_s {
ngx_http_lua_socket_tcp_retval_handler prepare_retvals;
ngx_http_lua_socket_tcp_retval_handler read_prepare_retvals;
ngx_http_lua_socket_tcp_retval_handler write_prepare_retvals;
ngx_http_lua_socket_tcp_upstream_handler_pt read_event_handler;
ngx_http_lua_socket_tcp_upstream_handler_pt write_event_handler;

Expand Down Expand Up @@ -84,15 +85,20 @@ struct ngx_http_lua_socket_tcp_upstream_s {
size_t request_len;
ngx_chain_t *request_bufs;

ngx_http_lua_co_ctx_t *co_ctx;
ngx_http_lua_co_ctx_t *read_co_ctx;
ngx_http_lua_co_ctx_t *write_co_ctx;

ngx_uint_t reused;

unsigned no_close:1;
unsigned waiting:1;
unsigned conn_waiting:1;
unsigned read_waiting:1;
unsigned write_waiting:1;
unsigned eof:1;
unsigned body_downstream:1;
unsigned raw_downstream:1;
unsigned read_closed:1;
unsigned write_closed:1;
};


Expand Down
22 changes: 11 additions & 11 deletions t/023-rewrite/tcp-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -518,7 +518,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -587,7 +587,7 @@ Connection: close\r
foo
err: nil
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -668,7 +668,7 @@ Connection: close\r
foo
err: nil
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -745,7 +745,7 @@ Con][nection: c][lose\r
\r
fo]failed to receive a line: closed [o
]
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -823,7 +823,7 @@ Con][nection: c][lose\r
\r
fo]failed to receive a line: closed [o
]
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -895,7 +895,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -964,7 +964,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -1073,7 +1073,7 @@ Con][nection: c][lose\r
\r
fo]failed to receive a line: closed [o
]
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -1590,7 +1590,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -1912,7 +1912,7 @@ received: Connection: close
received:
received: foobarbaz
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
--- SKIP
Expand Down
2 changes: 1 addition & 1 deletion t/023-rewrite/unix-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed
close: nil closed
close: 1 nil
30 changes: 15 additions & 15 deletions t/058-tcp-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -202,7 +202,7 @@ attempt to send data on a closed socket:
--- config
server_tokens off;
resolver $TEST_NGINX_RESOLVER;
resolver_timeout 1s;
resolver_timeout 3s;
location /t {
content_by_lua '
local sock = ngx.socket.tcp()
Expand Down Expand Up @@ -295,7 +295,7 @@ qr/connect\(\) failed \(\d+: Connection refused\)/
lua_socket_connect_timeout 100ms;
lua_socket_send_timeout 100ms;
lua_socket_read_timeout 100ms;
resolver_timeout 1s;
resolver_timeout 3s;
location /test {
content_by_lua '
local sock = ngx.socket.tcp()
Expand Down Expand Up @@ -363,7 +363,7 @@ connected: 1
--- config
server_tokens off;
resolver $TEST_NGINX_RESOLVER;
resolver_timeout 1s;
resolver_timeout 3s;
location /t {
content_by_lua '
local sock = ngx.socket.tcp()
Expand Down Expand Up @@ -502,7 +502,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -569,7 +569,7 @@ Connection: close\r
foo
err: nil
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -648,7 +648,7 @@ Connection: close\r
foo
err: nil
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -723,7 +723,7 @@ Con][nection: c][lose\r
\r
fo]failed to receive a line: closed [o
]
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -799,7 +799,7 @@ Con][nection: c][lose\r
\r
fo]failed to receive a line: closed [o
]
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -869,7 +869,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -936,7 +936,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -1048,7 +1048,7 @@ Con][nection: c][lose\r
\r
fo]failed to receive a line: closed [o
]
close: nil closed
close: 1 nil
"
--- no_error_log
[error]
Expand Down Expand Up @@ -1557,7 +1557,7 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed []
close: nil closed
close: 1 nil
--- no_error_log
[error]
Expand Down Expand Up @@ -2077,7 +2077,7 @@ F(ngx_http_lua_socket_resolve_handler) {
println("lua socket resolve handler")
}
F(ngx_http_lua_socket_tcp_connect_retval_handler) {
F(ngx_http_lua_socket_tcp_conn_retval_handler) {
println("lua socket tcp connect retval handler")
}
Expand Down Expand Up @@ -2661,7 +2661,7 @@ GET /t
--- response_body
connected: 1
request sent: 57
close: nil closed
close: 1 nil
--- no_error_log
[error]
--- error_log
Expand Down
2 changes: 1 addition & 1 deletion t/059-unix-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ received: Connection: close
received:
received: foo
failed to receive a line: closed
close: nil closed
close: 1 nil
75 changes: 72 additions & 3 deletions t/065-tcp-socket-timeout.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ our $StapScript = $t::StapThread::StapScript;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 4 + 13);
plan tests => repeat_each() * (blocks() * 4 + 12);

our $HtmlDir = html_dir;

Expand All @@ -48,7 +48,7 @@ __DATA__
server_tokens off;
lua_socket_connect_timeout 100ms;
resolver $TEST_NGINX_RESOLVER;
resolver_timeout 1s;
resolver_timeout 3s;
location /t {
content_by_lua '
local sock = ngx.socket.tcp()
Expand Down Expand Up @@ -76,6 +76,7 @@ lua tcp socket connect timed out
server_tokens off;
lua_socket_connect_timeout 60s;
resolver $TEST_NGINX_RESOLVER;
resolver_timeout 3s;
location /t {
content_by_lua '
local sock = ngx.socket.tcp()
Expand Down Expand Up @@ -685,7 +686,7 @@ after
server_tokens off;
lua_socket_connect_timeout 100ms;
resolver $TEST_NGINX_RESOLVER;
resolver_timeout 1s;
resolver_timeout 3s;
location /t {
content_by_lua '
local sock = ngx.socket.tcp()
Expand Down Expand Up @@ -918,3 +919,71 @@ lua tcp socket read timeout: 10
lua tcp socket connect timeout: 60000
lua tcp socket read timed out



=== TEST 22: concurrent operations while writing
--- config
server_tokens off;
lua_socket_log_errors off;
location /t {
content_by_lua '
local sock = ngx.socket.tcp()
local function f()
ngx.sleep(0.01)
local bytes, err = sock:send("flush_all")
ngx.say("send: ", bytes, " ", err)
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
local ok, err = sock:getreusedtimes()
ngx.say("getreusedtimes: ", ok, " ", err)
local ok, err = sock:setkeepalive()
ngx.say("setkeepalive: ", ok, " ", err)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
sock:settimeout(1)
local res, err = sock:receive(1)
ngx.say("receive: ", res, " ", err)
end
local ok, err = ngx.thread.spawn(f)
if not ok then
ngx.say("failed to spawn writer thread: ", err)
return
end
sock:settimeout(300)
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT)
ngx.say("connect: ", ok, " ", err)
local bytes, err = sock:send("get helloworld!")
if not bytes then
ngx.say("send failed: ", err)
end
local ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
';
}

--- request
GET /t
--- response_body
connect: 1 nil
send: nil socket busy writing
close: nil socket busy writing
getreusedtimes: 0 nil
setkeepalive: nil socket busy writing
connect: nil socket busy writing
receive: nil timeout
send failed: timeout
close: 1 nil

--- no_error_log
[error]

Loading

0 comments on commit 934e33e

Please sign in to comment.