Permalink
Browse files

Add Rack::Builder#warmup method for app preloading.

This new `warmup` method takes a block which is invoked after the app
is built. The block can be used to make mock requests that ensure
all application dependencies are loaded before the app starts
serving traffic.

With complex frameworks like Rails, many dependencies are auto-loaded
and data like mime-type and i18n is not loaded into memory by default. This
often means the first few requests handled by an application are quite
slow.

With this patch, config.ru can simply make requests via warmup to
exercise the app before it is used:

  $ tail -4 config.ru
  warmup do |app|
    client = Rack::MockRequest.new(app)
    client.get('/')
  end
  • Loading branch information...
tmm1 committed Sep 27, 2013
1 parent 65bf5e7 commit f791197f501776480a67211afbca0b32c628b2c9
Showing with 28 additions and 2 deletions.
  1. +17 −2 lib/rack/builder.rb
  2. +11 −0 test/spec_builder.rb
@@ -51,7 +51,7 @@ def self.new_from_string(builder_script, file="(rackup)")
end

def initialize(default_app = nil,&block)
@use, @map, @run = [], nil, default_app
@use, @map, @run, @warmup = [], nil, default_app, nil
instance_eval(&block) if block_given?
end

@@ -104,6 +104,19 @@ def run(app)
@run = app
end

# Takes a lambda or block that is used to warm-up the application.
#
# warmup do |app|
# client = Rack::MockRequest.new(app)
# client.get('/')
# end
#
# use SomeMiddleware
# run MyApp
def warmup(prc=nil, &block)
@warmup = prc || block
end

# Creates a route within the application.
#
# Rack::Builder.app do
@@ -131,7 +144,9 @@ def map(path, &block)
def to_app
app = @map ? generate_map(@run, @map) : @run
fail "missing run or map statement" unless app
@use.reverse.inject(app) { |a,e| e[a] }
app = @use.reverse.inject(app) { |a,e| e[a] }
@warmup.call(app) if @warmup
app
end

def call(env)
@@ -130,6 +130,17 @@ def builder_to_app(&block)
Rack::MockRequest.new(app).get("/foo").should.be.server_error
end

it "yields the generated app to a block for warmup" do
warmed_up_app = nil

app = Rack::Builder.new do
warmup { |a| warmed_up_app = a }
run lambda { |env| [200, {}, []] }
end.to_app

warmed_up_app.should.equal app
end

should "initialize apps once" do
app = builder do
class AppClass

0 comments on commit f791197

Please sign in to comment.