Changeset 198

Show
Ignore:
Timestamp:
03/09/07 17:35:21 (2 years ago)
Author:
e.@brainspl.at
Message:

Update to merb handler and merb dispatcher. Moved the Mutex lock around action dispatcing into the Dispatcher class. Also made it configurable. By default the mutex is locked for each dispacth. You can set :use_mutex: false in your merb.yml file or use --mutex on or --mutex off from the merb command line to turn it on and off for different runs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/merb_dispatcher.rb

    r193 r198  
    66      attr_accessor :path_prefix 
    77       
     8      @@mutex = Mutex.new 
     9      @@use_mutex = ::Merb::Server.use_mutex 
    810      # This is where we grab the incoming request PATH_INFO 
    911      # and use that in the merb routematcher to determine 
     
    1921        rest = route.delete(:rest) 
    2022       
    21         controller = instantiate_controller(route[:controller], request.body, request.params, route, response) 
     23        controller = instantiate_controller(route[:controller], request.body, request.params, route, response) 
    2224       
    2325        if rest 
    2426          method = controller.request.method 
    25           if allowed.keys.include?(method) 
    26             controller.params[:action] = allowed[method] 
    27             [controller, allowed[method]] 
     27          if allowed.keys.include?(method) && action = allowed[method] 
     28            controller.params[:action] = action 
    2829          else 
    2930            raise Merb::RestfulMethodNotAllowed.new(method, allowed) 
    3031          end   
    3132        else   
    32           [controller, route[:action]
     33          action = route[:action
    3334        end 
     35        if @@use_mutex 
     36          @@mutex.synchronize { 
     37            controller.dispatch(action) 
     38          } 
     39        else 
     40          controller.dispatch(action) 
     41        end 
     42        [controller, action] 
    3443      end 
    3544       
  • trunk/lib/merb/merb_handler.rb

    r174 r198  
    2020  def initialize(dir, opts = {}) 
    2121    @files = Mongrel::DirHandler.new(dir,false) 
    22     @guard = Mutex.new 
    2322  end 
    2423   
     
    6463    else 
    6564      begin 
    66         # This handles parsing the query string and post/file upload 
    67         # params and is outside of the synchronize call so that 
    68         # multiple file uploads can be done at once. 
     65        # dLet Merb:Dispatcher find the route and call the filter chain and action 
    6966        controller = nil 
    7067        controller, action = Merb::Dispatcher.handle(request, response) 
    7168         
    7269        MERB_LOGGER.info("Routing to controller: #{controller.class} action: #{action}\nRoute Recognition & Parsing HTTP Input took: #{Time.now - start} seconds") 
    73          
    74         # We need a mutex here because ActiveRecord code can be run 
    75         # in your controller actions. AR performs much better in single 
    76         # threaded mode so we lock here for the shortest amount of time  
    77         # possible. Route recognition and mime parsing has already occured 
    78         # at this point because those processes are thread safe. This  
    79         # gives us the best trade off for multi threaded performance  
    80         # of thread safe code paths, and the smallest lock possible 
    81         # around calls to your controller actions. 
    82         @guard.synchronize { 
    83           controller.dispatch(action) 
    84         } 
    8570      rescue Object => e 
    8671        response.start(500) do |head,out| 
  • trunk/lib/merb/merb_server.rb

    r197 r198  
    1515        :allow_reloading => true, 
    1616        :merb_root => Dir.pwd, 
    17         :cache_templates => false 
     17        :cache_templates => false, 
     18        :use_mutex => true 
    1819      } 
    1920      begin 
     
    3536       
    3637        opts = OptionParser.new do |opts| 
    37           opts.banner = "Usage: merb [fdcepghmisluG] [argument]" 
     38          opts.banner = "Usage: merb [fdcepghmisluMG] [argument]" 
    3839          opts.define_head "Merb Mongrel+ Erb. Lightweight replacement for ActionPack" 
    3940          opts.separator '*'*80 
     
    101102          opts.on("-M", "--merb-config FILENAME", "This flag is for explicitly declaring the merb app's config file") do |config| 
    102103            options[:merb_config] = config 
     104          end 
     105           
     106          opts.on("-X", "--mutex on/off", "This flag is for turnhing on and off the mutex lock.") do |mutex| 
     107            if mutex == 'off' 
     108              options[:use_mutex] = false 
     109            else 
     110              options[:use_mutex] = true 
     111            end    
    103112          end 
    104113           
  • trunk/specs/merb/merb_dispatch_spec.rb

    r194 r198  
    220220    controller.class.should == Foo 
    221221    action.should == "bar" 
    222     controller.dispatch(action) 
    223222    controller.body.should == "bar" 
    224223  end 
     
    228227    controller.class.should == Icon 
    229228    action.should == "show" 
    230     controller.dispatch(action) 
    231229    controller.body.should == :show 
    232230  end 
     
    237235    action.should == "show" 
    238236    controller.params[:format].should == 'xml' 
    239     controller.dispatch(action) 
    240237    controller.body.should == :show 
    241238  end 
     
    245242    controller.class.should == Icon 
    246243    action.should == "new" 
    247     controller.dispatch(action) 
    248244    controller.body.should == :new 
    249245  end 
     
    253249    controller.class.should == Icon 
    254250    action.should == "edit" 
    255     controller.dispatch(action) 
    256251    controller.body.should == :edit 
    257252  end 
     
    261256    controller.class.should == Icon 
    262257    action.should == "create" 
    263     controller.dispatch(action) 
    264258    controller.body.should == :create 
    265259  end 
     
    269263    controller.class.should == Icon 
    270264    action.should == "update" 
    271     controller.dispatch(action) 
    272265    controller.body.should == :update 
    273266  end 
     
    277270    controller.class.should == Icon 
    278271    action.should == "destroy" 
    279     controller.dispatch(action) 
    280272    controller.body.should == :destroy 
    281273  end 
     
    285277    controller.class.should == Tags 
    286278    action.should == "index" 
    287     controller.dispatch(action) 
    288279    controller.body.should == :index 
    289280  end 
     
    294285    action.should == "index" 
    295286    controller.params[:format].should == 'xml' 
    296     controller.dispatch(action) 
    297287    controller.body.should == :index 
    298288  end 
     
    302292    controller.class.should == Posts 
    303293    action.should == "index" 
    304     controller.dispatch(action) 
    305294    controller.body.should == :index 
    306295  end 
     
    310299    controller.class.should == Posts 
    311300    action.should == "filter" 
    312     controller.dispatch(action) 
    313301    controller.body.should == :filter 
    314302  end 
     
    318306    controller.class.should == Comments 
    319307    action.should == "index" 
    320     controller.dispatch(action) 
    321308    controller.body.should == :index 
    322309  end 
     
    326313    controller.class.should == Profile 
    327314    action.should == "show" 
    328     controller.dispatch(action) 
    329315    controller.body.should == :show 
    330316  end 
     
    335321    controller.params[:format].should == 'xml' 
    336322    action.should == "show" 
    337     controller.dispatch(action) 
    338323    controller.body.should == :show 
    339324  end 
     
    344329    controller.params[:format].should == 'xml' 
    345330    action.should == "index" 
    346     controller.dispatch(action) 
    347331    controller.body.should == :index 
    348332  end 
     
    353337    action.should == "show" 
    354338    controller.params[:id].should == '1' 
    355     controller.dispatch(action) 
    356339    controller.body.should == :show 
    357340  end 
     
    363346    controller.params[:post_id].should == '1' 
    364347    action.should == "show" 
    365     controller.dispatch(action) 
    366348    controller.body.should == :show 
    367349  end 
     
    373355    controller.params[:a_id].should == '1' 
    374356    action.should == "show" 
    375     controller.dispatch(action) 
    376357    controller.body.should == :show 
    377358  end 
     
    383364    controller.params[:a_id].should == '1' 
    384365    action.should == "index" 
    385     controller.dispatch(action) 
    386366    controller.body.should == :index 
    387367  end 
     
    394374    controller.params[:b_id].should == '1' 
    395375    action.should == "show" 
    396     controller.dispatch(action) 
    397376    controller.body.should == :show 
    398377  end 
     
    404383    action.should == "show" 
    405384    controller.params[:id].should == '1' 
    406     controller.dispatch(action) 
    407385    controller.body.should == :show 
    408386  end 
     
    415393    controller.params[:post_id].should == '1' 
    416394    action.should == "show" 
    417     controller.dispatch(action) 
    418395    controller.body.should == :show 
    419396  end 
     
    423400    controller.class.should == Posts 
    424401    action.should == "new" 
    425     controller.dispatch(action) 
    426402    controller.body.should == :new 
    427403  end 
     
    431407    controller.class.should == Comments 
    432408    action.should == "new" 
    433     controller.dispatch(action) 
    434409    controller.body.should == :new 
    435410  end 
     
    440415    action.should == "edit" 
    441416    controller.params[:id].should == '1' 
    442     controller.dispatch(action) 
    443417    controller.body.should == :edit 
    444418  end 
     
    450424    controller.params[:post_id].should == '1' 
    451425    action.should == "edit" 
    452     controller.dispatch(action) 
    453426    controller.body.should == :edit 
    454427  end 
     
    458431    controller.class.should == Posts 
    459432    action.should == "create" 
    460     controller.dispatch(action) 
    461433    controller.body.should == :create 
    462434  end 
     
    466438    controller.class.should == Comments 
    467439    action.should == "create" 
    468     controller.dispatch(action) 
    469440    controller.body.should == :create 
    470441  end 
     
    475446    controller.params[:format].should == 'xml' 
    476447    action.should == "create" 
    477     controller.dispatch(action) 
    478448    controller.body.should == :create 
    479449  end 
     
    485455    controller.params[:format].should == 'xml' 
    486456    action.should == "create" 
    487     controller.dispatch(action) 
    488457    controller.body.should == :create 
    489458  end 
     
    494463    action.should == "update" 
    495464    controller.params[:id].should == '1' 
    496     controller.dispatch(action) 
    497465    controller.body.should == :update 
    498466  end 
     
    504472    controller.params[:post_id].should == '1' 
    505473    controller.params[:id].should == '1' 
    506     controller.dispatch(action) 
    507474    controller.body.should == :update 
    508475  end 
     
    514481    controller.params[:id].should == '1' 
    515482    action.should == "update" 
    516     controller.dispatch(action) 
    517483    controller.body.should == :update 
    518484  end 
     
    525491    controller.params[:format].should == 'xml' 
    526492    controller.params[:id].should == '1' 
    527     controller.dispatch(action) 
    528493    controller.body.should == :update 
    529494  end 
     
    534499    controller.params[:id].should == '1' 
    535500    action.should == "destroy" 
    536     controller.dispatch(action) 
    537501    controller.body.should == :destroy 
    538502  end 
     
    544508    controller.params[:post_id].should == '1' 
    545509    action.should == "destroy" 
    546     controller.dispatch(action) 
    547510    controller.body.should == :destroy 
    548511  end 
     
    554517    controller.params[:id].should == '1' 
    555518    action.should == "destroy" 
    556     controller.dispatch(action) 
    557519    controller.body.should == :destroy 
    558520  end 
     
    565527    controller.params[:post_id].should == '1' 
    566528    action.should == "destroy" 
    567     controller.dispatch(action) 
    568529    controller.body.should == :destroy 
    569530  end 
     
    574535    action.should == 'stats' 
    575536    controller.params[:id].should == '1' 
    576     controller.dispatch(action) 
    577537    controller.body.should == :stats 
    578538  end 
     
    584544    controller.params[:id].should == '1' 
    585545    controller.params[:post_id].should == '1' 
    586     controller.dispatch(action) 
    587546    controller.body.should == :stats 
    588547  end 
     
    593552    action.should == 'stats' 
    594553    controller.params[:id].should == '1' 
    595     controller.dispatch(action) 
    596554    controller.body.should == :stats 
    597555  end 
     
    603561    controller.params[:id].should == '1' 
    604562    controller.params[:post_id].should == '1' 
    605     controller.dispatch(action) 
    606563    controller.body.should == :stats 
    607564  end