feature: support tcp binding ip:port or ip of ipv4 or ipv6#2412
feature: support tcp binding ip:port or ip of ipv4 or ipv6#2412chensunny wants to merge 9 commits intoopenresty:masterfrom
Conversation
|
This pull request is now in conflict :( |
|
|
Here are two ways:
I think following others ' hands, which was used first, is better? |
Please change the DOC of lua-resty-core. |
change the return reminder Co-authored-by: lijunlong <[email protected]>
|
merged and close |
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
addrstruct and inject into SOCKET_BIND_INDEX cache;When users use
sock:connectAPI , it will get theaddrstruct 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;