2012-04-03 19:40:38 +00:00
|
|
|
require 'socket'
|
2012-05-31 20:27:33 +00:00
|
|
|
require 'timeout'
|
2012-04-03 19:40:38 +00:00
|
|
|
|
2012-03-03 00:09:02 +00:00
|
|
|
module Mongo
|
2012-04-03 19:40:38 +00:00
|
|
|
# Wrapper class for Socket
|
|
|
|
#
|
|
|
|
# Emulates TCPSocket with operation and connection timeout
|
|
|
|
# sans Timeout::timeout
|
|
|
|
#
|
|
|
|
class TCPSocket
|
|
|
|
attr_accessor :pool
|
|
|
|
|
|
|
|
def initialize(host, port, op_timeout=nil, connect_timeout=nil)
|
|
|
|
@op_timeout = op_timeout
|
|
|
|
@connect_timeout = connect_timeout
|
|
|
|
|
|
|
|
# TODO: Prefer ipv6 if server is ipv6 enabled
|
|
|
|
@host = Socket.getaddrinfo(host, nil, Socket::AF_INET).first[3]
|
|
|
|
@port = port
|
2012-04-08 14:48:25 +00:00
|
|
|
|
2012-04-03 19:40:38 +00:00
|
|
|
@socket_address = Socket.pack_sockaddr_in(@port, @host)
|
|
|
|
@socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
2012-04-08 14:48:25 +00:00
|
|
|
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
2012-04-03 19:40:38 +00:00
|
|
|
|
|
|
|
connect
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect
|
2012-05-31 20:27:33 +00:00
|
|
|
if @connect_timeout
|
|
|
|
Timeout::timeout(@connect_timeout, OperationTimeout) do
|
2012-04-03 19:40:38 +00:00
|
|
|
@socket.connect(@socket_address)
|
|
|
|
end
|
|
|
|
else
|
2012-05-31 20:27:33 +00:00
|
|
|
@socket.connect(@socket_address)
|
2012-04-03 19:40:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send(data)
|
|
|
|
@socket.write(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(maxlen, buffer)
|
|
|
|
# Block on data to read for @op_timeout seconds
|
2012-04-05 16:47:17 +00:00
|
|
|
begin
|
|
|
|
ready = IO.select([@socket], nil, [@socket], @op_timeout)
|
|
|
|
rescue IOError
|
|
|
|
raise OperationFailure
|
|
|
|
end
|
|
|
|
if ready
|
2012-04-04 17:46:47 +00:00
|
|
|
begin
|
2012-05-15 17:54:44 +00:00
|
|
|
@socket.sysread(maxlen, buffer)
|
2012-05-15 19:41:57 +00:00
|
|
|
rescue SystemCallError => ex
|
|
|
|
# Needed because sometimes JRUBY doesn't throw Errno::ECONNRESET as it should
|
|
|
|
# http://jira.codehaus.org/browse/JRUBY-6180
|
|
|
|
raise ConnectionFailure, ex
|
|
|
|
rescue Errno::ENOTCONN, Errno::EBADF, Errno::ECONNRESET, Errno::EPIPE, Errno::ETIMEDOUT, EOFError => ex
|
|
|
|
raise ConnectionFailure, ex
|
|
|
|
rescue Errno::EINTR, Errno::EIO, IOError => ex
|
|
|
|
raise OperationFailure, ex
|
2012-04-04 17:46:47 +00:00
|
|
|
end
|
2012-04-03 19:40:38 +00:00
|
|
|
else
|
|
|
|
raise OperationTimeout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setsockopt(key, value, n)
|
|
|
|
@socket.setsockopt(key, value, n)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@socket.close
|
|
|
|
end
|
2012-03-03 00:09:02 +00:00
|
|
|
|
2012-04-03 19:40:38 +00:00
|
|
|
def closed?
|
|
|
|
@socket.closed?
|
|
|
|
end
|
|
|
|
end
|
2012-03-03 00:09:02 +00:00
|
|
|
end
|