1 # -*- encoding: binary -*-
5 # Implements a worker thread pool model. This is suited for platforms
6 # like Ruby 1.9, where the cost of dynamically spawning a new thread
7 # for every new client connection is higher than with the ThreadSpawn
10 # This model should provide a high level of compatibility with all
11 # Ruby implementations, and most libraries and applications.
12 # Applications running under this model should be thread-safe
13 # but not necessarily reentrant.
15 # Applications using this model are required to be thread-safe.
16 # Threads are never spawned dynamically under this model. If you're
17 # connecting to external services and need to perform DNS lookups,
18 # consider using the "resolv-replace" library which replaces parts of
19 # the core Socket package with concurrent DNS lookup capabilities.
21 # This model probably less suited for many slow clients than the
22 # others and thus a lower +worker_connections+ setting is recommended.
28 def worker_loop(worker)
29 init_worker_process(worker)
30 pool = (1..worker_connections).map { new_worker_thread }
33 while G.alive && master_pid == Process.ppid
35 worker.tmp.chmod(m = 0 == m ? 1 : 0)
36 # if any worker dies, something is serious wrong, bail
40 join_threads(pool, worker)
47 # TODO: check if select() or accept() is a problem on large
48 # SMP systems under Ruby 1.9. Hundreds of native threads
49 # all working off the same socket could be a thundering herd
50 # problem. On the other hand, a thundering herd may not
51 # even incur as much overhead as an extra Mutex#synchronize
52 ret = IO.select(LISTENERS, nil, nil, 1) and
53 ret.first.each do |sock|
55 process_client(sock.accept_nonblock)
56 rescue Errno::EAGAIN, Errno::ECONNABORTED
60 rescue Errno::EBADF, TypeError