rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / code / Atom feed
From: Konstantin Haase <k.haase@finn.de>
To: rack-devel@googlegroups.com
Cc: Aman Gupta <aman@tmm1.net>
Subject: Re: [RFC/PATCH] Add Rack::Builder#warmup method for app preloading.
Date: Thu, 3 Oct 2013 01:55:18 +0200	[thread overview]
Message-ID: <5F09D3FD-B53A-4166-9F63-438F075E4A7C@finn.de> (raw)
In-Reply-To: <1380755407-40487-1-git-send-email-ruby@tmm1.net>

"Could you rebase and add some tests." /trololo

I generally like the idea. I think we might have to remove Builder#call, though, just to avoid concurrency issues and double firing warmup blocks.

Konstantin

On Oct 3, 2013, at 1:10 AM, ruby@tmm1.net wrote:

> From: Aman Gupta <aman@tmm1.net>
> 
> 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
> ---
> lib/rack/builder.rb  | 19 +++++++++++++++++--
> test/spec_builder.rb | 11 +++++++++++
> 2 files changed, 28 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/rack/builder.rb b/lib/rack/builder.rb
> index 66dc7bd..fa3a1ea 100644
> --- a/lib/rack/builder.rb
> +++ b/lib/rack/builder.rb
> @@ -51,7 +51,7 @@ module Rack
>     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 @@ module Rack
>       @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 @@ module Rack
>     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)
> diff --git a/test/spec_builder.rb b/test/spec_builder.rb
> index 0774f59..20ea668 100644
> --- a/test/spec_builder.rb
> +++ b/test/spec_builder.rb
> @@ -130,6 +130,17 @@ describe Rack::Builder do
>     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
> -- 
> 1.8.3.4
> 
> -- 
> 
> --- 
> You received this message because you are subscribed to the Google Groups "Rack Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rack-devel+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 

--- 
You received this message because you are subscribed to the Google Groups "Rack Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rack-devel+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

  reply	other threads:[~2013-10-02 23:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-02 23:10 ruby
2013-10-02 23:55 ` Konstantin Haase [this message]
2013-10-29  0:02   ` James Tucker
2013-10-29  0:21     ` Santiago Pastorino
2013-10-29 17:54     ` Eric Wong
2013-11-06  2:57       ` Eric Wong
2013-11-06  3:12         ` [PATCH] builder: avoid to_app on every request when using map Eric Wong
2014-07-15  4:09           ` James Tucker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://groups.google.com/group/rack-devel

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5F09D3FD-B53A-4166-9F63-438F075E4A7C@finn.de \
    --to=rack-devel@googlegroups.com \
    --cc=aman@tmm1.net \
    --subject='Re: [RFC/PATCH] Add Rack::Builder#warmup method for app preloading.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/rack-devel

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 rack-devel rack-devel/ https://public-inbox.org/rack-devel \
		rack-devel@googlegroups.com
	public-inbox-index rack-devel

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.rack.devel
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.lang.ruby.rack.devel
	nntp://news.gmane.io/gmane.comp.lang.ruby.rack.devel
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/rack.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git