You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with the CouchRest Gem, and a massively multi-threaded, multi-process system where we are creating and closing sockets a lot. This gets us into trouble when the code hits the following lines in RubyTCPSocket.java:
try {
// This is a bit convoluted because (1) SocketChannel.bind is only in jdk 7 and// (2) Socket.getChannel() seems to return null in some caseschannel = SocketChannel.open();
Socketsocket = channel.socket();
if (localHost != null) {
socket.bind( newInetSocketAddress(InetAddress.getByName(localHost), localPort) );
}
The problem is that the time from the closing of a Socket to the opening, and therefore calling of bind() on the same host/port combination is insufficient to have the OS release the socket.
Typically, you want to set SO_REUSEADDR to true and then you can very quickly reuse that same host/port address.
Proposed Solution
My ideal solution would be to have setReuseAddress()always called with true because the number of times you don't want this are few and far between. Another idea would be able to make a system default like: -Djruby.socket.reuse=true or something where we can set it to be tru for all sockets we create in that process.
try {
// This is a bit convoluted because (1) SocketChannel.bind is only in jdk 7 and// (2) Socket.getChannel() seems to return null in some caseschannel = SocketChannel.open();
Socketsocket = channel.socket();
socket.setReuseAddress(true);
if (localHost != null) {
socket.bind( newInetSocketAddress(InetAddress.getByName(localHost), localPort) );
}
The less than ideal, but very workable solution is to have options that allow this to be set before the bind() call. This is bad in that it means I have to make sure these options are exposed in every Ruby Gem that uses sockets, and that means a lot of changes for something that, for me, is really a global preference.
The text was updated successfully, but these errors were encountered:
Ok, after three years I actually looked into this.
MRI does set SO_REUSEADDR by default, but only on INET_SERVER sockets (TCPServer). I'll make that change, but for other sockets where you want to reuse you'll need to set it (as in MRI).
Set-Up
I'm working with the CouchRest Gem, and a massively multi-threaded, multi-process system where we are creating and closing sockets a lot. This gets us into trouble when the code hits the following lines in
RubyTCPSocket.java
:The problem is that the time from the closing of a Socket to the opening, and therefore calling of
bind()
on the same host/port combination is insufficient to have the OS release the socket.Typically, you want to set
SO_REUSEADDR
totrue
and then you can very quickly reuse that same host/port address.Proposed Solution
My ideal solution would be to have
setReuseAddress()
always called withtrue
because the number of times you don't want this are few and far between. Another idea would be able to make a system default like:-Djruby.socket.reuse=true
or something where we can set it to be tru for all sockets we create in that process.The less than ideal, but very workable solution is to have options that allow this to be set before the
bind()
call. This is bad in that it means I have to make sure these options are exposed in every Ruby Gem that uses sockets, and that means a lot of changes for something that, for me, is really a global preference.The text was updated successfully, but these errors were encountered: