-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: master
Are you sure you want to change the base?
Conversation
This pull request is now in conflict :( |
|
Here are two ways:
I think following others ' hands, which was used first, is better? |
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 theaddr
struct from SOCKET_BIND_INDEX cache and set intou->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;