Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Merge 0546ecc into 8820362
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalextremist committed Nov 13, 2013
2 parents 8820362 + 0546ecc commit f9fbc4b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
48 changes: 48 additions & 0 deletions lib/reel/mixins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def remote_host
# NOTE: Celluloid::IO does not yet support non-blocking reverse DNS
socket.peeraddr(true)[2]
end

end

module RequestMixin
Expand Down Expand Up @@ -60,4 +61,51 @@ def fragment
end

end

module SocketMixin

# Optimizations possible, depending on OS:

# TCP_NODELAY: prevent TCP packets from being buffered
# TCP_CORK: TODO: tersely describe
# SO_REUSEADDR: TODO: tersely describe

if RUBY_PLATFORM =~ /linux/

# Only Linux supports the mix of socket behaviors given in these optimizations.
# Beaware, certain optimizations may work individually off Linux; not together.
def optimize_socket socket
if socket.is_a? TCPSocket
socket.setsockopt( Socket::IPPROTO_TCP, :TCP_NODELAY, 1 )
socket.setsockopt( Socket::IPPROTO_TCP, 3, 1 ) # TCP_CORK
socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
end
end

def deoptimize_socket socket
if socket.is_a? TCPSocket
socket.setsockopt( Socket::IPPROTO_TCP, :TCP_NODELAY, 1 )
socket.setsockopt( Socket::IPPROTO_TCP, 3, 1 ) # TCP_CORK
socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
end
end

else

# If the underying OS is not Linux, apply the remaining available optimizations.
def optimize_socket socket
if socket.is_a? TCPSocket
socket.setsockopt( Socket::IPPROTO_TCP, :TCP_NODELAY, 1 )
end
end

def deoptimize_socket socket
if socket.is_a? TCPSocket
socket.setsockopt( Socket::IPPROTO_TCP, :TCP_NODELAY, 0 )
end
end
end

end

end
14 changes: 8 additions & 6 deletions lib/reel/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Server
# TCP sockets.
#
class << self
include SocketMixin
alias_method :_new, :new
protected :_new
end
Expand All @@ -40,8 +41,7 @@ def self.new(host, port, options = {} , &callback)
server = Celluloid::IO::TCPServer.new(host, port)
backlog = options.fetch(:backlog, DEFAULT_BACKLOG)

# prevent TCP packets from being buffered
server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
self.optimize_socket server
server.listen(backlog)

self._new(server, options, &callback)
Expand All @@ -64,11 +64,13 @@ def initialize(server, options = {}, &callback)
@callback = callback

async.run
end


end

def shutdown
@server.close if @server
if @server
self.deoptimize_socket @server
@server.close
end
end

def run
Expand Down

0 comments on commit f9fbc4b

Please sign in to comment.