From 914481b635b0c7baefaf7d955c3cd59af2fafeb0 Mon Sep 17 00:00:00 2001 From: Evan Weaver Date: Sat, 31 Jan 2009 18:08:27 -0800 Subject: No more throttling. --- bin/mongrel_rails | 285 -------------------------------- lib/mongrel.rb | 16 +- lib/mongrel/configurator.rb | 385 -------------------------------------------- 3 files changed, 4 insertions(+), 682 deletions(-) delete mode 100644 bin/mongrel_rails delete mode 100644 lib/mongrel/configurator.rb diff --git a/bin/mongrel_rails b/bin/mongrel_rails deleted file mode 100644 index c9eac8f..0000000 --- a/bin/mongrel_rails +++ /dev/null @@ -1,285 +0,0 @@ -#!/usr/bin/env ruby -# -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'yaml' -require 'etc' - -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" -require 'mongrel' -require 'mongrel/rails' - -Mongrel::Gems.require 'gem_plugin' - -# require 'ruby-debug' -# Debugger.start - -module Mongrel - class Start < GemPlugin::Plugin "/commands" - include Mongrel::Command::Base - - def configure - options [ - ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"], - ["-d", "--daemonize", "Run daemonized in the background", :@daemon, false], - ['-p', '--port PORT', "Which port to bind to", :@port, 3000], - ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"], - ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"], - ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"], - ['-n', '--num-processors INT', "Number of processors active before clients denied", :@num_processors, 1024], - ['-N', '--num-threads INT', "Maximum number of requests to process concurrently", :@max_concurrent_threads, 1024], - ['-o', '--timeout TIME', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60], - ['-t', '--throttle TIME', "Time to pause (in hundredths of a second) between accepting clients", :@throttle, 0], - ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil], - ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd], - ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"], - ['-B', '--debug', "Enable debugging mode", :@debug, false], - ['-C', '--config PATH', "Use a config file", :@config_file, nil], - ['-S', '--script PATH', "Load the given file as an extra config script", :@config_script, nil], - ['-G', '--generate PATH', "Generate a config file for use with -C", :@generate, nil], - ['', '--user USER', "User to run as", :@user, nil], - ['', '--group GROUP', "Group to run as", :@group, nil], - ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil] - ] - end - - def validate - if @config_file - valid_exists?(@config_file, "Config file not there: #@config_file") - return false unless @valid - @config_file = File.expand_path(@config_file) - load_config - return false unless @valid - end - - @cwd = File.expand_path(@cwd) - valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" - - # Change there to start, then we'll have to come back after daemonize - Dir.chdir(@cwd) - - valid?(@prefix[0] == ?/ && @prefix[-1] != ?/, "Prefix must begin with / and not end in /") if @prefix - valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file" - valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file" - valid_dir? @docroot, "Path to docroot not valid: #@docroot" - valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map - valid_exists? @config_file, "Config file not there: #@config_file" if @config_file - valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate - valid_user? @user if @user - valid_group? @group if @group - - return @valid - end - - def run - if @generate - @generate = File.expand_path(@generate) - STDERR.puts "** Writing config to \"#@generate\"." - open(@generate, "w") {|f| f.write(settings.to_yaml) } - STDERR.puts "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file." - exit 0 - end - - config = Mongrel::Rails::RailsConfigurator.new(settings) do - if defaults[:daemon] - if File.exist? defaults[:pid_file] - log "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors." - log "!!! Exiting with error. You must stop mongrel and clear the .pid before I'll attempt a start." - exit 1 - end - - daemonize - write_pid_file - log "Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info." - log "Settings loaded from #{@config_file} (they override command line)." if @config_file - end - - log "Starting Mongrel listening at #{defaults[:host]}:#{defaults[:port]}" - - listener do - mime = {} - if defaults[:mime_map] - log "Loading additional MIME types from #{defaults[:mime_map]}" - mime = load_mime_map(defaults[:mime_map], mime) - end - - if defaults[:debug] - log "Installing debugging prefixed filters. Look in log/mongrel_debug for the files." - debug "/" - end - - log "Starting Rails with #{defaults[:environment]} environment..." - log "Mounting Rails at #{defaults[:prefix]}..." if defaults[:prefix] - uri defaults[:prefix] || "/", :handler => rails(:mime => mime, :prefix => defaults[:prefix]) - log "Rails loaded." - - log "Loading any Rails specific GemPlugins" - load_plugins - - if defaults[:config_script] - log "Loading #{defaults[:config_script]} external config script" - run_config(defaults[:config_script]) - end - - setup_rails_signals - end - end - - config.run - config.log "Mongrel #{Mongrel::Const::MONGREL_VERSION} available at #{@address}:#{@port}" - - unless config.defaults[:daemon] - config.log "Use CTRL-C to stop." - end - - config.join - - if config.needs_restart - if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ - cmd = "ruby #{__FILE__} start #{original_args.join(' ')}" - config.log "Restarting with arguments: #{cmd}" - config.stop(false, true) - config.remove_pid_file - - if config.defaults[:daemon] - system cmd - else - STDERR.puts "Can't restart unless in daemon mode." - exit 1 - end - else - config.log "Win32 does not support restarts. Exiting." - end - end - end - - def load_config - settings = {} - begin - settings = YAML.load_file(@config_file) - ensure - STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon] - end - - settings[:includes] ||= ["mongrel"] - - # Config file settings will override command line settings - settings.each do |key, value| - key = key.to_s - if config_keys.include?(key) - key = 'address' if key == 'host' - self.instance_variable_set("@#{key}", value) - else - failure "Unknown configuration setting: #{key}" - @valid = false - end - end - end - - def config_keys - @config_keys ||= - %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors timeout throttle user group prefix max_concurrent_threads) - end - - def settings - config_keys.inject({}) do |hash, key| - value = self.instance_variable_get("@#{key}") - key = 'host' if key == 'address' - hash[key.to_sym] ||= value - hash - end - end - end - - def Mongrel::send_signal(signal, pid_file) - pid = File.read(pid_file).to_i - print "Sending #{signal} to Mongrel at PID #{pid}..." - begin - Process.kill(signal, pid) - rescue Errno::ESRCH - puts "Process does not exist. Not running." - end - - puts "Done." - end - - - class Stop < GemPlugin::Plugin "/commands" - include Mongrel::Command::Base - - def configure - options [ - ['-c', '--chdir PATH', "Change to dir before starting (will be expanded).", :@cwd, "."], - ['-f', '--force', "Force the shutdown (kill -9).", :@force, false], - ['-w', '--wait SECONDS', "Wait SECONDS before forcing shutdown", :@wait, "0"], - ['-P', '--pid FILE', "Where the PID file is located (cannot be changed via soft restart).", :@pid_file, "log/mongrel.pid"] - ] - end - - def validate - @cwd = File.expand_path(@cwd) - valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" - - Dir.chdir @cwd - - valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?" - return @valid - end - - def run - if @force - @wait.to_i.times do |waiting| - exit(0) if not File.exist? @pid_file - sleep 1 - end - - Mongrel::send_signal("KILL", @pid_file) if File.exist? @pid_file - else - Mongrel::send_signal("TERM", @pid_file) - end - end - end - - - class Restart < GemPlugin::Plugin "/commands" - include Mongrel::Command::Base - - def configure - options [ - ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, '.'], - ['-s', '--soft', "Do a soft restart rather than a process exit restart", :@soft, false], - ['-P', '--pid FILE', "Where the PID file is located", :@pid_file, "log/mongrel.pid"] - ] - end - - def validate - @cwd = File.expand_path(@cwd) - valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" - - Dir.chdir @cwd - - valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?" - return @valid - end - - def run - if @soft - Mongrel::send_signal("HUP", @pid_file) - else - Mongrel::send_signal("USR2", @pid_file) - end - end - end -end - - -GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE, "rails" => GemPlugin::EXCLUDE - - -if not Mongrel::Command::Registry.instance.run ARGV - exit 1 -end diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 00e7624..0b90057 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -66,14 +66,13 @@ module Mongrel attr_reader :workers attr_reader :host attr_reader :port - attr_reader :throttle attr_reader :timeout attr_reader :max_queued_threads + attr_reader :max_concurrent_threads DEFAULTS = { :max_queued_threads => 20, :max_concurrent_threads => 20, - :throttle => 0, :timeout => 60 } @@ -87,9 +86,6 @@ module Mongrel # way to deal with overload. Other schemes involve still parsing the client's request # which defeats the point of an overload handling system. # - # The throttle parameter is a sleep timeout (in hundredths of a second) that is placed between - # socket.accept calls in order to give the server a cheap throttle time. It defaults to 0 and - # actually if it is 0 then the sleep is not done at all. def initialize(host, port, app, options = {}) options = DEFAULTS.merge(options) @@ -100,7 +96,6 @@ module Mongrel @host, @port, @app = host, port, app @workers = ThreadGroup.new - @throttle = options[:throttle] / 100.0 @timeout = options[:timeout] @max_queued_threads = options[:max_queued_threads] @max_concurrent_threads = options[:max_concurrent_threads] @@ -200,7 +195,7 @@ module Mongrel @workers.list.each do |worker| worker[:started_on] = Time.now if not worker[:started_on] - if mark - worker[:started_on] > @timeout + @throttle + if mark - worker[:started_on] > @timeout Mongrel.logger.info "Thread #{worker.inspect} is too old, killing." worker.raise(TimeoutError.new(error_msg)) end @@ -212,11 +207,10 @@ module Mongrel # Performs a wait on all the currently running threads and kills any that take # too long. It waits by @timeout seconds, which can be set in .initialize or - # via mongrel_rails. The @throttle setting does extend this waiting period by - # that much longer. + # via mongrel_rails. def graceful_shutdown while reap_dead_workers("shutdown") > 0 - Mongrel.logger.info "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds." + Mongrel.logger.info "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout} seconds." sleep @timeout / 10 end end @@ -269,8 +263,6 @@ module Mongrel thread = Thread.new(client) {|c| semaphore.synchronize { process_client(c) } } thread[:started_on] = Time.now @workers.add(thread) - - sleep @throttle if @throttle > 0 end rescue StopServer break diff --git a/lib/mongrel/configurator.rb b/lib/mongrel/configurator.rb deleted file mode 100644 index 2442152..0000000 --- a/lib/mongrel/configurator.rb +++ /dev/null @@ -1,385 +0,0 @@ -require 'yaml' -require 'etc' - -module Mongrel - # Implements a simple DSL for configuring a Mongrel server for your - # purposes. More used by framework implementers to setup Mongrel - # how they like, but could be used by regular folks to add more things - # to an existing mongrel configuration. - # - # It is used like this: - # - # require 'mongrel' - # config = Mongrel::Configurator.new :host => "127.0.0.1" do - # listener :port => 3000 do - # uri "/app", :handler => Mongrel::DirHandler.new(".", load_mime_map("mime.yaml")) - # end - # run - # end - # - # This will setup a simple DirHandler at the current directory and load additional - # mime types from mimy.yaml. The :host => "127.0.0.1" is actually not - # specific to the servers but just a hash of default parameters that all - # server or uri calls receive. - # - # When you are inside the block after Mongrel::Configurator.new you can simply - # call functions that are part of Configurator (like server, uri, daemonize, etc) - # without having to refer to anything else. You can also call these functions on - # the resulting object directly for additional configuration. - # - # A major thing about Configurator is that it actually lets you configure - # multiple listeners for any hosts and ports you want. These are kept in a - # map config.listeners so you can get to them. - # - # * :pid_file => Where to write the process ID. - class Configurator - attr_reader :listeners - attr_reader :defaults - attr_reader :needs_restart - - # You pass in initial defaults and then a block to continue configuring. - def initialize(defaults={}, &block) - @listener = nil - @listener_name = nil - @listeners = {} - @defaults = defaults - @needs_restart = false - @pid_file = defaults[:pid_file] - - if block - cloaker(&block).bind(self).call - end - end - - # Change privileges of the process to specified user and group. - def change_privilege(user, group) - begin - uid, gid = Process.euid, Process.egid - target_uid = Etc.getpwnam(user).uid if user - target_gid = Etc.getgrnam(group).gid if group - - if uid != target_uid or gid != target_gid - log "Initiating groups for #{user.inspect}:#{group.inspect}." - Process.initgroups(user, target_gid) - - log "Changing group to #{group.inspect}." - Process::GID.change_privilege(target_gid) - - log "Changing user to #{user.inspect}." - Process::UID.change_privilege(target_uid) - end - rescue Errno::EPERM => e - log "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}." - log "Mongrel failed to start." - exit 1 - end - end - - def remove_pid_file - File.unlink(@pid_file) if @pid_file and File.exists?(@pid_file) - end - - # Writes the PID file if we're not on Windows. - def write_pid_file - if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ - log "Writing PID file to #{@pid_file}" - open(@pid_file,"w") {|f| f.write(Process.pid) } - open(@pid_file,"w") do |f| - f.write(Process.pid) - File.chmod(0644, @pid_file) - end - end - end - - # Generates a class for cloaking the current self and making the DSL nicer. - def cloaking_class - class << self - self - end - end - - # Do not call this. You were warned. - def cloaker(&block) - cloaking_class.class_eval do - define_method :cloaker_, &block - meth = instance_method( :cloaker_ ) - remove_method :cloaker_ - meth - end - end - - # This will resolve the given options against the defaults. - # Normally just used internally. - def resolve_defaults(options) - options.merge(@defaults) - end - - # Starts a listener block. This is the only one that actually takes - # a block and then you make Configurator.uri calls in order to setup - # your URIs and handlers. If you write your Handlers as GemPlugins - # then you can use load_plugins and plugin to load them. - # - # It expects the following options (or defaults): - # - # * :host => Host name to bind. - # * :port => Port to bind. - # * :max_queued_threads => The maximum number of concurrent threads allowed. - # * :throttle => Time to pause (in hundredths of a second) between accepting clients. - # * :timeout => Time to wait (in seconds) before killing a stalled thread. - # * :user => User to change to, must have :group as well. - # * :group => Group to change to, must have :user as well. - # - def listener(options={},&block) - raise "Cannot call listener inside another listener block." if (@listener or @listener_name) - ops = resolve_defaults(options) - - @listener = Mongrel::HttpServer.new(ops[:host], ops[:port].to_i, ops) - @listener_name = "#{ops[:host]}:#{ops[:port]}" - @listeners[@listener_name] = @listener - - if ops[:user] and ops[:group] - change_privilege(ops[:user], ops[:group]) - end - - # Does the actual cloaking operation to give the new implicit self. - if block - cloaker(&block).bind(self).call - end - - # all done processing this listener setup, reset implicit variables - @listener = nil - @listener_name = nil - end - - - # Called inside a Configurator.listener block in order to - # add URI->handler mappings for that listener. Use this as - # many times as you like. It expects the following options - # or defaults: - # - # * :handler => HttpHandler -- Handler to use for this location. - # * :in_front => true/false -- Rather than appending, it prepends this handler. - def uri(location, options={}) - ops = resolve_defaults(options) - @listener.register(location, ops[:handler], ops[:in_front]) - end - - - # Daemonizes the current Ruby script turning all the - # listeners into an actual "server" or detached process. - # You must call this *before* frameworks that open files - # as otherwise the files will be closed by this function. - # - # Does not work for Win32 systems (the call is silently ignored). - # - # Requires the following options or defaults: - # - # * :cwd => Directory to change to. - # * :log_file => Where to write STDOUT and STDERR. - # - # It is safe to call this on win32 as it will only require the daemons - # gem/library if NOT win32. - def daemonize(options={}) - ops = resolve_defaults(options) - # save this for later since daemonize will hose it - if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ - require 'daemons/daemonize' - - logfile = ops[:log_file] - if logfile[0].chr != "/" - logfile = File.join(ops[:cwd],logfile) - if not File.exist?(File.dirname(logfile)) - log "!!! Log file directory not found at full path #{File.dirname(logfile)}. Update your configuration to use a full path." - exit 1 - end - end - - Daemonize.daemonize(logfile) - - # change back to the original starting directory - Dir.chdir(ops[:cwd]) - - else - log "WARNING: Win32 does not support daemon mode." - end - end - - - # Uses the GemPlugin system to easily load plugins based on their - # gem dependencies. You pass in either an :includes => [] or - # :excludes => [] setting listing the names of plugins to include - # or exclude from the determining the dependencies. - def load_plugins(options={}) - ops = resolve_defaults(options) - - load_settings = {} - if ops[:includes] - ops[:includes].each do |plugin| - load_settings[plugin] = GemPlugin::INCLUDE - end - end - - if ops[:excludes] - ops[:excludes].each do |plugin| - load_settings[plugin] = GemPlugin::EXCLUDE - end - end - - GemPlugin::Manager.instance.load(load_settings) - end - - - # Easy way to load a YAML file and apply default settings. - def load_yaml(file, default={}) - default.merge(YAML.load_file(file)) - end - - - # Loads the MIME map file and checks that it is correct - # on loading. This is commonly passed to Mongrel::DirHandler - # or any framework handler that uses DirHandler to serve files. - # You can also include a set of default MIME types as additional - # settings. See Mongrel::DirHandler for how the MIME types map - # is organized. - def load_mime_map(file, mime={}) - # configure any requested mime map - mime = load_yaml(file, mime) - - # check all the mime types to make sure they are the right format - mime.each {|k,v| log "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 } - - return mime - end - - - # Loads and creates a plugin for you based on the given - # name and configured with the selected options. The options - # are merged with the defaults prior to passing them in. - def plugin(name, options={}) - ops = resolve_defaults(options) - GemPlugin::Manager.instance.create(name, ops) - end - - # Lets you do redirects easily as described in Mongrel::RedirectHandler. - # You use it inside the configurator like this: - # - # redirect("/test", "/to/there") # simple - # redirect("/to", /t/, 'w') # regexp - # redirect("/hey", /(w+)/) {|match| ...} # block - # - def redirect(from, pattern, replacement = nil, &block) - uri from, :handler => Mongrel::RedirectHandler.new(pattern, replacement, &block) - end - - # Works like a meta run method which goes through all the - # configured listeners. Use the Configurator.join method - # to prevent Ruby from exiting until each one is done. - def run - @listeners.each {|name,s| - s.run - } - - $mongrel_sleeper_thread = Thread.new { loop { sleep 1 } } - end - - # Calls .stop on all the configured listeners so they - # stop processing requests (gracefully). By default it - # assumes that you don't want to restart. - def stop(needs_restart=false, synchronous=false) - @listeners.each do |name,s| - s.stop(synchronous) - end - @needs_restart = needs_restart - end - - - # This method should actually be called *outside* of the - # Configurator block so that you can control it. In other words - # do it like: config.join. - def join - @listeners.values.each {|s| s.acceptor.join } - end - - - # Calling this before you register your URIs to the given location - # will setup a set of handlers that log open files, objects, and the - # parameters for each request. This helps you track common problems - # found in Rails applications that are either slow or become unresponsive - # after a little while. - # - # You can pass an extra parameter *what* to indicate what you want to - # debug. For example, if you just want to dump rails stuff then do: - # - # debug "/", what = [:rails] - # - # And it will only produce the log/mongrel_debug/rails.log file. - # Available options are: :access, :files, :objects, :threads, :rails - # - # NOTE: Use [:files] to get accesses dumped to stderr like with WEBrick. - def debug(location, what = [:access, :files, :objects, :threads, :rails]) - require 'mongrel/debug' - handlers = { - :access => "/handlers/requestlog::access", - :files => "/handlers/requestlog::files", - :objects => "/handlers/requestlog::objects", - :threads => "/handlers/requestlog::threads", - :rails => "/handlers/requestlog::params" - } - - # turn on the debugging infrastructure, and ObjectTracker is a pig - MongrelDbg.configure - - # now we roll through each requested debug type, turn it on and load that plugin - what.each do |type| - MongrelDbg.begin_trace type - uri location, :handler => plugin(handlers[type]) - end - end - - # Used to allow you to let users specify their own configurations - # inside your Configurator setup. You pass it a script name and - # it reads it in and does an eval on the contents passing in the right - # binding so they can put their own Configurator statements. - def run_config(script) - open(script) {|f| eval(f.read, proc {self}) } - end - - # Sets up the standard signal handlers that are used on most Ruby - # It only configures if the platform is not win32 and doesn't do - # a HUP signal since this is typically framework specific. - # - # Requires a :pid_file option given to Configurator.new to indicate a file to delete. - # It sets the MongrelConfig.needs_restart attribute if - # the start command should reload. It's up to you to detect this - # and do whatever is needed for a "restart". - # - # This command is safely ignored if the platform is win32 (with a warning) - def setup_signals(options={}) - ops = resolve_defaults(options) - - # forced shutdown, even if previously restarted (actually just like TERM but for CTRL-C) - trap("INT") { log "INT signal received."; stop(false) } - - # clean up the pid file always - at_exit { remove_pid_file } - - if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ - # graceful shutdown - trap("TERM") { log "TERM signal received."; stop } - trap("USR1") { log "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client } - # restart - trap("USR2") { log "USR2 signal received."; stop(true) } - - log "Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart)." - else - log "Signals ready. INT => stop (no restart)." - end - end - - # Logs a simple message to STDERR (or the mongrel log if in daemon mode). - def log(msg) - STDERR.print "** ", msg, "\n" - end - - end -end -- cgit v1.2.3-24-ge0c7