ractor-dispatch 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: 8d467750fb7358768f6bdaefc11134e62f60fe642198cf28141ee160563caf43
4
+ data.tar.gz: 3974b307afe4d1f32fb1a3aa7989712465fb496a2c98fc475463f606cde1e9d8
5
+ SHA512:
6
+ metadata.gz: 38c7e36f435c8e9bfbba832bd8ceb313bddcf59d781f939e3079e88aae725c08f112db7ebeb39ad14982467010a5af1f57883443f568b55392a1c6b443415fd1
7
+ data.tar.gz: e4d1c7e84f803565684a0b789908d576c0a9f765d2147fb457e8a8a6b50a92d8820722e0d800efe9c2a9e58b1af3a2a2424ff897fb908b39f524f48bfc1f3b27
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "ractor-dispatch" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["john@hawthorn.email"](mailto:"john@hawthorn.email").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 John Hawthorn
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,95 @@
1
+ # Ractor::Dispatch
2
+
3
+ Dispatch work to a specific Ractor and get results back. The primary use case
4
+ is an "escape hatch" for code running in non-main Ractors that needs to execute
5
+ something only the main Ractor can do (access globals, unshareable constants,
6
+ ENV, etc).
7
+
8
+ ## Usage
9
+
10
+ ```ruby
11
+ require "ractor/dispatch"
12
+
13
+ Ractor.new do
14
+ # Synchronous — blocks until the main Ractor returns the result
15
+ home = Ractor::Dispatch.main.run { ENV["HOME"] }
16
+ puts home
17
+
18
+ # Async — returns a Future immediately
19
+ future = Ractor::Dispatch.main.submit { ENV["PATH"] }
20
+ # ... do other work ...
21
+ path = future.value
22
+ end.join
23
+ ```
24
+
25
+ The passed block is automatically made into a shareable proc. Because shareable
26
+ procs can close over frozen/shareable values, this works naturally with frozen
27
+ string literals and other shareable objects.
28
+
29
+ ### Error handling
30
+
31
+ Exceptions raised by the block are propagated back to the caller:
32
+
33
+ ```ruby
34
+ Ractor.new do
35
+ Ractor::Dispatch.main.run { raise "oops" } # => raises RuntimeError "oops"
36
+ end.join
37
+ ```
38
+
39
+ ### Shutdown
40
+
41
+ ```ruby
42
+ Ractor::Dispatch.main.shutdown # closes the work port, background thread exits
43
+ ```
44
+
45
+ ### Custom executors
46
+
47
+ `Ractor::Dispatch.main` is a convenience for the common case. You can also
48
+ create your own executor on any Ractor:
49
+
50
+ ```ruby
51
+ executor = Ractor::Dispatch::Executor.new
52
+
53
+ Ractor.new(executor) do |ex|
54
+ ex.run { ENV["HOME"] }
55
+ end.join
56
+ ```
57
+
58
+ ## How it works
59
+
60
+ `Ractor::Dispatch.main` lazily creates an `Executor` on the main Ractor.
61
+ `Executor.new` creates a `Ractor::Port` and spawns a background `Thread` on
62
+ the current Ractor that loops receiving work from the port. The Executor is
63
+ then made shareable so it can be passed to other Ractors.
64
+
65
+ When `submit` is called, the block is made into a shareable proc, the caller
66
+ creates a reply `Ractor::Port`, sends `[callable, args, reply_port]` to the
67
+ executor's work port, and returns a `Future` wrapping the reply port. The
68
+ background thread receives the job, executes it, and sends the result back on
69
+ the reply port.
70
+
71
+ The key `Ractor::Port` property that makes this work: **any Ractor can send
72
+ to a port, but only the creating Ractor can receive from it**. This means
73
+ reply ports naturally route results back to the correct caller.
74
+
75
+ ## Requirements
76
+
77
+ Requires Ruby with `Ractor::Port` support (Ruby 4.0+).
78
+
79
+ ## Development
80
+
81
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/ractor-dispatch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jhawthorn/ractor-dispatch/blob/main/CODE_OF_CONDUCT.md).
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
92
+
93
+ ## Code of Conduct
94
+
95
+ Everyone interacting in the Ractor::Dispatch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/ractor-dispatch/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Ractor
4
+ module Dispatch
5
+ class Executor
6
+ def initialize
7
+ @port = Ractor::Port.new
8
+
9
+ Thread.new do
10
+ loop do
11
+ callable, reply_port = @port.receive
12
+ begin
13
+ result = callable.call
14
+ reply_port << [:ok, result]
15
+ rescue => e
16
+ reply_port << [:error, e]
17
+ rescue Ractor::ClosedError
18
+ # caller went away, discard
19
+ end
20
+ end
21
+ rescue Ractor::ClosedError
22
+ # port closed via shutdown
23
+ end
24
+
25
+ Ractor.make_shareable(self)
26
+ end
27
+
28
+ def submit(&block)
29
+ callable = Ractor.shareable_proc(&block)
30
+ reply_port = Ractor::Port.new
31
+ @port << [callable, reply_port]
32
+ Future.new(reply_port)
33
+ end
34
+
35
+ def run(&block)
36
+ submit(&block).value
37
+ end
38
+
39
+ def shutdown
40
+ @port.close
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Ractor
4
+ module Dispatch
5
+ class Future
6
+ def initialize(port)
7
+ @port = port
8
+ @mutex = Mutex.new
9
+ end
10
+
11
+ def value
12
+ @mutex.synchronize do
13
+ unless defined?(@resolved)
14
+ @resolved = true
15
+ status, val = @port.receive
16
+ @port.close
17
+ if status == :error
18
+ @error = val
19
+ else
20
+ @value = val
21
+ end
22
+ end
23
+ end
24
+
25
+ raise @error if @error
26
+ @value
27
+ end
28
+
29
+ def resolved?
30
+ @mutex.synchronize { defined?(@resolved) }
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Ractor
4
+ module Dispatch
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dispatch/version"
4
+ require_relative "dispatch/future"
5
+ require_relative "dispatch/executor"
6
+
7
+ class Ractor
8
+ module Dispatch
9
+ class Error < StandardError; end
10
+
11
+ @main = Executor.new
12
+
13
+ def self.main
14
+ @main
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ module Ractor
2
+ module Dispatch
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ractor-dispatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Hawthorn
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A lightweight library for dispatching work to a specific Ractor. Useful
13
+ as an escape hatch when code running in non-main Ractors needs to execute something
14
+ only the main Ractor can do.
15
+ email:
16
+ - john@hawthorn.email
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/ractor/dispatch.rb
26
+ - lib/ractor/dispatch/executor.rb
27
+ - lib/ractor/dispatch/future.rb
28
+ - lib/ractor/dispatch/version.rb
29
+ - sig/ractor/dispatch.rbs
30
+ homepage: https://github.com/jhawthorn/ractor-dispatch
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/jhawthorn/ractor-dispatch
35
+ source_code_uri: https://github.com/jhawthorn/ractor-dispatch
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.0.0.dev
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 4.0.3
51
+ specification_version: 4
52
+ summary: Dispatch work to a specific Ractor and get results back
53
+ test_files: []