Class: WEBrick::HighPerformanceServer

Inherits:
HTTPServer
  • Object
show all
Defined in:
lib/webrick/highperformanceserver.rb

Overview

A high performance WEBrick server that takes advantage of sendfile(2) and fork(2) to drastically increase the performance of serving static files.

Instead of starting a HighPerformanceServer like a regular HTTPServer, use HighPerformanceServer::create.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, default = WEBrick::Config::HighPerformance) ⇒ HighPerformanceServer

Only need to override default.



131
132
133
# File 'lib/webrick/highperformanceserver.rb', line 131

def initialize(config = {}, default = WEBrick::Config::HighPerformance)
  super config, default
end

Class Attribute Details

.listenersObject

Server sockets for my children.



77
78
79
# File 'lib/webrick/highperformanceserver.rb', line 77

def listeners
  @listeners
end

Class Method Details

.create(config = {}, default = WEBrick::Config::HighPerformance) ⇒ Object

Creates :Processes new HighPerformanceServers all listening on the same server sockets.

config and default work the same as they do for HTTPServer.

yields each forked child’s server object. The server will not be started nor will signals be caught.

Returns the pids of the created HighPerformanceServer children.

Typical usage:

WEBrick::HighPerformanceServer.create do |server|
  server.mount '/', WEBrick::HTTPServlet::FileHandler,
               '/usr/local/www/data'
  trap 'INT' do server.shutdown end
  trap 'TERM' do server.shutdown end
  server.start
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/webrick/highperformanceserver.rb', line 100

def create(config = {}, default = WEBrick::Config::HighPerformance)
  config = default.dup.update config
  bind_address = config[:BindAddress]
  port = config[:Port]
  processes = config[:Processes]
  config[:Logger] ||= WEBrick::Log.new

  @listeners = WEBrick::Utils.create_listeners bind_address, port
  if @listeners.first.respond_to? :set_accept_filter then
    @listeners.each do |server|
      server.set_accept_filter 'httpready'
    end
  end

  pids = []

  processes.times do
    pids << fork do
      server = self.new config, default
      yield server
    end
  end

  return pids
end

Instance Method Details

#access_log(*args) ⇒ Object

HighPerformanceServer doesn’t log. Oh you want access logs? Well tough!



138
139
# File 'lib/webrick/highperformanceserver.rb', line 138

def access_log(*args)
end

#listen(address, port) ⇒ Object

Our listeners have already been created, so suck them in from the class.



144
145
146
# File 'lib/webrick/highperformanceserver.rb', line 144

def listen(address, port)
  @listeners = self.class.listeners
end

#shutdownObject

Don’t close the listeners, just stop.



151
152
153
# File 'lib/webrick/highperformanceserver.rb', line 151

def shutdown
  stop
end