Skip to content

feature(socket.tcp): enhance the logic of parameter verification in #1735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2020

Conversation

rainingmaster
Copy link
Member

In ngx.socket.tcp:connect, if host is a TCP socket, the third parameter could be nil, it is to some third part module to write their code like this:

function _M.connect(host, port, opt)
    sock = ngx.socket.tcp()
    sock:connect(host, port, opt)
end

But if the host is a unix-domain socket, the second parameter could not be a nil, we have to write the code like this:

function _M.connect(path, opt)
    sock = ngx.socket.tcp()
    if opt ~= nil then
        sock:connect("unix:" .. path, opt)
    else
       sock:connect("unix:" .. path)
   end
end

Just like here: https://github.com/openresty/lua-resty-redis/pull/200/files#diff-adb515948c05651051c83fd6fb3ce7ceR143-R149. I think it is a comment use way, so update the second parameter and allow it be nil.

@agentzh
Copy link
Member

agentzh commented Jun 25, 2020

@rainingmaster Please use feature: prefix in the commit log titles. Thank you!

@rainingmaster rainingmaster force-pushed the allow_port_nil branch 3 times, most recently from aaffcf0 to 0460b95 Compare June 28, 2020 09:07
@rainingmaster rainingmaster changed the title feat(socket.tcp): allow second parameter is nil when host is unix-domain feature(socket.tcp): allow second parameter is nil when host is unix-domain Jun 28, 2020
@rainingmaster rainingmaster force-pushed the allow_port_nil branch 3 times, most recently from b615f86 to 2d734cb Compare June 28, 2020 12:57
@rainingmaster
Copy link
Member Author

Hi @agentzh, I have update the code, make the port optional and accept nil, could you help me review again?

@mergify
Copy link

mergify bot commented Jul 20, 2020

This pull request is now in conflict :(

content_by_lua_block {
local sock = ngx.socket.tcp()
sock:settimeout(500)
local ok, err = sock:connect("127.0.0.1")
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this behavior. This may lead to hard-to-debug mistakes. When the host name part is an INET address, the port cannot be optional.

Copy link
Member Author

@rainingmaster rainingmaster Jul 21, 2020

Choose a reason for hiding this comment

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

I see, I agree with this point. So we should allow like this:

sock:connect("unix:/path") -- work
sock:connect("unix:/path", nil) -- work
sock:connect("unix:/path", opt) -- work

sock:connect("127.0.0.1", 80) -- work
sock:connect("127.0.0.1", 80, nil) -- work
sock:connect("127.0.0.1", 80, opt) -- work

But like this will be raise an error:

sock:connect("127.0.0.1", nil) -- error

In this version, the behaviour should be like above: #1735 (comment), but we have to judge the domain is an unix domain or a ip.

Copy link
Member

Choose a reason for hiding this comment

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

@rainingmaster My original comment you quoted was for the unix domain socket case, not for the INET case.

Copy link
Member Author

Choose a reason for hiding this comment

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

@agentzh hihi, my quoted should be mean not unix domain socket? so it should mean INET case.

@rainingmaster
Copy link
Member Author

@agentzh @doujiang24 @spacewander Hi, I have raise a new version for this concern, now it will require that the second parameter must be passed in if the host is not unix domain.

Another point to note is that the unix domain has always supported usage like this:

connect("unix/path", nil, opts)

IMO, I think this usage should be retained, because users can easily use the format of connect(host, port, opts) to pass parameters when implementing their own lib, without the need for more if.

@rainingmaster rainingmaster changed the title feature(socket.tcp): allow second parameter is nil when host is unix-domain feature(socket.tcp): enhance the logic of parameter verification in Jul 31, 2020
@rainingmaster rainingmaster merged commit d6a29d2 into openresty:master Aug 16, 2020
@rainingmaster rainingmaster deleted the allow_port_nil branch October 23, 2020 04:51
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