Skip to content
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

option to not delay TCPServer socket creation #24115

Merged
merged 2 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,15 @@ mutable struct TCPServer <: LibuvServer
return tcp
end
end
function TCPServer()

# Keyword arg "delay": if true, libuv delays creation of socket fd till bind.
# It can be set to false if there is a need to set socket options before
# further calls to `bind` and `listen`, e.g. `SO_REUSEPORT`.
function TCPServer(; delay=true)
tcp = TCPServer(Libc.malloc(_sizeof_uv_tcp), StatusUninit)
err = ccall(:uv_tcp_init, Cint, (Ptr{Void}, Ptr{Void}),
eventloop(), tcp.handle)
af_spec = delay ? 0 : 2 # AF_UNSPEC is 0, AF_INET is 2
err = ccall(:uv_tcp_init_ex, Cint, (Ptr{Void}, Ptr{Void}, Cuint),
eventloop(), tcp.handle, af_spec)
uv_error("failed to create tcp server", err)
tcp.status = StatusInit
return tcp
Expand Down
7 changes: 7 additions & 0 deletions test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,10 @@ end
@test test_connect(addr)
end
end

@testset "TCPServer constructor" begin
s = Base.TCPServer(; delay=false)
if ccall(:jl_has_so_reuseport, Int32, ()) == 1
@test 0 == ccall(:jl_tcp_reuseport, Int32, (Ptr{Void},), s.handle)
end
end
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Maybe test the positive case as well? (Unless that's already done somewhere else)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that is tested elsewhere as that's the default.