8000 Make the worker pool an optional feature that defaults to off. · socketry/async@41a0c92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41a0c92

Browse files
committed
Make the worker pool an optional feature that d 8000 efaults to off.
1 parent 9449e6f commit 41a0c92

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numb 8000 erDiff line change
@@ -0,0 +1,38 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
permissions:
6+
contents: read
7+
8+
env:
9+
CONSOLE_OUTPUT: XTerm
10+
ASYNC_SCHEDULER_DEFAULT_WORKER_POOL: true
11+
12+
jobs:
13+
test:
14+
name: ${{matrix.ruby}} on ${{matrix.os}} / ASYNC_SCHEDULER_DEFAULT_WORKER_POOL=true
15+
runs-on: ${{matrix.os}}-latest
16+
17+
strategy:
18+
matrix:
19+
os:
20+
- ubuntu
21+
22+
ruby:
23+
- head
24+
25+
steps:
26+
- uses: actions/checkout@v3
27+
- uses: ruby/setup-ruby@v1
28+
with:
29+
ruby-version: ${{matrix.ruby}}
30+
bundler-cache: true
31+
32+
- name: Run tests
33+
timeout-minutes: 10
34+
run: bundle exec bake test
35+
36+
- name: Run external tests
37+
timeout-minutes: 10
38+
run: bundle exec bake test:external

lib/async/scheduler.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
module Async
1818
# Handles scheduling of fibers. Implements the fiber scheduler interface.
1919
class Scheduler < Node
20+
DEFAULT_WORKER_POOL = ENV.fetch("ASYNC_SCHEDULER_DEFAULT_WORKER_POOL", nil).then do |value|
21+
value == "true" ? true : nil
22+
end
23+
2024
# Raised when an operation is attempted on a closed scheduler.
2125
class ClosedError < RuntimeError
2226
# Create a new error.
@@ -38,7 +42,7 @@ def self.supported?
3842
# @public Since *Async v1*.
3943
# @parameter parent [Node | Nil] The parent node to use for task hierarchy.
4044
# @parameter selector [IO::Event::Selector] The selector to use for event handling.
41-
def initialize(parent = nil, selector: nil)
45+
def initialize(parent = nil, selector: nil, worker_pool: DEFAULT_WORKER_POOL)
4246
super(parent)
4347

4448
@selector = selector || ::IO::Event::Selector.new(Fiber.current)
@@ -50,7 +54,15 @@ def initialize(parent = nil, selector: nil)
5054
@idle_time = 0.0
5155

5256
@timers = ::IO::Event::Timers.new
53-
@worker_pool = WorkerPool.new
57+
if worker_pool == true
58+
@worker_pool = WorkerPool.new
59+
else
60+
@worker_pool = worker_pool
61+
end
62+
63+
if @worker_pool
64+
self.singleton_class.prepend(WorkerPool::BlockingOperationWait)
65+
end
5466
end
5567

5668
# Compute the scheduler load according to the busy and idle times that are updated by the run loop.
@@ -348,17 +360,6 @@ def process_wait(pid, flags)
348360
return @selector.process_wait(Fiber.current, pid, flags)
349361
end
350362

351-
# Wait for the given work to be executed.
352-
#
353-
# @public Since *Async v2.19* and *Ruby v3.4*.
354-
# @asynchronous May be non-blocking.
355-
#
356-
# @parameter work [Proc] The work to execute on a background thread.
357-
# @returns [Object] The result of the work.
358-
def blocking_operation_wait(work)
359-
@worker_pool.call(work)
360-
end
361-
362363
# Run one iteration of the event loop.
363364
#
364365
# When terminating the event loop, we already know we are finished. So we don't need to check the task tree. This is a logical requirement because `run_once` ignores transient tasks. For example, a single top level transient task is not enough to keep the reactor running, but during termination we must still process it in order to terminate child tasks.

lib/async/worker_pool.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ module Async
1010
#
1111
# @private
1212
class WorkerPool
13+
module BlockingOperationWait
14+
# Wait for the given work to be executed.
15+
#
16+
# @public Since *Async v2.19* and *Ruby v3.4*.
17+
# @asynchronous May be non-blocking.
18+
#
19+
# @parameter work [Proc] The work to execute on a background thread.
20+
# @returns [Object] The result of the work.
21+
def blocking_operation_wait(work)
22+
@worker_pool.call(work)
23+
end
24+
end
25+
1326
class Promise
1427
def initialize(work)
1528
@work = work

0 commit comments

Comments
 (0)
0