'X-Sendfile' for Rack , take 2

Posted by stoyan

In the spirit of “Fork me if you like me” I forked the Rack project on github and applied my ‘X-Sendfile’ related changes. Now you can have download acceleration with:
use Rack::Static, :urls => ["/files"], :root => "public",
    :extra => { 'X-Sendfile' => 'yes' }
I just made a small change to pass the extra parameter down to Rack::File and adding ‘X-Sendfile’ related headers for nginx, apache and lighttpd. extra parameter can also contain others, non x-sendfile related headers (like cache-control etc.):
use Rack::Static, :urls => ["/css","/images"], :root => "public", :extra => { 
  'Cache-Control' => 'max-age=86400, public',
  'Expires' => (Time.now + 86400).utc.rfc2822
}
I also added some examples for rackup and middleware to the examples/ directory. Merged the josh’s daemonize fork too, but today (11-May-2008) it was merged to the master branch.

Rack::XStatic Middleware - 'X-Sendfile' for Rack

Posted by stoyan

After I found the Rails X-Sendfile plugin , decided to code something similar for Rack (and Ramaze). The result: Rack::XStatic middleware . Example usage: xfile.ru rackup file . Start it with:
rackup -s thin -p 7000 xfile.ru
For using it with nginx ’X-Accel-Redirect’ :
# inside your .ru rackup file:
use Rack::XStatic, :urls => ["/down", "/files"], :root => "public",  :extra => { 
  'X-Sendfile-Type' => 'nginx',
  'X-Accel-Limit-Rate' => '1024',
  'X-Accel-Charset' => 'utf-8'
}
# inside your nginx.conf file:
location ~ ^/(down|files)/ {
  internal;
  root /full/path/to/rack/public;
}

‘X-Sendfile-Type’ can be also ‘lighttpd’ , ‘apache’ or just ‘yes’ for adding only the ‘X-Sendfile’ header.

Rack::XStatic also allow me to add custom headers to the response :
use Rack::XStatic, :urls => ["/css","/images"], :root => "public", :extra => { 
  'Cache-Control' => 'max-age=86400, public',
  'Expires' => (Time.now + 86400).utc.rfc2822
}

Throttler Ramaze helper

Posted by stoyan

After reading Rails Plugin: Throttler I created a Throttler Ramaze Helper .

  • What is it for? ”...The throttling plugin monitors the load of your server and allows you to disable certain features of your app when the load is too high…”
  • How to use it?
    • Copy throttler.rb to /{ramaze_gem_dir}/lib/ramaze/helper/throttler.rb
    • In your controllers code:
      class MainController < Ramaze::Controller
        helper :throttler
      
        def index
            # (optional): override the threshold when calling throttled?()
            # unless throttled?(10.00) ...
            unless throttled?
               "Hello World" 
            else
               "Overloaded: #{current_load}" 
            end
        end
      
      protected
         # (optional) set another threshold value
         def threshold
            5.00
         end
      end
      

Thanks a lot to Kashia from #ramaze for the help.

Later I start thinking “Hm, maybe that one is better to be Rack middleware ...”. Maybe next try…