<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Badger&#039;s Personal Site &#187; Ruby on Rails</title>
	<atom:link href="http://jamesbadger.ca/category/development/rubyonrails/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesbadger.ca</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jan 2012 21:49:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rails 3: Rack-Throttle your Rack API</title>
		<link>http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/</link>
		<comments>http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 21:49:16 +0000</pubDate>
		<dc:creator>James Badger</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://jamesbadger.ca/?p=130</guid>
		<description><![CDATA[Sometimes, you will want to decouple your Rails app and place your API in its own special folder in lib. You could be using Sinatra, Grape, or even your own Rack app. The advantage is you can separate out your &#8230; <a href="http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you will want to decouple your Rails app and place your API in its own special folder in lib. You could be using <a href="http://www.sinatrarb.com/">Sinatra</a>, <a href="https://github.com/intridea/grape">Grape</a>, or even your own Rack app. The advantage is you can separate out your API code, making it just that much easier to port over to another app. This guide is not intended to show you how to extract an API from your code, but how to throttle an API that has already been extracted. For some tips on how you can mount your API in your Rails app, take a look at <a href="http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/">the Inductor blog</a> for a quick intro.</p>
<p>Now that you&#8217;ve skimmed the basics of mounting your Rack app (e.g. API) in your Rails app <em>without touching your rackup file</em>, you&#8217;re all set for the next step: <del datetime="2012-01-12T20:13:45+00:00">throttling!</del> testing!</p>
<p>Let&#8217;s go over a quick example with <a href="http://cukes.info/">Cucumber</a>. Don&#8217;t worry, I&#8217;ll keep it simple.<br />
<span id="more-130"></span><br />
Go ahead and install <code>cucumber-rails</code> and <code>database_cleaner</code> into your Gemfile, if you aren&#8217;t using them already.</p>
<p>Note: You can use anything for your Rack app here, Sinatra, Grape, etc. as long as it returns a 200 status code for its base path. If you want to follow along, you can use this dumb rack app. There&#8217;s a link to a Github repo at the end of the post.</p>
<p>Here it is, a simple Rack app (a proc!) mounted in your Rails app at &#8220;/api&#8221;. It always responds &#8220;OK&#8221;, with status code 200.</p>
<div id="gist-1603022" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1"># routes.rb</span></div><div class='line' id='LC2'><span class="no">RailsApp</span><span class="o">::</span><span class="no">Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="n">mount</span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="o">[</span><span class="s2">&quot;OK&quot;</span><span class="o">]]</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="p">}</span> <span class="o">=&gt;</span> <span class="s2">&quot;/api&quot;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="c1"># your other Rails routes</span></div><div class='line' id='LC8'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603022/2bb719d653bd1ee1d1aca7fd50a9905ee592d28c/routes.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603022#file_routes.rb" style="float:right;margin-right:10px;color:#666">routes.rb</a>
            <a href="https://gist.github.com/1603022">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At this point, you can start up the Rails server and hit your &#8220;API&#8221; at <a href="http://localhost:3000/api">http://localhost:3000/api</a>. You should see the word &#8220;<code>OK</code>&#8221; — that means it&#8217;s running. You just created a Rack app in your Rails app. Wasn&#8217;t that easy?</p>
<p>Now for the cuking. A simple feature to start off:</p>
<div id="gist-1603022" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c"># features/my_dumb_api.feature</span><span class="nf"></span></div><div class='line' id='LC2'><span class="k">Feature:</span><span class="nf"> My Dumb API</span></div><div class='line' id='LC3'><span class="nf">  In order to retrieve an API response</span></div><div class='line' id='LC4'><span class="nf">  As a web API developer</span></div><div class='line' id='LC5'><span class="nf">  I want an API to respond to my requests</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> API is available</span></div><div class='line' id='LC8'><span class="k">    When </span><span class="nf">I send a GET request for &quot;</span><span class="s">http://example.com/api/</span><span class="nf">&quot;</span></div><div class='line' id='LC9'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">200</span><span class="nf">&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603022/f2f8fcc7422814d25858ab9a4b6551c2690a2103/my_dumb_api.feature" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603022#file_my_dumb_api.feature" style="float:right;margin-right:10px;color:#666">my_dumb_api.feature</a>
            <a href="https://gist.github.com/1603022">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And some step definitions:</p>
<div id="gist-1603022" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">When</span> <span class="sr">/^I send a GET request for &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC3'><span class="k">end</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="no">Then</span> <span class="sr">/^the response code should be &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_response</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">code</span><span class="o">.</span><span class="n">to_i</span></div><div class='line' id='LC7'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603022/39bf593a0c4e9e008aacd47fef5aa9bc740583db/api_steps.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603022#file_api_steps.rb" style="float:right;margin-right:10px;color:#666">api_steps.rb</a>
            <a href="https://gist.github.com/1603022">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now give cucumber a run.</p>
<pre>
$ cucumber features/my_dumb_api.feature

Using the default profile...
# features/my_dumb_api.feature
Feature: My Dumb API
  In order to retrieve an API response
  As a web API developer
  I want an API to respond to my requests

  Scenario: API is available                                # features/my_dumb_api.feature:7
    When I send a GET request for "http://example.com/api/" # features/step_definitions/api_steps.rb:1
    Then the response code should be "200"                  # features/step_definitions/api_steps.rb:5

1 scenario (1 passed)
2 steps (2 passed)
0m0.154s
</pre>
<p>Excellent! It picked it up right away. <em>Now</em> we can start thinking about throttling. Jump over to your Gemfile and add <a href="https://github.com/datagraph/rack-throttle">rack-throttle</a>:</p>
<pre>
gem 'rack-throttle', :require => 'rack/throttle'
</pre>
<p>Update your bundle. Now, let&#8217;s start with the Cucumber feature this time.</p>
<div id="gist-1603143" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">Feature:</span><span class="nf"> My Dumb API</span></div><div class='line' id='LC2'><span class="nf">  In order to retrieve an API response</span></div><div class='line' id='LC3'><span class="nf">  As a web API developer</span></div><div class='line' id='LC4'><span class="nf">  I want an API to respond to my requests</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> API is available</span></div><div class='line' id='LC7'><span class="k">    When </span><span class="nf">I send a GET request for &quot;</span><span class="s">http://example.com/api/</span><span class="nf">&quot;</span></div><div class='line' id='LC8'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">200</span><span class="nf">&quot;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> Exceeding API Query Rate</span></div><div class='line' id='LC11'><span class="k">    When </span><span class="nf">I send more than one GET request in a second to &quot;</span><span class="s">http://example.com/api</span><span class="nf">&quot;</span></div><div class='line' id='LC12'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">403</span><span class="nf">&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603143/04e313002e3a134b0b0a0e15dee3493457449466/my_dumb_api.feature" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603143#file_my_dumb_api.feature" style="float:right;margin-right:10px;color:#666">my_dumb_api.feature</a>
            <a href="https://gist.github.com/1603143">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And it&#8217;s corresponding steps file:</p>
<div id="gist-1603143" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">When</span> <span class="sr">/^I send a GET request for &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC3'><span class="k">end</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="no">When</span> <span class="sr">/^I send more than one GET request in a second to &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># We&#39;ll assume this happens in &lt; 1 second</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC9'><span class="k">end</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="no">Then</span> <span class="sr">/^the response code should be &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_response</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">code</span><span class="o">.</span><span class="n">to_i</span></div><div class='line' id='LC13'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603143/a84591976e051b5a57dd5878556f125c06e81404/api_steps.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603143#file_api_steps.rb" style="float:right;margin-right:10px;color:#666">api_steps.rb</a>
            <a href="https://gist.github.com/1603143">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If we run this through Cucumber, it fails, because we haven&#8217;t done throttling yet. Jump to your routes file, and switch it to:</p>
<div id="gist-1603143" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">RailsApp</span><span class="o">::</span><span class="no">Application</span><span class="o">.</span><span class="n">routes</span><span class="o">.</span><span class="n">draw</span> <span class="k">do</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="n">mount</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Builder</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">use</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Throttle</span><span class="o">::</span><span class="no">Interval</span><span class="p">,</span> <span class="ss">:min</span> <span class="o">=&gt;</span> <span class="mi">1</span><span class="o">.</span><span class="mi">0</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">run</span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="o">[</span><span class="s2">&quot;OK&quot;</span><span class="o">]]</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="p">}</span> <span class="o">=&gt;</span> <span class="s2">&quot;/api&quot;</span></div><div class='line' id='LC8'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603143/e4bd6267148b82aa537b22363d2b9b761ed5e75e/routes.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603143#file_routes.rb" style="float:right;margin-right:10px;color:#666">routes.rb</a>
            <a href="https://gist.github.com/1603143">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>There, we just built a Rack app with middleware (the <code>Rack::Throttle</code> line), which defers to our Rack app (the proc) when the middleware passes the request onwards. Now when you run Cucumber, everything passes! You may think you&#8217;re ready to Cuke out the rest of your API, but you&#8217;re about to hit a roadblock — throttling hits all your Cucumber features. I have considered two ways to deal with this: </p>
<ol>
<li>Stub out <code>Rack::Throttle</code> and tell it which features you specifically want to throttle using Cucumber tags.</li>
<li>Use a separate Rack app for Cucumber testing, and turn throttling on for certain features with Cucumber tags.</li>
</ol>
<p>I chose the second option. First, I added the @no-throttle tag to the scenarios where throttling was not relevant:</p>
<div id="gist-1603216" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">Feature:</span><span class="nf"> My Dumb API</span></div><div class='line' id='LC2'><span class="nf">  In order to retrieve an API response</span></div><div class='line' id='LC3'><span class="nf">  As a web API developer</span></div><div class='line' id='LC4'><span class="nf">  I want an API to respond to my requests</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nf">  @no-throttle</span></div><div class='line' id='LC7'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> API is available</span></div><div class='line' id='LC8'><span class="k">    When </span><span class="nf">I send a GET request for &quot;</span><span class="s">http://example.com/api/</span><span class="nf">&quot;</span></div><div class='line' id='LC9'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">200</span><span class="nf">&quot;</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="nf">  </span><span class="k">Scenario:</span><span class="nf"> Exceeding API Query Rate</span></div><div class='line' id='LC12'><span class="k">    When </span><span class="nf">I send more than one GET request in a second to &quot;</span><span class="s">http://example.com/api</span><span class="nf">&quot;</span></div><div class='line' id='LC13'><span class="nf">    </span><span class="k">Then </span><span class="nf">the response code should be &quot;</span><span class="s">403</span><span class="nf">&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603216/107aa7591acb5050d997fc032af8bf4be8728212/my_dumb_api.feature" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603216#file_my_dumb_api.feature" style="float:right;margin-right:10px;color:#666">my_dumb_api.feature</a>
            <a href="https://gist.github.com/1603216">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And then a before filter in the steps file:</p>
<div id="gist-1603216" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="no">Before</span> <span class="s2">&quot;@no-throttle&quot;</span> <span class="k">do</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="vi">@app</span> <span class="o">=</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Builder</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">map</span> <span class="s2">&quot;/api&quot;</span> <span class="k">do</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">run</span> <span class="nb">proc</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">[</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="o">[</span><span class="s2">&quot;OK&quot;</span><span class="o">]]</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="p">}</span> </div><div class='line' id='LC9'><span class="k">end</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="no">When</span> <span class="sr">/^I send a GET request for &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC13'><span class="k">end</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="no">When</span> <span class="sr">/^I send more than one GET request in a second to &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">path</span><span class="o">|</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># We&#39;ll assume this happens in &lt; 1 second</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">get</span> <span class="n">path</span></div><div class='line' id='LC19'><span class="k">end</span> </div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC21'><span class="no">Then</span> <span class="sr">/^the response code should be &quot;([^&quot;]*)&quot;$/</span> <span class="k">do</span> <span class="o">|</span><span class="n">code</span><span class="o">|</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_response</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">should</span> <span class="o">==</span> <span class="n">code</span><span class="o">.</span><span class="n">to_i</span></div><div class='line' id='LC23'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1603216/fa1d4556da2657d087a8df31aeee9f3fefee6322/api_steps.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1603216#file_api_steps.rb" style="float:right;margin-right:10px;color:#666">api_steps.rb</a>
            <a href="https://gist.github.com/1603216">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now when you run your Cucumber features, the unthrottled scenarios will use a separate rack app, mounted without throttling. The downside to this method is that your have to keep the Rack app in <code>api_steps.rb</code> up to date with the one in <code>routes.rb</code>. A small price to pay, but less work than stubbing out <code>Rack::Throttle</code>.</p>
<p>You can <a href="https://github.com/openfirmware/dumb_app">browse the source code for this example Rails app</a> on Github.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesbadger.ca/2012/01/14/rails-3-rack-throttle-your-rack-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passenger and nginx Maintenance Page</title>
		<link>http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/</link>
		<comments>http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 20:01:27 +0000</pubDate>
		<dc:creator>James Badger</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Capistrano]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[Passenger]]></category>

		<guid isPermaLink="false">http://jamesbadger.ca/?p=97</guid>
		<description><![CDATA[I recently had the pleasure of moving a Rails app from Apache/Passenger based system to an nginx/Passenger system, deployed by Capistrano. In Apache, I had a setup where I could take a production site down for maintenance by touching &#8216;tmp/stop.txt&#8217; &#8230; <a href="http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently had the pleasure of moving a Rails app from Apache/Passenger based system to an nginx/Passenger system, deployed by Capistrano. In Apache, I had a setup where I could take a production site down for maintenance by touching &#8216;tmp/stop.txt&#8217; and using the following directive in my VirtualHost (courtesy of <a href="http://blog.nodeta.fi/2009/03/11/stopping-your-rails-application-with-phusion-passenger/"> Otto Hilska at Nodeta</a>):</p>
<pre>
&lt;VirtualHost *:80&gt;
	ServerName www.example.com
	DocumentRoot /u/apps/example/current/public
	ErrorDocument 503 /maintenance.html
	RewriteEngine on
	RewriteCond %{DOCUMENT_ROOT}/../tmp/stop.txt -f
	RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
	RewriteCond %{SCRIPT_FILENAME} !maintenance.html
	RewriteRule ^(.*)$ /$1 [R=503,L]

	&lt;Directory /u/apps/example/current/public&gt;
		RailsEnv production
		Options -MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	&lt;/Directory&gt;
&lt;/VirtualHost&gt;
</pre>
<p>This worked perfectly, and allowed me to use images/css/etc on my error documents without them being redirected to 503s as well. Based on <a href="http://www.ruby-forum.com/topic/113740#594606">Eli Miller&#8217;s solution</a>, I was able to write something similar for my nginx server config:</p>
<pre>
server {
	listen 80;
	server_name www.example.com;
	error_page 503 /maintenance.html;
	root /u/apps/example/current/public;
	passenger_enabled on;
	rails_env production;

	# If the maintenance stop file exists
	if (-f $document_root/../tmp/stop.txt) {
		set $maintenance 1;
	}

	# If the request exists as a static file in public
	if (-f $document_root/$uri) {
		set $maintenance 0;
	}

	if ($maintenance) {
		return 503;
	}
}
</pre>
<p>I was able to test this successfully with nginx 0.7.67 and 0.8.53, and Passenger 2.2.15 and 3.0.1. If you know a better/clearer way to do this, please share!</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesbadger.ca/2010/12/10/passenger-and-nginx-maintenance-page/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lighttpd + Mongrel + Rails + Windows</title>
		<link>http://jamesbadger.ca/2009/04/25/lighttpd-mongrel-rails-windows/</link>
		<comments>http://jamesbadger.ca/2009/04/25/lighttpd-mongrel-rails-windows/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 04:31:26 +0000</pubDate>
		<dc:creator>James Badger</dc:creator>
				<category><![CDATA[Local Talk]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[LightTPD]]></category>
		<category><![CDATA[Mongrel]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://jamesbadger.ca/?p=77</guid>
		<description><![CDATA[I recently worked on a project that required me to host a rails application on Windows XP Home, something I had no experience with but took on as our options were limited. We wanted a load-balanced server running a Rails &#8230; <a href="http://jamesbadger.ca/2009/04/25/lighttpd-mongrel-rails-windows/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently worked on a project that required me to host a rails application on Windows XP Home, something I had no experience with but took on as our options were limited. We wanted a load-balanced server running a Rails app with a MySQL database. An XAMPP install was also being used by another group on the server, through which we had MySQL access.</p>
<p>I learned quite a bit trying to get it working, and I hope I can help someone else with how I did it. I started this last fall, so some of the software may have been updated. Also, I would like to thank Joseph Jaramillo for his post on <a href="http://www.mightyinteractive.com/ruby-on-rails/deploying-to-iis-on-windows/">Deploying to IIS with lighttpd and Mongrel</a> which was a huge help setting this up.</p>
<p><span id="more-77"></span></p>
<h2 class="heading">1. Install Ruby</h2>
<p>For Ruby, I used the 1.8.6 <a href="http://www.ruby-lang.org/en/downloads/">one-click installer</a>. Download, and install.</p>
<h2 class="heading">2. Install Ruby Gems</h2>
<p>Hop on over to RubyForge and grab the <a href="http://rubyforge.org/frs/?group_id=126">latest zip file</a> (rubygems-1.3.2.zip as of this article). Download, and install.</p>
<h2 class="heading">3. Install Rails and Mongrel</h2>
<p>Open a command prompt and run <strong>gem install rails</strong> to get rails installed. Next, run <strong>gem install mongrel mongrel_service</strong> which will install the <a href="http://mongrel.rubyforge.org/">Mongrel</a> server.</p>
<h2 class="heading">4. Setup a Rails Application</h2>
<p>I will assume you have setup a Rails application before, and know how to create one from scratch or that you have one ready to drop in to be served. If you are not that experienced with Rails yet, I would recommend starting with Mongrel only for development.</p>
<p>Try out your Rails install by opening a command prompt to your Rails directory and running <strong>script\server</strong> (note the backslash), and then see if rails loads at <a href="http://localhost:3000/">http://localhost:3000/</a>. You may get a Windows Security Alert that you need to unblock to allow outgoing connections. At this point, you have enough to run a Rails server for development, but the aim of this article is to be more robust.</p>
<h2 class="heading">5. Install lighttpd</h2>
<p>Lighttpd is a great lightweight server that will be an excellent load-balancing proxy for this setup. We could have hooked into the Apache install from XAMPP, but I did not want to cause any problems for the group running that server.<br />
To install lighttpd, download the most recent lighttpd installer from the <a href="http://code.google.com/p/wlmp-project/">WLMP project</a> (I used LightTPD-1.4.22-1-Win32-SSL.exe). I had major problems with a Cygwin1.dll from an openSSH install conflicting with the ones for lighttpd, and had to remove the openSSH Cygwin1.dll before lighttpd would work properly; you can check for this error by running lighttpd from a command prompt and seeing if you get an error.</p>
<h2 class="heading">6. Install Windows Server 2003 Resource Kit</h2>
<p>To get lighttpd running as the kind of service we need, download and install the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&#038;displaylang=en">Windows Server 2003 Resource Kit</a>.</p>
<h2 class="heading">7. Install Mongrel Rails Service</h2>
<p>We are going to have two Mongrel servers serving our Rails application, although you can run more (See <a href="http://mongrel.rubyforge.org/wiki/Tuning">How Many Mongrel Instances Should I Run?</a>). To do this, open a command prompt and goto your Rails app directory. For each Mongrel server you want to run, enter the following command:<br />
<strong>mongrel_rails service::install -N rails_app_4001 -p 4001</strong><br />
Increase the number by one for each service (4002, 4003, etc) as you run the command. This command will install Windows services that will run our Mongrel servers in the background. To start these servers, run the following command for each server, incrementing the number for each server (4001, 4002, etc.):<br />
<strong>net start rails_app_4001</strong><br />
To make it easy to start and stop these servers, create a mserver.bat file in your Rails directory with the following contents:</p>
<blockquote><p>
@echo off<br />
net %1 rails_app_4001<br />
net %1 rails_app_4002
</p></blockquote>
<p>Add additional lines for each extra Mongrel server, again incrementing the number. This script will allow you to start/stop all the servers by opening a command prompt, going to the Rails directory, and typing <strong>mserver.bat start</strong> or <strong>mserver.bat stop</strong> , which will come in handy when restarting your Rails app. To test if these servers are running, open a web browser and navigate to ports <a href="http://localhost:4001/">4001</a>, <a href="http://localhost:4002/">4002</a>, etc on your server and see if your Rails app shows up.
</p>
<h2 class="heading">8. Setup lighttpd Proxy</h2>
<p>Download and install <a href="http://www.mightyinteractive.com/ruby-on-rails/deploying-to-iis-on-windows/lighttpd.conf">Joseph Jaramillo&#8217;s lighttpd conf file</a> to your Rails directory, and put it in your config directory.<br />
<br />Edit the file with an app that supports UNIX-style line endings (not notepad), changing the following values:</p>
<p><strong>var.basedir = &#8220;C:/Rails/myrailsapp&#8221;</strong> (point to your Rails app directory, use forward slashes)<br />
<strong>server.bind = &#8220;&#8221;</strong> (do not bind to a specific address)<br />
<strong>server.port = 3000</strong> (the port you want your users to use. 80 is a good default, or if already used as in my case, 3000)<br />
and under <strong>proxy.server</strong>, add extra entries for any additional Mongrel servers you are running.</p>
<p>Next, setup lighttpd to run as a Windows service. <a href="http://www.mightyinteractive.com/ruby-on-rails/deploying-to-iis-on-windows/index3.shtml">Follow Joseph&#8217;s instructions on step 3,</a> of course changing the AppParameters to use the path to your lighttpd config file in your rails directory. Test your setup by pointing your browser at your server&#8217;s main address (or other port if you chose one other than 80), and you should see your Rails application!</p>
<p></p>
<p>If you have any comments, suggestions, or problems let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamesbadger.ca/2009/04/25/lighttpd-mongrel-rails-windows/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

