Skip to content

Commit

Permalink
Attempt to fix connection hanging issues. Retrofit of mongoid#331.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhyman committed Apr 20, 2015
1 parent aafec12 commit fcf449f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/moped/sockets/connectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Sockets
module Connectable

attr_reader :host, :port
attr_accessor :timeout

# Is the socket connection alive?
#
Expand Down Expand Up @@ -43,7 +44,13 @@ def self.included(klass)
# @since 1.2.0
def read(length)
check_if_alive!
handle_socket_errors { super }
handle_socket_errors {
if Kernel.select([self], nil, [self], @timeout)
super
else
raise Errors::ConnectionFailure, "timeout #{@timeout} exceeded on read from #{host}:#{port}"
end
}
end

# Write to the socket.
Expand Down Expand Up @@ -152,6 +159,7 @@ def connect(host, port, timeout)
sock = new(host, port)
sock.set_encoding('binary')
timeout_val = [ timeout, 0 ].pack("l_2")
sock.timeout = timeout
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, timeout_val)
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, timeout_val)
Expand Down

0 comments on commit fcf449f

Please sign in to comment.