From 00cea59c103c2f3684df92afe6a93f0d15c8e29e Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Sun, 8 Apr 2012 10:48:25 -0400 Subject: [PATCH] minor: refactor TCP_NODELAY --- lib/mongo/connection.rb | 4 +--- lib/mongo/util/node.rb | 6 +----- lib/mongo/util/pool.rb | 1 - lib/mongo/util/ssl_socket.rb | 4 +++- lib/mongo/util/tcp_socket.rb | 2 ++ 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index fbc2451..cf6743d 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -622,9 +622,7 @@ module Mongo socket = nil config = nil - socket = @socket_class.new(host, port, @op_timeout, @connect_timeout) - socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) - + socket = @socket_class.new(host, port, @op_timeout, @connect_timeout) config = self['admin'].command({:ismaster => 1}, :socket => socket) rescue OperationFailure, SocketError, SystemCallError, IOError close diff --git a/lib/mongo/util/node.rb b/lib/mongo/util/node.rb index 5fa691c..55e528c 100644 --- a/lib/mongo/util/node.rb +++ b/lib/mongo/util/node.rb @@ -40,11 +40,7 @@ module Mongo @connection.op_timeout, @connection.connect_timeout ) - if socket.nil? - return nil - else - socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) - end + return nil if socket.nil? rescue OperationTimeout, ConnectionFailure, OperationFailure, SocketError, SystemCallError, IOError => ex @connection.log(:debug, "Failed connection to #{host_string} with #{ex.class}, #{ex.message}.") socket.close if socket diff --git a/lib/mongo/util/pool.rb b/lib/mongo/util/pool.rb index 7d5c704..b1a207b 100644 --- a/lib/mongo/util/pool.rb +++ b/lib/mongo/util/pool.rb @@ -157,7 +157,6 @@ module Mongo def checkout_new_socket begin socket = @connection.socket_class.new(@host, @port, @connection.op_timeout) - socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) socket.pool = self rescue => ex socket.close if socket diff --git a/lib/mongo/util/ssl_socket.rb b/lib/mongo/util/ssl_socket.rb index 20d366b..c3beab6 100644 --- a/lib/mongo/util/ssl_socket.rb +++ b/lib/mongo/util/ssl_socket.rb @@ -15,7 +15,9 @@ module Mongo @op_timeout = op_timeout @connect_timeout = connect_timeout - @socket = ::TCPSocket.new(host, port) + @socket = ::TCPSocket.new(host, port) + @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) + @ssl = OpenSSL::SSL::SSLSocket.new(@socket) @ssl.sync_close = true diff --git a/lib/mongo/util/tcp_socket.rb b/lib/mongo/util/tcp_socket.rb index a5cd7af..9c3e4d0 100644 --- a/lib/mongo/util/tcp_socket.rb +++ b/lib/mongo/util/tcp_socket.rb @@ -16,8 +16,10 @@ module Mongo # TODO: Prefer ipv6 if server is ipv6 enabled @host = Socket.getaddrinfo(host, nil, Socket::AF_INET).first[3] @port = port + @socket_address = Socket.pack_sockaddr_in(@port, @host) @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) connect end