@@ -489,57 +489,52 @@ end
489
489
490
490
const client_port = Ref {Cushort} (0 )
491
491
492
- function socket_reuse_port ()
492
+ function socket_reuse_port (iptype )
493
493
if ccall (:jl_has_so_reuseport , Int32, ()) == 1
494
- s = TCPSocket (delay = false )
494
+ sock = TCPSocket (delay = false )
495
495
496
496
# Some systems (e.g. Linux) require the port to be bound before setting REUSEPORT
497
497
bind_early = Sys. islinux ()
498
498
499
- bind_early && bind_client_port (s )
500
- rc = ccall (:jl_tcp_reuseport , Int32, (Ptr{Cvoid},), s . handle)
499
+ bind_early && bind_client_port (sock, iptype )
500
+ rc = ccall (:jl_tcp_reuseport , Int32, (Ptr{Cvoid},), sock . handle)
501
501
if rc < 0
502
502
# This is an issue only on systems with lots of client connections, hence delay the warning
503
503
nworkers () > 128 && @warn " Error trying to reuse client port number, falling back to regular socket" maxlog= 1
504
504
505
505
# provide a clean new socket
506
506
return TCPSocket ()
507
507
end
508
- bind_early || bind_client_port (s )
509
- return s
508
+ bind_early || bind_client_port (sock, iptype )
509
+ return sock
510
510
else
511
511
return TCPSocket ()
512
512
end
513
513
end
514
514
515
- # TODO : this doesn't belong here, it belongs in Sockets
516
- function bind_client_port (s:: TCPSocket )
517
- Sockets. iolock_begin ()
518
- @assert s. status == Sockets. StatusInit
519
- host_in = Ref (hton (UInt32 (0 ))) # IPv4 0.0.0.0
520
- err = ccall (:jl_tcp_bind , Int32, (Ptr{Cvoid}, UInt16, Ptr{Cvoid}, Cuint, Cint),
521
- s, hton (client_port[]), host_in, 0 , false )
522
- Sockets. iolock_end ()
523
- uv_error (" tcp_bind" , err)
524
-
525
- _addr, port = getsockname (s)
515
+ function bind_client_port (sock:: TCPSocket , iptype)
516
+ bind_host = iptype (0 )
517
+ Sockets. bind (sock, bind_host, client_port[])
518
+ _addr, port = getsockname (sock)
526
519
client_port[] = port
527
- return s
520
+ return sock
528
521
end
529
522
530
523
function connect_to_worker (host:: AbstractString , port:: Integer )
531
- s = socket_reuse_port ()
532
- connect (s, host, UInt16 (port))
533
-
534
524
# Avoid calling getaddrinfo if possible - involves a DNS lookup
535
525
# host may be a stringified ipv4 / ipv6 address or a dns name
536
526
bind_addr = nothing
537
527
try
538
- bind_addr = string ( parse (IPAddr,host) )
528
+ bind_addr = parse (IPAddr,host)
539
529
catch
540
- bind_addr = string ( getaddrinfo (host) )
530
+ bind_addr = getaddrinfo (host)
541
531
end
542
- (s, bind_addr)
532
+
533
+ iptype = typeof (bind_addr)
534
+ sock = socket_reuse_port (iptype)
535
+ connect (sock, bind_addr, UInt16 (port))
536
+
537
+ (sock, string (bind_addr))
543
538
end
544
539
545
540
0 commit comments