@@ -20,9 +20,12 @@ class RubyThreadPoolExecutor < RubyExecutorService
20
20
# @!macro thread_pool_executor_constant_default_max_queue_size
21
21
DEFAULT_MAX_QUEUE_SIZE = 0
22
22
23
- # @!macro thread_pool_executor_constant_default_thread_timeout
23
+ # @!macro thread_pool_executor_constant_default_thread_idle_timeout
24
24
DEFAULT_THREAD_IDLETIMEOUT = 60
25
25
26
+ # @!macro thread_pool_executor_constant_default_pool_prune_timeout
27
+ DEFAULT_POOL_PRUNETIMEOUT = 30
28
+
26
29
# @!macro thread_pool_executor_constant_default_synchronous
27
30
DEFAULT_SYNCHRONOUS = false
28
31
@@ -149,6 +152,8 @@ def ns_initialize(opts)
149
152
150
153
@gc_interval = opts . fetch ( :gc_interval , @idletime / 2.0 ) . to_i # undocumented
151
154
@next_gc_time = Concurrent . monotonic_time + @gc_interval
155
+
156
+ ns_set_pruner
152
157
end
153
158
154
159
# @!visibility private
@@ -166,7 +171,6 @@ def ns_execute(*args, &task)
166
171
return fallback_action ( *args , &task )
167
172
end
168
173
169
- ns_prune_pool if @next_gc_time < Concurrent . monotonic_time
170
174
nil
171
175
end
172
176
@@ -183,6 +187,8 @@ def ns_shutdown_execution
183
187
# no more tasks will be accepted, just stop all workers
184
188
@pool . each ( &:stop )
185
189
end
190
+
191
+ ns_pruner &.kill
186
192
end
187
193
188
194
# @!visibility private
@@ -218,7 +224,7 @@ def ns_assign_worker(*args, &task)
218
224
# @!visibility private
219
225
def ns_enqueue ( *args , &task )
220
226
return false if @synchronous
221
-
227
+
222
228
if !ns_limited_queue? || @queue . size < @max_queue
223
229
@queue << [ task , args ]
224
230
true
@@ -303,8 +309,24 @@ def ns_reset_if_forked
303
309
@largest_length = 0
304
310
@workers_counter = 0
305
311
@ruby_pid = $$
312
+
313
+ ns_set_pruner
314
+ end
315
+ end
316
+
317
+ def ns_pruner
318
+ return if @min_length == @max_length
319
+
320
+ return @pruner if @pruner && @pruner . alive?
321
+
322
+ @pruner = Thread . new do
323
+ until stopped_event &.set?
324
+ sleep DEFAULT_POOL_PRUNETIMEOUT
325
+ ns_prune_pool
326
+ end
306
327
end
307
328
end
329
+ alias_method :ns_set_pruner , :ns_pruner
308
330
309
331
# @!visibility private
310
332
class Worker
0 commit comments