Skip to content

feature: implemented bsd style cosocket receive method. #1310

Closed
spacewander wants to merge 5 commits intoopenresty:masterfrom
spacewander:feature/bsd_read
Closed

feature: implemented bsd style cosocket receive method. #1310
spacewander wants to merge 5 commits intoopenresty:masterfrom
spacewander:feature/bsd_read

Conversation

@spacewander
Copy link
Copy Markdown
Member

This feature is atop #861, with some minor issues fixed.

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.

Comment thread README.markdown Outdated
If a non-number-like string argument is specified, then it is interpreted as a "pattern". The following patterns are supported:

* `'*a'`: reads from the socket until the connection is closed. No end-of-line translation is performed;
* `'*b'`: the BSD style receive, return anything once socket receives;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better be consistent: "return" => "returns".

Also, this is better:

"returns anything the socket has already received (won't return if it has not yet received any data)."


if (bytes == 0) {
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_CLOSED;
return NGX_ERROR;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. Maybe it's better to return the empty Lua string when the reading part of the connection has been closed by the remote? This should not be considered a fatal error. And we need a test case to cover this code path.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agentzh
If we do so, we could not detect if the remote is closed as the same way with sock:receive('*l').

This case is already covered by this line: https://github.com/spacewander/lua-nginx-module/blob/77c34e1f3aec37acb84bf67661ae9cc22b21c2eb/t/058-tcp-socket.t#L3849

Comment thread t/058-tcp-socket.t Outdated
'Host: localhost\r\n',
'Connection: close\r\n\r\n',
}
sock:send(req)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to check the return values of send() and handle any potential errors? Please fix other similar places (if any).

Comment thread t/058-tcp-socket.t



=== TEST 64: *b pattern for receive
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you also duplicate this test case in a separate .t test file which requires` the use of mockeagain reading test mode for the extreme case of receiving one byte at a time:

t/058-tcp-socket.t .. 115/416
#   Failed test 'TEST 64: *b pattern for receive - response_body - response is expected (repeated req 0, req 0)'
#   at /home/agentzh/git/lua-nginx-module/../test-nginx/lib/Test/Nginx/Socket.pm line 1555.
# @@ -1,3 +1,14 @@
#  1
# -22
# +2
# -hello world
# +2
# +h
# +e
# +l
# +l
# +o
# +
# +w
# +o
# +r
# +l
# +d

#   Failed test 'TEST 64: *b pattern for receive - response_body - response is expected (repeated req 1, req 0)'
#   at /home/agentzh/git/lua-nginx-module/../test-nginx/lib/Test/Nginx/Socket.pm line 1555.
# @@ -1,3 +1,14 @@
#  1
# -22
# +2
# -hello world
# +2
# +h
# +e
# +l
# +l
# +o
# +
# +w
# +o
# +r
# +l
# +d
t/058-tcp-socket.t .. 379/416 # Looks like you failed 2 tests of 416.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and maybe mockeagain writing mode too :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agentzh
Additional test added.

@spacewander
Copy link
Copy Markdown
Member Author

Will change the method name and submit in another pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants