From mboxrd@z Thu Jan 1 00:00:00 1970 Delivered-To: chneukirchen@gmail.com Received: by 10.52.76.196 with SMTP id m4csp79968vdw; Wed, 2 Oct 2013 16:55:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=dkim-signature:x-gm-message-state:mime-version:subject:from :in-reply-to:date:cc:message-id:references:to:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:list-post:list-help:list-archive:sender:list-subscribe :list-unsubscribe:content-type; bh=clNwi5ZwbDVu4nl6WYRjLdXBKXQW4ZzKmY6lSjm+AFY=; b=OrqMX2loKZX3Hdl7Sfq3KTXxp5lEYQFgTF8UxU0obaTsUYaALz9Ox9YlzU2t5LaMxq M5sBXnUbIplCbjfoGj+q2IO7nTMbmvuJWgSrwZB8tSOHpb5wjOd6FVzW35vN/nJv+0hS GnVTr1slUN2CGqZttyAKkkIEqXUslRN4DUPyxA4m6A6r449um1t1DCp5Az6RTq2tlvRE U1eLHA0SVfvrh4Hoyl3i5KWueOoJHeIVD2tht3V7Oj5FlO0Ry3J+KGRJmy+o4jNBArKJ namED9oGDZ7yEuNH9sFURuLviJoYR5BcW4k/+PT1rpzFouE5iAM/k7NBUIjq/iuEYve+ ZK2Q== Return-Path: Received-SPF: pass (google.com: domain of rack-devel+bncBDNLPLOTRIEBB2XEWKJAKGQEQF7UU5I@googlegroups.com designates 10.49.1.112 as permitted sender) client-ip=10.49.1.112 Authentication-Results: mr.google.com; spf=pass (google.com: domain of rack-devel+bncBDNLPLOTRIEBB2XEWKJAKGQEQF7UU5I@googlegroups.com designates 10.49.1.112 as permitted sender) smtp.mail=rack-devel+bncBDNLPLOTRIEBB2XEWKJAKGQEQF7UU5I@googlegroups.com; dkim=pass header.i=@googlegroups.com X-Received: from mr.google.com ([10.49.1.112]) by 10.49.1.112 with SMTP id 16mr1516398qel.7.1380758123500 (num_hops = 1); Wed, 02 Oct 2013 16:55:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=mime-version:subject:from:in-reply-to:date:cc:message-id:references :to:x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:list-post:list-help:list-archive :sender:list-subscribe:list-unsubscribe:content-type; bh=clNwi5ZwbDVu4nl6WYRjLdXBKXQW4ZzKmY6lSjm+AFY=; b=vzZ8Dl0wKVleUtJ7ACwAWq+3njw+BEi0wQ3ZUn59h/SPni3/PnA5ZAwYQ+k7Y438kO IgntS/mlLiC70zZB0SfdoDJ7MX/pkOqgv8kaW3gTUshDxN0FL68tRMyjnpbtHTTqqO7e Utg1CrG2W7ys0XR6cCeFHuVm7Q9xuSELa+13hbd5TPxjx32wznnIL3FItIqqmSnxQdHy NgLEtKBwsLijCn+nLY62jwTINvqgWWZT4B04GgqZgEJcUGoE6WmBL75C4FRN72zTOZPK G7fLjyzzqPRUIyDPgjMciTMBY7B7g+iO4V6OJmbkgTkz5MqpqdFQksuRjIvkryRRO737 QG7g== X-Received: by 10.49.1.112 with SMTP id 16mr177000qel.7.1380758123368; Wed, 02 Oct 2013 16:55:23 -0700 (PDT) X-BeenThere: rack-devel@googlegroups.com Received: by 10.49.117.69 with SMTP id kc5ls760554qeb.42.gmail; Wed, 02 Oct 2013 16:55:22 -0700 (PDT) X-Received: by 10.236.135.79 with SMTP id t55mr4877217yhi.39.1380758122266; Wed, 02 Oct 2013 16:55:22 -0700 (PDT) Received: from mail-qe0-f44.google.com (mail-qe0-f44.google.com [209.85.128.44]) by gmr-mx.google.com with ESMTPS id t6si911436qcj.3.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 02 Oct 2013 16:55:21 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.44 is neither permitted nor denied by best guess record for domain of k.haase@finn.de) client-ip=209.85.128.44; Received: by mail-qe0-f44.google.com with SMTP id 3so1189110qeb.17 for ; Wed, 02 Oct 2013 16:55:21 -0700 (PDT) X-Gm-Message-State: ALoCoQkjFb1TX9L4gRvhrh/AOH2x5UV9C4COlnSG2ACLVqcxSf42ZGo4UxLjc0KxDCu/B6HCRfCs X-Received: by 10.49.95.68 with SMTP id di4mr6285900qeb.50.1380758121729; Wed, 02 Oct 2013 16:55:21 -0700 (PDT) Received: from konstantins-macbook-air-2.fritz.box (p5B3D41F1.dip0.t-ipconnect.de. [91.61.65.241]) by mx.google.com with ESMTPSA id e7sm10790929qag.7.1969.12.31.16.00.00 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 02 Oct 2013 16:55:20 -0700 (PDT) Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) Subject: Re: [RFC/PATCH] Add Rack::Builder#warmup method for app preloading. From: Konstantin Haase In-Reply-To: <1380755407-40487-1-git-send-email-ruby@tmm1.net> Date: Thu, 3 Oct 2013 01:55:18 +0200 Cc: Aman Gupta Message-Id: <5F09D3FD-B53A-4166-9F63-438F075E4A7C@finn.de> References: <1380755407-40487-1-git-send-email-ruby@tmm1.net> To: rack-devel@googlegroups.com X-Mailer: Apple Mail (2.1508) X-Original-Sender: k.haase@finn.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 209.85.128.44 is neither permitted nor denied by best guess record for domain of k.haase@finn.de) smtp.mail=k.haase@finn.de Reply-To: rack-devel@googlegroups.com Precedence: list Mailing-list: list rack-devel@googlegroups.com; contact rack-devel+owners@googlegroups.com List-ID: X-Google-Group-Id: 486215384060 List-Post: , List-Help: , List-Archive: Sender: rack-devel@googlegroups.com List-Subscribe: , List-Unsubscribe: , Content-Type: text/plain; charset=ISO-8859-1 "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 > > 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.