Sha256: 76e7674f688389a21b2eb52cd0f71934f2cebfd1b0b2e46cc00c86275a5a2090

Contents?: true

Size: 1.63 KB

Versions: 24

Compression:

Stored size: 1.63 KB

Contents

# -*- encoding: binary -*-
require 'thread'

# Spawns a new thread for every client connection we accept().  This
# model is recommended for platforms like Ruby (MRI) 1.8 where spawning
# new threads is inexpensive, but still seems to work well enough with
# good native threading implementations such as NPTL under Linux on
# Ruby (MRI/YARV) 1.9
#
# This model should provide a high level of compatibility with all Ruby
# implementations, and most libraries and applications.  Applications
# running under this model should be thread-safe but not necessarily
# reentrant.
#
# If you're using green threads (MRI 1.8) and need to perform DNS lookups,
# consider using the "resolv-replace" library which replaces parts of the
# core Socket package with concurrent DNS lookup capabilities.

module Rainbows::ThreadSpawn
  include Rainbows::Base
  include Rainbows::WorkerYield

  def accept_loop(klass) #:nodoc:
    lock = Mutex.new
    limit = worker_connections
    nr = 0
    LISTENERS.each do |l|
      klass.new do
        begin
          if lock.synchronize { nr >= limit }
            worker_yield
          elsif client = l.kgio_accept
            klass.new(client) do |c|
              begin
                lock.synchronize { nr += 1 }
                c.process_loop
              ensure
                lock.synchronize { nr -= 1 }
              end
            end
          end
        rescue => e
          Rainbows::Error.listen_loop(e)
        end while Rainbows.alive
      end
    end
    sleep 1 while Rainbows.tick || lock.synchronize { nr > 0 }
  end

  def worker_loop(worker) #:nodoc:
    init_worker_process(worker)
    accept_loop(Thread)
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rainbows-5.2.1 lib/rainbows/thread_spawn.rb
rainbows-5.2.0 lib/rainbows/thread_spawn.rb
rainbows-5.1.1 lib/rainbows/thread_spawn.rb
rainbows-5.1.0 lib/rainbows/thread_spawn.rb
rainbows-5.0.0.5.ge717 lib/rainbows/thread_spawn.rb
rainbows-5.0.0 lib/rainbows/thread_spawn.rb
rainbows-4.7.0 lib/rainbows/thread_spawn.rb
rainbows-4.6.2 lib/rainbows/thread_spawn.rb
rainbows-4.6.1 lib/rainbows/thread_spawn.rb
rainbows-4.6.0.4.g4108 lib/rainbows/thread_spawn.rb
rainbows-4.6.0 lib/rainbows/thread_spawn.rb
rainbows-4.5.0 lib/rainbows/thread_spawn.rb
rainbows-4.4.3 lib/rainbows/thread_spawn.rb
rainbows-4.4.2 lib/rainbows/thread_spawn.rb
rainbows-4.4.1.1.gd5c8c lib/rainbows/thread_spawn.rb
rainbows-4.4.1 lib/rainbows/thread_spawn.rb
rainbows-4.4.0 lib/rainbows/thread_spawn.rb
rainbows-4.3.1 lib/rainbows/thread_spawn.rb
rainbows-4.3.0 lib/rainbows/thread_spawn.rb
rainbows-4.2.0 lib/rainbows/thread_spawn.rb