Created
April 8, 2012 00:57
-
-
Save paneq/2333311 to your computer and use it in GitHub Desktop.
does Socket::SO_REUSEADDR work in ruby ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tcp 0 0 127.0.0.1:35648 127.0.0.1:9876 TIME_WAIT | |
tcp 0 0 127.0.0.1:9876 127.0.0.1:35646 TIME_WAIT | |
retrying ... | |
tcp 0 0 127.0.0.1:35648 127.0.0.1:9876 TIME_WAIT | |
tcp 0 0 127.0.0.1:9876 127.0.0.1:35646 TIME_WAIT | |
retrying ... | |
tcp 0 0 127.0.0.1:35648 127.0.0.1:9876 TIME_WAIT | |
tcp 0 0 127.0.0.1:9876 127.0.0.1:35646 TIME_WAIT | |
retrying ... | |
. | |
. | |
. | |
retrying ... | |
tcp 0 0 127.0.0.1:9876 127.0.0.1:35646 TIME_WAIT | |
retrying ... | |
tcp 0 0 127.0.0.1:9876 127.0.0.1:35646 TIME_WAIT | |
retrying ... | |
tcp 0 0 127.0.0.1:9876 127.0.0.1:35646 TIME_WAIT | |
. | |
. | |
. | |
retrying ... | |
retrying ... | |
retrying ... | |
. | |
. | |
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_tcp_listener(host, port, optimize_for_latency=true, backlog=1024) | |
s = TCPServer.new(host, port) | |
if optimize_for_latency | |
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) | |
end | |
#s.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) | |
#s.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) | |
s.setsockopt(:SOCKET, :REUSEADDR, 1) | |
s.listen backlog | |
@ios << s | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@server = Puma::Server.new(app) | |
loop do | |
begin | |
@server.add_tcp_listener('0.0.0.0', DEFAULT_PORT) | |
break | |
rescue Errno::EADDRINUSE => x | |
puts `netstat -n | grep #{DEFAULT_PORT}` if debug? | |
puts "retrying ..." if debug? | |
sleep(1) | |
retry | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment