Skip to content

Commit

Permalink
Add back in raw socket timeout as a backup to the non-blocking IO.select
Browse files Browse the repository at this point in the history
timeout.
  • Loading branch information
mperham committed Oct 29, 2009
1 parent 7df4e8c commit 74b2599
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/memcache.rb
Expand Up @@ -1061,6 +1061,21 @@ def connect_to(host, port, timeout=nil)

io = MemCache::BufferedIO.new(sock)
io.read_timeout = timeout
# Getting reports from several customers, including 37signals,
# that the non-blocking timeouts in 1.7.5 don't seem to be reliable.
# It can't hurt to set the underlying socket timeout also, if possible.
if timeout
secs = Integer(timeout)
usecs = Integer((timeout - secs) * 1_000_000)
optval = [secs, usecs].pack("l_2")
begin
io.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval

This comment has been minimized.

Copy link
@defunkt

defunkt Oct 30, 2009

Is this supported in OS X? I can't get the timeout to fire in my local testing.

io.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
rescue Exception => ex
# Solaris, for one, does not like/support socket timeouts.
@logger.info "[memcache-client] Unable to use raw socket timeouts: #{ex.class.name}: #{ex.message}" if @logger
end
end
io
end

Expand Down

1 comment on commit 74b2599

@mperham
Copy link
Owner Author

Choose a reason for hiding this comment

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

It works for normal blocking IO. http://gist.github.com/222735

Please sign in to comment.