Class: RobotLab::RactorMemoryProxy
- Inherits:
-
Object
- Object
- RobotLab::RactorMemoryProxy
- Defined in:
- lib/robot_lab/ractor_memory_proxy.rb
Overview
Wraps a Memory instance via Ractor::Wrapper so Ractor workers can safely read and write shared state.
Only get, set, and keys are proxied across the Ractor boundary. Subscriptions and callbacks are NOT proxied — closures are not Ractor-safe. Use the thread-side Memory directly for reactive subscriptions.
Values passed to set() must be Ractor-shareable; RactorBoundary.freeze_deep is applied automatically.
The proxy uses use_current_ractor: true so the Memory object stays in the calling Ractor and is not moved. This allows direct access alongside the proxy and works with Memory’s mutex-based internals.
Instance Method Summary collapse
-
#get(key) ⇒ Object?
Read a value from the proxied Memory.
-
#initialize(memory) ⇒ RactorMemoryProxy
constructor
A new instance of RactorMemoryProxy.
-
#keys ⇒ Array<Symbol>
List all keys currently set in the proxied Memory.
-
#set(key, value) ⇒ void
Write a frozen value to the proxied Memory.
-
#shutdown ⇒ void
Shut down the ractor-wrapper.
-
#stub ⇒ Ractor::Wrapper stub
Returns the Ractor-shareable stub for use inside Ractors.
Constructor Details
#initialize(memory) ⇒ RactorMemoryProxy
Returns a new instance of RactorMemoryProxy.
32 33 34 35 36 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 32 def initialize(memory) @memory = memory @wrapper = Ractor::Wrapper.new(memory, use_current_ractor: true) @stub = @wrapper.stub end |
Instance Method Details
#get(key) ⇒ Object?
Read a value from the proxied Memory.
52 53 54 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 52 def get(key) @stub.get(key) end |
#keys ⇒ Array<Symbol>
List all keys currently set in the proxied Memory.
71 72 73 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 71 def keys @stub.keys end |
#set(key, value) ⇒ void
This method returns an undefined value.
Write a frozen value to the proxied Memory. The value is deep-frozen before crossing the Ractor boundary.
63 64 65 66 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 63 def set(key, value) frozen_value = RactorBoundary.freeze_deep(value) @stub.set(key, frozen_value) end |
#shutdown ⇒ void
This method returns an undefined value.
Shut down the ractor-wrapper.
78 79 80 81 82 83 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 78 def shutdown @wrapper.async_stop @wrapper.join rescue => e RobotLab.config.logger.warn("RactorMemoryProxy shutdown error: #{e.}") end |
#stub ⇒ Ractor::Wrapper stub
Returns the Ractor-shareable stub for use inside Ractors.
The stub proxies get/set/keys to the wrapped Memory. Pass this to Ractor.new rather than the proxy itself (the proxy is not shareable).
44 45 46 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 44 def stub @stub end |