Skip to content

feature: support tcp binding ip:port or ip of ipv4 or ipv6 #2412

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

chensunny
Copy link

Brackgroud

ref: #2381

Key Changes

Original PR uses sock:bind(IP:Port) API will cause a Breaking Change.
New design uses sock:bind(IP, Port) API.

To implement the function, it needs to pass 2 params in Lua VM;
Parse the params to make addr struct and inject into SOCKET_BIND_INDEX cache;
When users use sock:connect API , it will get the addr struct from SOCKET_BIND_INDEX cache and set into u->peer.local ;

Last will use the peer.local the make peer connection is other nginx module;

Test Coverage

This PR adds a new test case (TEST 7) to verify the behavior of upstream sockets when binding to a specific IP and port. The test ensures that the socket binding either succeeds with the expected IP and port (bind: 127.0.0.1:12345) or fails with the error message failed to connect: address already in use.

New Test Case: TEST 7 is added to validate socket binding with a specific IP and port.
Response Validation: The test checks for two possible outcomes in the response body:
Successful binding: bind: 127.0.0.1:12345
Failure due to address conflict: failed to connect: address already in use
Error Log Check: Ensures that the error log contains the expected message when binding fails.

The test case will run 4 times, Expect:
The first time will be successful,
Other times will fail to bind because the TCP connection is still in TIME-WAIT, which the port is still be used.

Why design the case above?
I was trying to use https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#tcpsocksetoption
to set SO_REUSEADDR to the bind, but the setting is for the connection that will be ESTABLISHED.
But to reuse the TIME-WAIT port, it needs to be set before binding, which can not inject nginx code before bind now;

Copy link

mergify bot commented Apr 25, 2025

This pull request is now in conflict :(

@mergify mergify bot added the conflict label Apr 25, 2025
@mergify mergify bot removed the conflict label Apr 25, 2025
@zhuizhuhaomeng
Copy link
Contributor

Brackgroud

ref: #2381

Key Changes

Original PR uses sock:bind(IP:Port) API will cause a Breaking Change. New design uses sock:bind(IP, Port) API.

To implement the function, it needs to pass 2 params in Lua VM; Parse the params to make addr struct and inject into SOCKET_BIND_INDEX cache; When users use sock:connect API , it will get the addr struct from SOCKET_BIND_INDEX cache and set into u->peer.local ;

Last will use the peer.local the make peer connection is other nginx module;

Test Coverage

This PR adds a new test case (TEST 7) to verify the behavior of upstream sockets when binding to a specific IP and port. The test ensures that the socket binding either succeeds with the expected IP and port (bind: 127.0.0.1:12345) or fails with the error message failed to connect: address already in use.

New Test Case: TEST 7 is added to validate socket binding with a specific IP and port. Response Validation: The test checks for two possible outcomes in the response body: Successful binding: bind: 127.0.0.1:12345 Failure due to address conflict: failed to connect: address already in use Error Log Check: Ensures that the error log contains the expected message when binding fails.

The test case will run 4 times, Expect: The first time will be successful, Other times will fail to bind because the TCP connection is still in TIME-WAIT, which the port is still be used.

Why design the case above? I was trying to use https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#tcpsocksetoption to set SO_REUSEADDR to the bind, but the setting is for the connection that will be ESTABLISHED. But to reuse the TIME-WAIT port, it needs to be set before binding, which can not inject nginx code before bind now;

sock:bind(IP:Port) does not case a breaking change. The original sock:bind(IP) is also compatible.
But sock:bind(IP, Port) is more efficient indeed.

@chensunny
Copy link
Author

Here are two ways:

  • Uniformly process the param first, and then handle the logic
  • Handle the logic while processing parameters

I think following others ' hands, which was used first, is better?

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.

3 participants