ractor-pool 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 20f4d6392022d30bb6c629bcf5fc7e1de3cbc586a6bd111dfd1771a3ceb47845
4
+ data.tar.gz: c653cd83e3b363471d671e2515bf6597f19bb27190f80a6c89571ae97adfbd19
5
+ SHA512:
6
+ metadata.gz: 97cdc940a685d8cf4522fa5b7e88ca64ba395eace72cbd84535d1b0021f03c1e8ac3823ac55b875390f7b5993832ee1b1aee706666a9f141edad3412c37a027f
7
+ data.tar.gz: e50b79953aa3521285c76e756c64e1342032ae3f299ca5f712f5785fec2ed3bb1189c9074267137978c1d9e35a2f2214c72fbc7bb809cc3ed1c75770024e5f98
@@ -0,0 +1,21 @@
1
+ steps:
2
+ - label: ":ruby: Ruby master-dev"
3
+ commands:
4
+ - apt-get update && apt-get install -y libyaml-dev
5
+ - bundle install
6
+ - bundle exec rake
7
+ plugins:
8
+ - docker#v5.13.0:
9
+ image: "rubylang/ruby:master-dev"
10
+
11
+ # - label: ":ruby: Ruby {{matrix.ruby}}"
12
+ # commands:
13
+ # - bundle install
14
+ # - bundle exec rake
15
+ # matrix:
16
+ # setup:
17
+ # ruby:
18
+ # - 3.5.0
19
+ # plugins:
20
+ # - docker#v5.13.0:
21
+ # image: "ruby:{{matrix.ruby}}"
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-10-22
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Joshua Young
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # RactorPool
2
+
3
+ ## Installation
4
+
5
+ Install the gem and add to the application's Gemfile by executing:
6
+
7
+ ```bash
8
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
9
+ ```
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ ```bash
14
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Calculating Fibonacci numbers in parallel:
20
+
21
+ ```ruby
22
+ fib_worker = lambda do |index|
23
+ first, second = 0, 1
24
+ index.times { first, second = second, first + second }
25
+ [index, first]
26
+ end
27
+ results = []
28
+ pool = RactorPool.new(size: 2, worker: fib_worker) { |result| results << result }
29
+
30
+ 25.times { |index| pool << index }
31
+ pool.shutdown
32
+
33
+ results.sort_by! { |index, _value| index }
34
+ results.each { |index, value| puts "fib(#{index}) = #{value}" }
35
+
36
+ #=> fib(0) = 0
37
+ #=> fib(1) = 1
38
+ #=> fib(2) = 1
39
+ #=> fib(3) = 2
40
+ #=> ...
41
+ #=> fib(21) = 10946
42
+ #=> fib(22) = 17711
43
+ #=> fib(23) = 28657
44
+ #=> fib(24) = 46368
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the
50
+ tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshuay03/ractor-pool. This project is
55
+ intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of
56
+ conduct](https://github.com/joshuay03/ractor-pool/blob/main/CODE_OF_CONDUCT.md).
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
61
+
62
+ ## Code of Conduct
63
+
64
+ Everyone interacting in the RactorPool project's codebases, issue trackers, chat rooms and mailing lists is expected to
65
+ follow the [code of conduct](https://github.com/joshuay03/ractor-pool/blob/main/CODE_OF_CONDUCT.md).
66
+
67
+ ## Acknowledgements
68
+
69
+ Thanks to [Gleb Sinyavskiy (zhulik)](https://github.com/zhulik) for graciously transferring ownership of the
70
+ `ractor_pool` gem name, enabling this gem to be published as `ractor-pool`.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ namespace :rbs do
9
+ task :generate do
10
+ puts
11
+ sh "rm -rf sig && rbs-inline --opt-out --output lib && echo"
12
+ end
13
+ end
14
+
15
+ task build: :"rbs:generate"
16
+ task default: %i[rbs:generate test]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RactorPool
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,266 @@
1
+ # rbs_inline: enabled
2
+ # frozen_string_literal: true
3
+
4
+ require "warning"
5
+ require "atomic-ruby/atom"
6
+
7
+ Warning.ignore(/Ractor is experimental/, __FILE__)
8
+
9
+ # A thread-safe, lock-free pool of Ractor workers with a coordinator pattern for distributing work.
10
+ #
11
+ # RactorPool manages a fixed number of worker ractors that process work items in parallel.
12
+ # Work is distributed on-demand to idle workers, ensuring efficient utilisation. Results
13
+ # are collected and passed to a result handler running in a separate thread.
14
+ #
15
+ # @example Basic usage
16
+ # results = []
17
+ # worker = -> (work) { work * 2 }
18
+ # pool = RactorPool.new(size: 4, worker: worker) { |result| results << result }
19
+ #
20
+ # 10.times { |index| pool << index }
21
+ # pool.shutdown
22
+ #
23
+ # p results #=> [2, 0, 6, 4, 14, 10, 8, 16, 18, 12]
24
+ #
25
+ # @example Without result handler
26
+ # counter = Atom.new(0)
27
+ # worker = lambda do |work|
28
+ # counter.swap { |current_value| current_value + 1 }
29
+ # work * 2
30
+ # end
31
+ # pool = RactorPool.new(size: 4, worker: worker)
32
+ #
33
+ # 10.times { |index| pool << index }
34
+ # pool.shutdown
35
+ #
36
+ # p counter.value #=> 10
37
+ #
38
+ # @see https://docs.ruby-lang.org/en/master/ractor_md.html Ractor Guide
39
+ # @see https://docs.ruby-lang.org/en/master/Ractor.html Ractor API
40
+ # @see https://docs.ruby-lang.org/en/master/Ractor/Port.html Ractor::Port API
41
+ # @see https://github.com/joshuay03/atomic-ruby atomic-ruby gem
42
+ #
43
+ class RactorPool
44
+ class Error < StandardError; end
45
+
46
+ class EnqueuedWorkAfterShutdownError < Error
47
+ # @rbs () -> String
48
+ def message = "cannot queue work after shutdown"
49
+ end
50
+
51
+ SHUTDOWN = :shutdown
52
+
53
+ # @rbs @size: Integer
54
+ # @rbs @worker: ^(untyped) -> untyped
55
+ # @rbs @name: String?
56
+ # @rbs @result_handler: (^(untyped) -> void | nil)
57
+ # @rbs @state: Atom[Hash[Symbol, bool | Integer]]
58
+ # @rbs @result_port: Ractor::Port?
59
+ # @rbs @coordinator: Ractor
60
+ # @rbs @workers: Array[Ractor]
61
+ # @rbs @collector: Thread?
62
+
63
+ # Creates a new RactorPool with the specified number of workers.
64
+ #
65
+ # @param size [Integer] number of worker ractors to create
66
+ # @param worker [Proc] a shareable lambda that processes each work item
67
+ # @param name [String, nil] optional name for the pool, used in thread/ractor names
68
+ # @yieldparam result [Object] the result returned by the worker lambda
69
+ # @return [void]
70
+ # @raise [ArgumentError] if size is not a positive integer
71
+ # @raise [ArgumentError] if worker is not a lambda
72
+ #
73
+ # @example With result handler
74
+ # pool = RactorPool.new(size: 4, worker: -> { it }) { |result| puts result }
75
+ #
76
+ # @example Without result handler
77
+ # pool = RactorPool.new(size: 4, worker: -> { it })
78
+ #
79
+ # @rbs (?size: Integer, worker: ^(untyped) -> untyped, ?name: String?) ?{ (untyped) -> void } -> void
80
+ def initialize(size: Etc.nprocessors, worker:, name: nil, &result_handler)
81
+ raise ArgumentError, "size must be a positive Integer" unless size.is_a?(Integer) && size > 0
82
+ raise ArgumentError, "worker must be a lambda Proc" unless worker.lambda?
83
+
84
+ @size = size
85
+ @worker = Ractor.shareable_lambda(&worker)
86
+ @name = name
87
+ @result_handler = result_handler
88
+
89
+ @state = Atom.new(in_flight: 0, shutdown: false)
90
+
91
+ @result_port = Ractor::Port.new if result_handler
92
+ @coordinator = start_coordinator if size > 1
93
+ @workers = start_workers
94
+ @collector = start_collector
95
+ end
96
+
97
+ # Queues a work item to be processed by an available worker.
98
+ #
99
+ # @param work [Object] the work item to process
100
+ # @return [void]
101
+ # @raise [EnqueuedWorkAfterShutdownError] if the pool has been shut down
102
+ #
103
+ # @example
104
+ # pool << "http://example.com/page1"
105
+ # pool << "http://example.com/page2"
106
+ #
107
+ # @rbs (untyped work) -> void
108
+ def <<(work)
109
+ state = @state.swap do |current_state|
110
+ if current_state[:shutdown]
111
+ current_state
112
+ else
113
+ current_state.merge(in_flight: current_state[:in_flight] + 1)
114
+ end
115
+ end
116
+ raise EnqueuedWorkAfterShutdownError if state[:shutdown]
117
+
118
+ begin
119
+ (@coordinator || @workers.first).send(work, move: true)
120
+ ensure
121
+ @state.swap do |current_state|
122
+ current_state.merge(in_flight: current_state[:in_flight] - 1)
123
+ end
124
+ end
125
+ end
126
+
127
+ # Shuts down the pool gracefully.
128
+ #
129
+ # This method:
130
+ # 1. Prevents new work from being queued
131
+ # 2. Waits for all in-flight work submissions to complete
132
+ # 3. Allows all queued work to complete
133
+ # 4. Waits for all workers to finish
134
+ # 5. Waits for all results to be processed
135
+ #
136
+ # This method is idempotent and can be called multiple times safely.
137
+ #
138
+ # @return [void]
139
+ #
140
+ # @example
141
+ # pool.shutdown
142
+ #
143
+ # @rbs () -> void
144
+ def shutdown
145
+ already_shutdown = false
146
+ @state.swap do |current_state|
147
+ if current_state[:shutdown]
148
+ already_shutdown = true
149
+ current_state
150
+ else
151
+ current_state.merge(shutdown: true)
152
+ end
153
+ end
154
+ return if already_shutdown
155
+
156
+ Thread.pass until @state.value[:in_flight] == 0
157
+
158
+ @coordinator&.send(SHUTDOWN, move: true) ||
159
+ (@workers.first.send(SHUTDOWN, move: true) && @result_port&.send(SHUTDOWN, move: true))
160
+ @workers.each(&:join)
161
+ @coordinator&.join
162
+ @collector&.join
163
+ end
164
+
165
+ private
166
+
167
+ # @rbs () -> Ractor
168
+ def start_coordinator
169
+ ractor_name = String.new("#{self.class.name} coordinator ractor")
170
+ ractor_name << " for #{@name}" if @name
171
+
172
+ Ractor.new(@size, @result_port, name: ractor_name) do |worker_count, result_port|
173
+ work_queue = []
174
+ waiting_workers = []
175
+ shutdown_received = false
176
+ workers_finished = 0
177
+
178
+ loop do
179
+ case data = Ractor.receive
180
+ when SHUTDOWN
181
+ shutdown_received = true
182
+
183
+ workers_finished += waiting_workers.size
184
+ waiting_workers.each { |worker| worker.send(SHUTDOWN, move: true) }
185
+ waiting_workers.clear
186
+ if workers_finished == worker_count
187
+ result_port&.send(SHUTDOWN, move: true)
188
+ break
189
+ end
190
+
191
+ when Ractor
192
+ ractor = data
193
+
194
+ if work_queue.any?
195
+ ractor.send(work_queue.shift, move: true)
196
+ elsif shutdown_received
197
+ ractor.send(SHUTDOWN, move: true)
198
+
199
+ workers_finished += 1
200
+ if workers_finished == worker_count
201
+ result_port&.send(SHUTDOWN, move: true)
202
+ break
203
+ end
204
+ else
205
+ waiting_workers << ractor
206
+ end
207
+
208
+ else
209
+ work = data
210
+
211
+ if waiting_workers.any?
212
+ waiting_workers.shift.send(work, move: true)
213
+ else
214
+ work_queue << work
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end
220
+
221
+ # @rbs () -> Array[Ractor]
222
+ def start_workers
223
+ @size.times.map do |index|
224
+ ractor_name = String.new("#{self.class.name} ractor #{index}")
225
+ ractor_name << " for #{@name}" if @name
226
+
227
+ Ractor.new(@worker, @coordinator, @result_port, name: ractor_name) do |worker, coordinator, result_port|
228
+ loop do
229
+ coordinator&.send(Ractor.current, move: true)
230
+
231
+ work = Ractor.receive
232
+ break if work == SHUTDOWN
233
+
234
+ begin
235
+ result = worker.call(work)
236
+
237
+ result_port&.send(result, move: true)
238
+ rescue => error
239
+ puts "#{Ractor.current.name} rescued:"
240
+ puts "#{error.class}: #{error.message}"
241
+ puts error.backtrace.join("\n")
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end
247
+
248
+ # @rbs () -> Thread?
249
+ def start_collector
250
+ return unless @result_handler
251
+
252
+ thread_name = String.new("#{self.class.name} collector thread")
253
+ thread_name << " for #{@name}" if @name
254
+
255
+ Thread.new(@result_port, @result_handler, thread_name) do |result_port, result_handler, name|
256
+ Thread.current.name = name
257
+
258
+ loop do
259
+ result = result_port.receive
260
+ break if result == SHUTDOWN
261
+
262
+ result_handler.call(result)
263
+ end
264
+ end
265
+ end
266
+ end
@@ -0,0 +1,5 @@
1
+ # Generated from lib/ractor-pool/version.rb with RBS::Inline
2
+
3
+ class RactorPool
4
+ VERSION: ::String
5
+ end
@@ -0,0 +1,126 @@
1
+ # Generated from lib/ractor-pool.rb with RBS::Inline
2
+
3
+ # A thread-safe, lock-free pool of Ractor workers with a coordinator pattern for distributing work.
4
+ #
5
+ # RactorPool manages a fixed number of worker ractors that process work items in parallel.
6
+ # Work is distributed on-demand to idle workers, ensuring efficient utilisation. Results
7
+ # are collected and passed to a result handler running in a separate thread.
8
+ #
9
+ # @example Basic usage
10
+ # results = []
11
+ # worker = -> (work) { work * 2 }
12
+ # pool = RactorPool.new(size: 4, worker: worker) { |result| results << result }
13
+ #
14
+ # 10.times { |index| pool << index }
15
+ # pool.shutdown
16
+ #
17
+ # p results #=> [2, 0, 6, 4, 14, 10, 8, 16, 18, 12]
18
+ #
19
+ # @example Without result handler
20
+ # counter = Atom.new(0)
21
+ # worker = lambda do |work|
22
+ # counter.swap { |current_value| current_value + 1 }
23
+ # work * 2
24
+ # end
25
+ # pool = RactorPool.new(size: 4, worker: worker)
26
+ #
27
+ # 10.times { |index| pool << index }
28
+ # pool.shutdown
29
+ #
30
+ # p counter.value #=> 10
31
+ #
32
+ # @see https://docs.ruby-lang.org/en/master/ractor_md.html Ractor Guide
33
+ # @see https://docs.ruby-lang.org/en/master/Ractor.html Ractor API
34
+ # @see https://docs.ruby-lang.org/en/master/Ractor/Port.html Ractor::Port API
35
+ # @see https://github.com/joshuay03/atomic-ruby atomic-ruby gem
36
+ class RactorPool
37
+ class Error < StandardError
38
+ end
39
+
40
+ class EnqueuedWorkAfterShutdownError < Error
41
+ # @rbs () -> String
42
+ def message: () -> String
43
+ end
44
+
45
+ SHUTDOWN: ::Symbol
46
+
47
+ @collector: Thread?
48
+
49
+ @workers: Array[Ractor]
50
+
51
+ @coordinator: Ractor
52
+
53
+ @result_port: Ractor::Port?
54
+
55
+ @state: Atom[Hash[Symbol, bool | Integer]]
56
+
57
+ @result_handler: ^(untyped) -> void | nil
58
+
59
+ @name: String?
60
+
61
+ @worker: ^(untyped) -> untyped
62
+
63
+ @size: Integer
64
+
65
+ # Creates a new RactorPool with the specified number of workers.
66
+ #
67
+ # @param size [Integer] number of worker ractors to create
68
+ # @param worker [Proc] a shareable lambda that processes each work item
69
+ # @param name [String, nil] optional name for the pool, used in thread/ractor names
70
+ # @yieldparam result [Object] the result returned by the worker lambda
71
+ # @return [void]
72
+ # @raise [ArgumentError] if size is not a positive integer
73
+ # @raise [ArgumentError] if worker is not a lambda
74
+ #
75
+ # @example With result handler
76
+ # pool = RactorPool.new(size: 4, worker: -> { it }) { |result| puts result }
77
+ #
78
+ # @example Without result handler
79
+ # pool = RactorPool.new(size: 4, worker: -> { it })
80
+ #
81
+ # @rbs (?size: Integer, worker: ^(untyped) -> untyped, ?name: String?) ?{ (untyped) -> void } -> void
82
+ def initialize: (worker: ^(untyped) -> untyped, ?size: Integer, ?name: String?) ?{ (untyped) -> void } -> void
83
+
84
+ # Queues a work item to be processed by an available worker.
85
+ #
86
+ # @param work [Object] the work item to process
87
+ # @return [void]
88
+ # @raise [EnqueuedWorkAfterShutdownError] if the pool has been shut down
89
+ #
90
+ # @example
91
+ # pool << "http://example.com/page1"
92
+ # pool << "http://example.com/page2"
93
+ #
94
+ # @rbs (untyped work) -> void
95
+ def <<: (untyped work) -> void
96
+
97
+ # Shuts down the pool gracefully.
98
+ #
99
+ # This method:
100
+ # 1. Prevents new work from being queued
101
+ # 2. Waits for all in-flight work submissions to complete
102
+ # 3. Allows all queued work to complete
103
+ # 4. Waits for all workers to finish
104
+ # 5. Waits for all results to be processed
105
+ #
106
+ # This method is idempotent and can be called multiple times safely.
107
+ #
108
+ # @return [void]
109
+ #
110
+ # @example
111
+ # pool.shutdown
112
+ #
113
+ # @rbs () -> void
114
+ def shutdown: () -> void
115
+
116
+ private
117
+
118
+ # @rbs () -> Ractor
119
+ def start_coordinator: () -> Ractor
120
+
121
+ # @rbs () -> Array[Ractor]
122
+ def start_workers: () -> Array[Ractor]
123
+
124
+ # @rbs () -> Thread?
125
+ def start_collector: () -> Thread?
126
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ractor-pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Young
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: warning
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: atomic-ruby
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ email:
41
+ - djry1999@gmail.com
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - ".buildkite/pipeline.yml"
47
+ - CHANGELOG.md
48
+ - CODE_OF_CONDUCT.md
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/ractor-pool.rb
53
+ - lib/ractor-pool/version.rb
54
+ - sig/generated/ractor-pool.rbs
55
+ - sig/generated/ractor-pool/version.rbs
56
+ homepage: https://github.com/joshuay03/ractor-pool
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ source_code_uri: https://github.com/joshuay03/ractor-pool
61
+ changelog_uri: https://github.com/joshuay03/ractor-pool/blob/main/CHANGELOG.md
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.5.0.dev
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 4.0.0.dev
77
+ specification_version: 4
78
+ summary: A thread-safe, lock-free pool of Ractor workers with a coordinator pattern
79
+ for distributing work
80
+ test_files: []