pitchfork 0.18.2 → 0.18.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/actions.lock +40 -0
- data/.github/workflows/ci.yml +31 -12
- data/.github/workflows/release.yml +3 -3
- data/.ruby-version +1 -1
- data/CHANGELOG.md +16 -1
- data/Gemfile +1 -0
- data/Rakefile +15 -0
- data/benchmark/request_benchmark.rb +86 -0
- data/docs/CONFIGURATION.md +51 -14
- data/docs/MIGRATING_FROM_UNICORN.md +6 -0
- data/docs/SIGNALS.md +6 -0
- data/examples/pitchfork.conf.reload.rb +41 -0
- data/exe/pitchfork +3 -2
- data/ext/pitchfork_http/c_util.h +3 -8
- data/ext/pitchfork_http/child_subreaper.h +7 -1
- data/ext/pitchfork_http/epollexclusive.h +6 -3
- data/ext/pitchfork_http/extconf.rb +1 -0
- data/ext/pitchfork_http/global_variables.h +2 -0
- data/ext/pitchfork_http/memory_page.c +188 -25
- data/ext/pitchfork_http/pitchfork_http.c +1319 -1291
- data/ext/pitchfork_http/pitchfork_http.rl +59 -29
- data/ext/pitchfork_http/pitchfork_http_common.rl +1 -1
- data/lib/pitchfork/children.rb +74 -20
- data/lib/pitchfork/chunked.rb +9 -0
- data/lib/pitchfork/configurator.rb +25 -6
- data/lib/pitchfork/http_response.rb +35 -12
- data/lib/pitchfork/http_server.rb +384 -189
- data/lib/pitchfork/info.rb +8 -0
- data/lib/pitchfork/listeners.rb +52 -0
- data/lib/pitchfork/message.rb +58 -8
- data/lib/pitchfork/shared_memory.rb +64 -13
- data/lib/pitchfork/socket_helper.rb +6 -2
- data/lib/pitchfork/stream_input.rb +12 -9
- data/lib/pitchfork/version.rb +1 -1
- data/lib/pitchfork/worker.rb +124 -64
- data/lib/pitchfork.rb +13 -1
- metadata +5 -2
data/lib/pitchfork/info.rb
CHANGED
|
@@ -106,6 +106,14 @@ module Pitchfork
|
|
|
106
106
|
end
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
+
def idle_workers_count
|
|
110
|
+
now = Pitchfork.time_now(true)
|
|
111
|
+
(0...workers_count).count do |nr|
|
|
112
|
+
state = SharedMemory.worker_state(nr)
|
|
113
|
+
!state.busy? && state.ready? && state.deadline > now
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
109
117
|
# Returns true if the server is shutting down.
|
|
110
118
|
# This can be useful to implement health check endpoints, so they
|
|
111
119
|
# can fail immediately after TERM/QUIT/INT was received by the monitor
|
data/lib/pitchfork/listeners.rb
CHANGED
|
@@ -3,12 +3,44 @@
|
|
|
3
3
|
module Pitchfork
|
|
4
4
|
|
|
5
5
|
class Listeners
|
|
6
|
+
class << self
|
|
7
|
+
def load(data)
|
|
8
|
+
data.map { |l| load_io(l) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def dump_io(io)
|
|
12
|
+
{
|
|
13
|
+
class: io.class,
|
|
14
|
+
fd: io.fileno,
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def load_io(data)
|
|
19
|
+
data[:class].for_fd(data[:fd])
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
6
23
|
class Group
|
|
24
|
+
class << self
|
|
25
|
+
def load(data)
|
|
26
|
+
allocate.init_with(data)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
7
30
|
def initialize(listeners, queues_per_worker:)
|
|
8
31
|
@listeners = listeners
|
|
9
32
|
@queues_per_worker = queues_per_worker
|
|
10
33
|
end
|
|
11
34
|
|
|
35
|
+
def init_with(data)
|
|
36
|
+
@listeners = data.fetch(:listerners).map { |l| Listeners.load_io(l) }
|
|
37
|
+
@queues_per_worker = data.fetch(:queues_per_worker)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def close_on_exec=(close_on_exec)
|
|
41
|
+
@listeners.each { |l| l.close_on_exec = close_on_exec }
|
|
42
|
+
end
|
|
43
|
+
|
|
12
44
|
def each(&block)
|
|
13
45
|
@listeners.each(&block)
|
|
14
46
|
end
|
|
@@ -27,6 +59,26 @@ module Pitchfork
|
|
|
27
59
|
@listeners = listeners
|
|
28
60
|
end
|
|
29
61
|
|
|
62
|
+
def close_on_exec=(close_on_exec)
|
|
63
|
+
@listeners.each { |l| l.close_on_exec = close_on_exec }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def init_with(listeners)
|
|
67
|
+
@listeners = listeners.map do |data|
|
|
68
|
+
if data[:class] == Group
|
|
69
|
+
Group.load(data)
|
|
70
|
+
else
|
|
71
|
+
Listeners.load_io(data)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def dump
|
|
77
|
+
map do |listener|
|
|
78
|
+
Listeners.dump_io(listener)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
30
82
|
def for_worker(nr)
|
|
31
83
|
ios = []
|
|
32
84
|
@listeners.each do |listener|
|
data/lib/pitchfork/message.rb
CHANGED
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
# :stopdoc:
|
|
5
5
|
module Pitchfork
|
|
6
6
|
class MessageSocket
|
|
7
|
+
class << self
|
|
8
|
+
def for_fd(fd)
|
|
9
|
+
new(UNIXSocket.for_fd(fd))
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
7
13
|
unless respond_to?(:ruby2_keywords, true)
|
|
8
14
|
class << self
|
|
9
15
|
def ruby2_keywords(*args)
|
|
@@ -28,6 +34,18 @@ module Pitchfork
|
|
|
28
34
|
end
|
|
29
35
|
ruby2_keywords :wait
|
|
30
36
|
|
|
37
|
+
def fileno
|
|
38
|
+
@socket.fileno
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def close_on_exec?
|
|
42
|
+
@socket.close_on_exec?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def close_on_exec=(close_on_exec)
|
|
46
|
+
@socket.close_on_exec = close_on_exec
|
|
47
|
+
end
|
|
48
|
+
|
|
31
49
|
def close_read
|
|
32
50
|
@socket.close_read
|
|
33
51
|
end
|
|
@@ -70,6 +88,20 @@ module Pitchfork
|
|
|
70
88
|
end
|
|
71
89
|
end
|
|
72
90
|
|
|
91
|
+
def try_send_message(message)
|
|
92
|
+
success = false
|
|
93
|
+
begin
|
|
94
|
+
case sendmsg_nonblock(message, exception: false)
|
|
95
|
+
when :wait_writable
|
|
96
|
+
else
|
|
97
|
+
success = true
|
|
98
|
+
end
|
|
99
|
+
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ENOTCONN
|
|
100
|
+
# worker will be reaped soon
|
|
101
|
+
end
|
|
102
|
+
success
|
|
103
|
+
end
|
|
104
|
+
|
|
73
105
|
private
|
|
74
106
|
|
|
75
107
|
MARSHAL_PREFIX = (Marshal::MAJOR_VERSION.chr << Marshal::MINOR_VERSION.chr).freeze
|
|
@@ -122,18 +154,36 @@ module Pitchfork
|
|
|
122
154
|
|
|
123
155
|
Message = Class.new(Struct)
|
|
124
156
|
class Message
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
157
|
+
class << self
|
|
158
|
+
def new(*members)
|
|
159
|
+
if members.empty?
|
|
160
|
+
super(:_) # Struct.new requires at least 1 member on Ruby < 3.3
|
|
161
|
+
else
|
|
162
|
+
klass = super
|
|
163
|
+
location = caller_locations(1, 1).first
|
|
164
|
+
klass.class_eval(<<~RUBY, location.path, location.lineno)
|
|
165
|
+
def initialize(#{members.join(", ")})
|
|
166
|
+
super
|
|
167
|
+
end
|
|
168
|
+
RUBY
|
|
169
|
+
klass
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
SpawnWorkers = new(:nrs)
|
|
175
|
+
WorkerSpawned = new(:nr, :pid, :generation, :version, :pipe)
|
|
176
|
+
WorkerReady = new(:nr, :pid)
|
|
128
177
|
PromoteWorker = new(:generation)
|
|
129
178
|
|
|
130
|
-
MoldSpawned = new(:nr, :pid, :generation, :pipe)
|
|
131
|
-
MoldReady = new(:
|
|
179
|
+
MoldSpawned = new(:nr, :pid, :generation, :version, :pipe)
|
|
180
|
+
MoldReady = new(:pid)
|
|
132
181
|
|
|
133
|
-
SpawnService = new
|
|
134
|
-
ServiceSpawned = new(:pid, :generation, :pipe)
|
|
135
|
-
ServiceReady = new(:pid
|
|
182
|
+
SpawnService = new
|
|
183
|
+
ServiceSpawned = new(:pid, :generation, :version, :pipe)
|
|
184
|
+
ServiceReady = new(:pid)
|
|
136
185
|
|
|
137
186
|
SoftKill = new(:signum)
|
|
187
|
+
ProcessReaped = new(:status)
|
|
138
188
|
end
|
|
139
189
|
end
|
|
@@ -5,13 +5,40 @@ module Pitchfork
|
|
|
5
5
|
extend self
|
|
6
6
|
|
|
7
7
|
CURRENT_GENERATION_OFFSET = 0
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
CURRENT_VERSION_OFFSET = 1
|
|
9
|
+
SHUTDOWN_OFFSET = 2
|
|
10
|
+
MOLD_TICK_OFFSET = 3
|
|
11
|
+
MOLD_PROMOTION_TICK_OFFSET = 4
|
|
12
|
+
SERVICE_TICK_OFFSET = 5
|
|
13
|
+
WORKER_TICK_OFFSET = 6
|
|
13
14
|
|
|
14
|
-
PAGES = [
|
|
15
|
+
PAGES = []
|
|
16
|
+
|
|
17
|
+
def fds
|
|
18
|
+
PAGES.map(&:fileno)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def reopen(fds)
|
|
22
|
+
fds.each do |fd|
|
|
23
|
+
page = MemoryPage.for_fd(fd)
|
|
24
|
+
page.close_on_exec = true
|
|
25
|
+
PAGES << page
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def close_on_exec=(close_on_exec)
|
|
30
|
+
PAGES.each { |p| p.close_on_exec = close_on_exec }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def close_on_exec=(close_on_exec)
|
|
34
|
+
PAGES.each { |p| p.close_on_exec = close_on_exec }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
READY_FLAG = 1 << 0
|
|
38
|
+
BUSY_FLAG = 1 << 1
|
|
39
|
+
DEADLINE_SHIFT = 2
|
|
40
|
+
|
|
41
|
+
FLAGS_MASK = 0x3
|
|
15
42
|
|
|
16
43
|
def current_generation
|
|
17
44
|
PAGES[0][CURRENT_GENERATION_OFFSET]
|
|
@@ -21,6 +48,14 @@ module Pitchfork
|
|
|
21
48
|
PAGES[0][CURRENT_GENERATION_OFFSET] = value
|
|
22
49
|
end
|
|
23
50
|
|
|
51
|
+
def current_version
|
|
52
|
+
PAGES[0][CURRENT_VERSION_OFFSET]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def current_version=(value)
|
|
56
|
+
PAGES[0][CURRENT_VERSION_OFFSET] = value
|
|
57
|
+
end
|
|
58
|
+
|
|
24
59
|
def shutting_down!
|
|
25
60
|
PAGES[0][SHUTDOWN_OFFSET] = 1
|
|
26
61
|
end
|
|
@@ -50,24 +85,40 @@ module Pitchfork
|
|
|
50
85
|
end
|
|
51
86
|
|
|
52
87
|
def ready?
|
|
53
|
-
(@field.value &
|
|
88
|
+
(@field.value & READY_FLAG) == READY_FLAG
|
|
54
89
|
end
|
|
55
90
|
|
|
56
91
|
def ready=(bool)
|
|
57
92
|
if bool
|
|
58
|
-
@field.value |=
|
|
93
|
+
@field.value |= READY_FLAG
|
|
94
|
+
else
|
|
95
|
+
@field.value &= ~READY_FLAG
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def busy?
|
|
100
|
+
(@field.value & BUSY_FLAG) == BUSY_FLAG
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def busy=(bool)
|
|
104
|
+
if bool
|
|
105
|
+
@field.value |= BUSY_FLAG
|
|
59
106
|
else
|
|
60
|
-
@field.value &= ~
|
|
107
|
+
@field.value &= ~BUSY_FLAG
|
|
61
108
|
end
|
|
62
109
|
end
|
|
63
110
|
|
|
64
|
-
def deadline=(
|
|
111
|
+
def deadline=(deadline)
|
|
65
112
|
# Shift the value up and preserve the current ready bit.
|
|
66
|
-
@field.value = (
|
|
113
|
+
@field.value = (deadline << DEADLINE_SHIFT) | (@field.value & FLAGS_MASK)
|
|
67
114
|
end
|
|
68
115
|
|
|
69
116
|
def deadline
|
|
70
|
-
@field.value >>
|
|
117
|
+
@field.value >> DEADLINE_SHIFT
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def reset!
|
|
121
|
+
@field.value = 0
|
|
71
122
|
end
|
|
72
123
|
end
|
|
73
124
|
|
|
@@ -97,7 +148,7 @@ module Pitchfork
|
|
|
97
148
|
# However this doesn't account for TTIN signals that increase the
|
|
98
149
|
# number of workers, but we should probably remove that feature too.
|
|
99
150
|
def preallocate_pages(workers_count)
|
|
100
|
-
|
|
151
|
+
((WORKER_TICK_OFFSET + workers_count) / MemoryPage::SLOTS.to_f).ceil.times do |i|
|
|
101
152
|
PAGES[i] ||= MemoryPage.new(MemoryPage::SLOTS)
|
|
102
153
|
end
|
|
103
154
|
end
|
|
@@ -113,10 +113,14 @@ module Pitchfork
|
|
|
113
113
|
# creates a new server, socket. address may be a HOST:PORT or
|
|
114
114
|
# an absolute path to a UNIX socket. address can even be a Socket
|
|
115
115
|
# object in which case it is immediately returned
|
|
116
|
-
def bind_listen(address = '0.0.0.0:8080', opt = {})
|
|
116
|
+
def bind_listen(address = '0.0.0.0:8080', opt = {}, inherited_listeners = nil)
|
|
117
117
|
return address unless String === address
|
|
118
118
|
|
|
119
|
-
sock = if
|
|
119
|
+
sock = if inherited_listeners
|
|
120
|
+
inherited_listeners[address]&.shift
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
sock ||= if address.start_with?('/')
|
|
120
124
|
if File.exist?(address)
|
|
121
125
|
if File.socket?(address)
|
|
122
126
|
begin
|
|
@@ -50,17 +50,20 @@ module Pitchfork
|
|
|
50
50
|
else
|
|
51
51
|
to_read = length - @rbuf.size
|
|
52
52
|
rv.replace(@rbuf.slice!(0, @rbuf.size))
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
begin
|
|
54
|
+
until to_read == 0 || eof? || (rv.size > 0 && @chunked)
|
|
55
|
+
begin
|
|
56
|
+
@socket.readpartial(to_read, @buf)
|
|
57
|
+
rescue EOFError
|
|
58
|
+
eof!
|
|
59
|
+
end
|
|
60
|
+
filter_body(@rbuf, @buf)
|
|
61
|
+
rv << @rbuf
|
|
62
|
+
to_read -= @rbuf.size
|
|
58
63
|
end
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
to_read -= @rbuf.size
|
|
64
|
+
ensure
|
|
65
|
+
@rbuf.clear
|
|
62
66
|
end
|
|
63
|
-
@rbuf.clear
|
|
64
67
|
end
|
|
65
68
|
rv = nil if rv.empty? && length != 0
|
|
66
69
|
else
|
data/lib/pitchfork/version.rb
CHANGED
data/lib/pitchfork/worker.rb
CHANGED
|
@@ -13,20 +13,80 @@ module Pitchfork
|
|
|
13
13
|
class Worker
|
|
14
14
|
# :stopdoc:
|
|
15
15
|
EXIT_SIGNALS = [:QUIT, :TERM]
|
|
16
|
-
attr_accessor :nr, :pid, :generation
|
|
16
|
+
attr_accessor :nr, :pid, :generation, :version
|
|
17
17
|
attr_reader :monitor, :requests_count
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
class << self
|
|
20
|
+
def load(data)
|
|
21
|
+
allocate.init_with(data)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(nr, pid: nil, generation: 0, version: 0, service: false)
|
|
20
26
|
@nr = nr
|
|
21
27
|
@pid = pid
|
|
22
28
|
@generation = generation
|
|
29
|
+
@version = version
|
|
23
30
|
@mold = false
|
|
24
|
-
@
|
|
31
|
+
@mold_ready = false
|
|
32
|
+
@service = service
|
|
33
|
+
@to_io = nil
|
|
34
|
+
@monitor = nil
|
|
25
35
|
@exiting = false
|
|
26
36
|
@requests_count = 0
|
|
27
37
|
init_state
|
|
28
38
|
end
|
|
29
39
|
|
|
40
|
+
def init_with(data)
|
|
41
|
+
@nr = data.fetch(:nr)
|
|
42
|
+
@pid = data.fetch(:pid)
|
|
43
|
+
@generation = data.fetch(:generation)
|
|
44
|
+
@version = data.fetch(:version)
|
|
45
|
+
@mold = data.fetch(:mold)
|
|
46
|
+
@mold_ready = data.fetch(:mold_ready)
|
|
47
|
+
@service = data.fetch(:service)
|
|
48
|
+
@to_io = nil
|
|
49
|
+
fd = data.fetch(:monitor_fd)
|
|
50
|
+
@monitor = fd ? MessageSocket.for_fd(fd) : nil
|
|
51
|
+
@exiting = data.fetch(:exiting)
|
|
52
|
+
@requests_count = data.fetch(:requests_count)
|
|
53
|
+
|
|
54
|
+
@state_drop = if @nr
|
|
55
|
+
SharedMemory.worker_state(@nr)
|
|
56
|
+
elsif mold?
|
|
57
|
+
if data.fetch(:mold_ready)
|
|
58
|
+
SharedMemory.mold_state
|
|
59
|
+
else
|
|
60
|
+
SharedMemory.mold_promotion_state
|
|
61
|
+
end
|
|
62
|
+
elsif service?
|
|
63
|
+
SharedMemory.service_state
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
self
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def dump
|
|
70
|
+
{
|
|
71
|
+
nr: nr,
|
|
72
|
+
pid: pid,
|
|
73
|
+
generation: generation,
|
|
74
|
+
version: version,
|
|
75
|
+
mold: mold?,
|
|
76
|
+
mold_ready: @mold_ready,
|
|
77
|
+
service: service?,
|
|
78
|
+
monitor_fd: @monitor&.fileno,
|
|
79
|
+
exiting: exiting?,
|
|
80
|
+
requests_count: requests_count,
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def close_on_exec=(close_on_exec)
|
|
85
|
+
if @monitor
|
|
86
|
+
@monitor.close_on_exec = close_on_exec
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
30
90
|
def exiting?
|
|
31
91
|
@exiting
|
|
32
92
|
end
|
|
@@ -36,7 +96,7 @@ module Pitchfork
|
|
|
36
96
|
end
|
|
37
97
|
|
|
38
98
|
def outdated?
|
|
39
|
-
SharedMemory.current_generation
|
|
99
|
+
SharedMemory.current_generation != @generation || SharedMemory.current_version != @version
|
|
40
100
|
end
|
|
41
101
|
|
|
42
102
|
def update(message)
|
|
@@ -48,27 +108,35 @@ module Pitchfork
|
|
|
48
108
|
when Message::MoldSpawned
|
|
49
109
|
@state_drop = SharedMemory.mold_promotion_state
|
|
50
110
|
when Message::MoldReady
|
|
111
|
+
@mold_ready = true
|
|
51
112
|
@state_drop = SharedMemory.mold_state
|
|
52
113
|
end
|
|
53
114
|
end
|
|
54
115
|
|
|
55
116
|
def register_to_monitor(control_socket)
|
|
56
117
|
create_socketpair!
|
|
57
|
-
|
|
118
|
+
|
|
119
|
+
message = if service?
|
|
120
|
+
Message::ServiceSpawned.new(@pid, generation, version, @monitor)
|
|
121
|
+
else
|
|
122
|
+
Message::WorkerSpawned.new(@nr, @pid, generation, version, @monitor)
|
|
123
|
+
end
|
|
124
|
+
|
|
58
125
|
control_socket.sendmsg(message)
|
|
59
126
|
@monitor.close
|
|
127
|
+
@monitor = nil
|
|
60
128
|
end
|
|
61
129
|
|
|
62
130
|
def start_promotion(control_socket)
|
|
63
131
|
create_socketpair!
|
|
64
|
-
message = Message::MoldSpawned.new(@nr, @pid, generation, @monitor)
|
|
132
|
+
message = Message::MoldSpawned.new(@nr, @pid, generation, version, @monitor)
|
|
65
133
|
control_socket.sendmsg(message)
|
|
66
134
|
@monitor.close
|
|
135
|
+
@monitor = nil
|
|
67
136
|
end
|
|
68
137
|
|
|
69
138
|
def finish_promotion(control_socket)
|
|
70
|
-
|
|
71
|
-
message = Message::MoldReady.new(@nr, @pid, generation)
|
|
139
|
+
message = Message::MoldReady.new(@pid)
|
|
72
140
|
control_socket.sendmsg(message)
|
|
73
141
|
@state_drop = SharedMemory.mold_state
|
|
74
142
|
end
|
|
@@ -76,9 +144,9 @@ module Pitchfork
|
|
|
76
144
|
def notify_ready(control_socket)
|
|
77
145
|
self.ready = true
|
|
78
146
|
message = if worker?
|
|
79
|
-
Message::WorkerReady.new(@nr, @pid
|
|
147
|
+
Message::WorkerReady.new(@nr, @pid)
|
|
80
148
|
elsif service?
|
|
81
|
-
Message::ServiceReady.new(@pid
|
|
149
|
+
Message::ServiceReady.new(@pid)
|
|
82
150
|
else
|
|
83
151
|
raise "Unexpected child type"
|
|
84
152
|
end
|
|
@@ -90,8 +158,8 @@ module Pitchfork
|
|
|
90
158
|
send_message_nonblock(Message::PromoteWorker.new(generation))
|
|
91
159
|
end
|
|
92
160
|
|
|
93
|
-
def
|
|
94
|
-
send_message_nonblock(Message::
|
|
161
|
+
def spawn_workers(new_workers)
|
|
162
|
+
send_message_nonblock(Message::SpawnWorkers.new(new_workers.map(&:nr)))
|
|
95
163
|
end
|
|
96
164
|
|
|
97
165
|
def spawn_service(_new_service)
|
|
@@ -115,8 +183,12 @@ module Pitchfork
|
|
|
115
183
|
@mold
|
|
116
184
|
end
|
|
117
185
|
|
|
186
|
+
def ready_mold?
|
|
187
|
+
mold? && @mold_ready
|
|
188
|
+
end
|
|
189
|
+
|
|
118
190
|
def service?
|
|
119
|
-
|
|
191
|
+
@service
|
|
120
192
|
end
|
|
121
193
|
|
|
122
194
|
def worker?
|
|
@@ -196,15 +268,20 @@ module Pitchfork
|
|
|
196
268
|
@state_drop.ready = bool
|
|
197
269
|
end
|
|
198
270
|
|
|
271
|
+
def busy?
|
|
272
|
+
@state_drop.busy?
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def busy=(bool)
|
|
276
|
+
@state_drop.busy = bool
|
|
277
|
+
end
|
|
278
|
+
|
|
199
279
|
def update_deadline(timeout)
|
|
200
|
-
self.deadline = Pitchfork.time_now
|
|
280
|
+
self.deadline = Integer(Pitchfork.time_now + timeout)
|
|
201
281
|
end
|
|
202
282
|
|
|
203
283
|
# called in the worker process
|
|
204
284
|
def deadline=(value) # :nodoc:
|
|
205
|
-
# If we are (re)setting to zero mark worker as not ready.
|
|
206
|
-
self.ready = false if value == 0
|
|
207
|
-
|
|
208
285
|
@state_drop.deadline = value
|
|
209
286
|
end
|
|
210
287
|
|
|
@@ -223,9 +300,22 @@ module Pitchfork
|
|
|
223
300
|
|
|
224
301
|
# called in both the monitor (reaping worker) and worker (SIGQUIT handler)
|
|
225
302
|
def close # :nodoc:
|
|
226
|
-
|
|
227
|
-
@monitor
|
|
228
|
-
@to_io
|
|
303
|
+
@monitor&.close
|
|
304
|
+
@monitor = nil
|
|
305
|
+
@to_io&.close
|
|
306
|
+
@to_io = nil
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def reaped!
|
|
310
|
+
close
|
|
311
|
+
# Not ideal: All molds share two state_drops, so when reaping a mold
|
|
312
|
+
# we can't reset the state as it might have been adopted by another mold.
|
|
313
|
+
# Now that our shared memory pages are backed by a FD and can be sent across
|
|
314
|
+
# unix sockets, we could consider assigning a unique page for each mold to solve
|
|
315
|
+
# this problem.
|
|
316
|
+
unless mold?
|
|
317
|
+
@state_drop.reset!
|
|
318
|
+
end
|
|
229
319
|
end
|
|
230
320
|
|
|
231
321
|
def create_socketpair!
|
|
@@ -237,11 +327,17 @@ module Pitchfork
|
|
|
237
327
|
end
|
|
238
328
|
|
|
239
329
|
def to_log
|
|
240
|
-
if mold?
|
|
241
|
-
|
|
330
|
+
type = if mold?
|
|
331
|
+
"mold"
|
|
332
|
+
elsif service?
|
|
333
|
+
"service"
|
|
242
334
|
else
|
|
243
|
-
|
|
335
|
+
"worker=#{nr}"
|
|
244
336
|
end
|
|
337
|
+
|
|
338
|
+
pid_log = " pid=#{pid}" if pid
|
|
339
|
+
|
|
340
|
+
"#{type} gen=#{version}.#{generation}#{pid_log}"
|
|
245
341
|
end
|
|
246
342
|
|
|
247
343
|
private
|
|
@@ -249,7 +345,10 @@ module Pitchfork
|
|
|
249
345
|
def init_state
|
|
250
346
|
if nr
|
|
251
347
|
@state_drop = SharedMemory.worker_state(@nr)
|
|
252
|
-
|
|
348
|
+
@state_drop.reset!
|
|
349
|
+
elsif service?
|
|
350
|
+
@state_drop = SharedMemory.service_state
|
|
351
|
+
@state_drop.reset!
|
|
253
352
|
else
|
|
254
353
|
promoted!(nil)
|
|
255
354
|
end
|
|
@@ -262,46 +361,7 @@ module Pitchfork
|
|
|
262
361
|
end
|
|
263
362
|
|
|
264
363
|
def send_message_nonblock(message)
|
|
265
|
-
|
|
266
|
-
return false unless @monitor
|
|
267
|
-
begin
|
|
268
|
-
case @monitor.sendmsg_nonblock(message, exception: false)
|
|
269
|
-
when :wait_writable
|
|
270
|
-
else
|
|
271
|
-
success = true
|
|
272
|
-
end
|
|
273
|
-
rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ENOTCONN
|
|
274
|
-
# worker will be reaped soon
|
|
275
|
-
end
|
|
276
|
-
success
|
|
277
|
-
end
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
class Service < Worker
|
|
281
|
-
def initialize(pid: nil, generation: 0)
|
|
282
|
-
super(nil, pid: pid, generation: generation)
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
def service?
|
|
286
|
-
true
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
def register_to_monitor(control_socket)
|
|
290
|
-
create_socketpair!
|
|
291
|
-
message = Message::ServiceSpawned.new(@pid, generation, @monitor)
|
|
292
|
-
control_socket.sendmsg(message)
|
|
293
|
-
@monitor.close
|
|
294
|
-
end
|
|
295
|
-
|
|
296
|
-
def to_log
|
|
297
|
-
pid ? "service gen=#{generation} pid=#{pid}" : "service gen=#{generation}"
|
|
298
|
-
end
|
|
299
|
-
|
|
300
|
-
private
|
|
301
|
-
|
|
302
|
-
def init_state
|
|
303
|
-
@state_drop = SharedMemory.service_state
|
|
304
|
-
self.deadline = 0
|
|
364
|
+
@monitor&.try_send_message(message)
|
|
305
365
|
end
|
|
306
366
|
end
|
|
307
367
|
end
|
data/lib/pitchfork.rb
CHANGED
|
@@ -108,11 +108,16 @@ module Pitchfork
|
|
|
108
108
|
# and Unix domain socket paths. This is useful for use with
|
|
109
109
|
# Raindrops::Middleware under Linux: https://yhbt.net/raindrops/
|
|
110
110
|
def listener_names
|
|
111
|
-
Pitchfork::HttpServer::LISTENERS.map do |io|
|
|
111
|
+
@listener_names || Pitchfork::HttpServer::LISTENERS.map do |io|
|
|
112
112
|
Pitchfork::SocketHelper.sock_name(io)
|
|
113
113
|
end
|
|
114
114
|
end
|
|
115
115
|
|
|
116
|
+
def close_listeners
|
|
117
|
+
@listener_names = listener_names
|
|
118
|
+
Pitchfork::HttpServer::LISTENERS.each(&:close).clear
|
|
119
|
+
end
|
|
120
|
+
|
|
116
121
|
def log_error(logger, prefix, exc)
|
|
117
122
|
message = exc.message
|
|
118
123
|
message = message.dump if /[[:cntrl:]]/ =~ message
|
|
@@ -120,6 +125,13 @@ module Pitchfork
|
|
|
120
125
|
exc.backtrace.each { |line| logger.error(line) }
|
|
121
126
|
end
|
|
122
127
|
|
|
128
|
+
def socketpair_for_fds(fds)
|
|
129
|
+
# TODO: check if we need close_write/close_read
|
|
130
|
+
pair = fds.map { |fd| MessageSocket.for_fd(Integer(fd)) }
|
|
131
|
+
pair.each { |s| s.close_on_exec = true }
|
|
132
|
+
pair
|
|
133
|
+
end
|
|
134
|
+
|
|
123
135
|
def socketpair
|
|
124
136
|
pair = UNIXSocket.socketpair(@socket_type).map { |s| MessageSocket.new(s) }
|
|
125
137
|
pair[0].close_write
|