1 # -*- encoding: binary -*-
4 # Spawns a new thread for every client connection we accept(). This
5 # model is recommended for platforms where spawning threads is
8 # If you're connecting to external services and need to perform DNS
9 # lookups, consider using the "resolv-replace" library which replaces
10 # parts of the core Socket package with concurrent DNS lookup
16 def worker_loop(worker)
17 init_worker_process(worker)
18 RACK_DEFAULTS["rack.multithread"] = true
19 threads = ThreadGroup.new
22 limit = worker_connections
26 alive.chmod(m = 0 == m ? 1 : 0)
27 IO.select(LISTENERS, nil, nil, timeout) or next
30 rescue Errno::EBADF, TypeError
33 alive.chmod(m = 0 == m ? 1 : 0)
36 # Sleep if we're busy, another less busy worker process may
37 # take it for us if we sleep. This is gross but other options
38 # still suck because they require expensive/complicated
39 # synchronization primitives for _every_ case, not just this
40 # unlikely one. Since this case is (or should be) uncommon,
41 # just busy wait when we have to.
42 while threads.list.size > limit # unlikely
43 sleep(0.1) # hope another process took it
44 break # back to IO.select
47 threads.add(Thread.new(l.accept_nonblock) {|c| process_client(c) })
48 rescue Errno::EAGAIN, Errno::ECONNABORTED
52 listen_loop_error(e) if LISTENERS.first
53 end while LISTENERS.first && master_pid == Process.ppid
54 join_threads(threads.list, worker)