unicorn-instruments 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/unicorn-instruments.rb +41 -45
- metadata +1 -1
data/unicorn-instruments.rb
CHANGED
@@ -1,53 +1,49 @@
|
|
1
|
-
|
2
|
-
def
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
l = LISTENERS.dup
|
9
|
-
ready = l.dup
|
1
|
+
class Unicorn::HttpServer
|
2
|
+
def worker_loop(worker)
|
3
|
+
ppid = master_pid
|
4
|
+
init_worker_process(worker)
|
5
|
+
nr = 0 # this becomes negative if we need to reopen logs
|
6
|
+
l = LISTENERS.dup
|
7
|
+
ready = l.dup
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
# closing anything we IO.select on will raise EBADF
|
10
|
+
trap(:USR1) { nr = -65536; SELF_PIPE[0].close rescue nil }
|
11
|
+
trap(:QUIT) { worker = nil; LISTENERS.each { |s| s.close rescue nil }.clear }
|
12
|
+
logger.info "worker=#{worker.nr} ready"
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
begin
|
15
|
+
nr < 0 and reopen_worker_logs(worker.nr)
|
16
|
+
nr = 0
|
17
|
+
worker.tick = Time.now.to_i
|
18
|
+
while sock = ready.shift
|
19
|
+
start_process = Time.now.to_i
|
20
|
+
if client = sock.kgio_tryaccept
|
21
|
+
process_client(client)
|
22
|
+
nr += 1
|
23
|
+
elapsed = (Time.now.to_i - start_process) * 1000
|
24
|
+
$stdout.puts("measure=unicorn.process val=#{elapsed}")
|
19
25
|
worker.tick = Time.now.to_i
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
process_client(client)
|
24
|
-
nr += 1
|
25
|
-
elapsed = (Time.now.to_i - start_process) * 1000
|
26
|
-
$stdout.puts("measure=unicorn.process val=#{elapsed}")
|
27
|
-
worker.tick = Time.now.to_i
|
28
|
-
end
|
29
|
-
break if nr < 0
|
30
|
-
end
|
26
|
+
end
|
27
|
+
break if nr < 0
|
28
|
+
end
|
31
29
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
30
|
+
# make the following bet: if we accepted clients this round,
|
31
|
+
# we're probably reasonably busy, so avoid calling select()
|
32
|
+
# and do a speculative non-blocking accept() on ready listeners
|
33
|
+
# before we sleep again in select().
|
34
|
+
unless nr == 0 # (nr < 0) => reopen logs (unlikely)
|
35
|
+
ready = l.dup
|
36
|
+
redo
|
37
|
+
end
|
40
38
|
|
41
|
-
|
39
|
+
ppid == Process.ppid or return
|
42
40
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
41
|
+
# timeout used so we can detect parent death:
|
42
|
+
worker.tick = Time.now.to_i
|
43
|
+
ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0]
|
44
|
+
rescue => e
|
45
|
+
redo if nr < 0 && (Errno::EBADF === e || IOError === e) # reopen logs
|
46
|
+
Unicorn.log_error(@logger, "listen loop error", e) if worker
|
47
|
+
end while worker
|
52
48
|
end
|
53
49
|
end
|