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.
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) ⇒ Object
Write a frozen value to the proxied Memory.
-
#shutdown ⇒ Object
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.
26 27 28 29 30 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 26 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.
41 42 43 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 41 def get(key) @stub.get(key) end |
#keys ⇒ Array<Symbol>
List all keys currently set in the proxied Memory.
56 57 58 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 56 def keys @stub.keys end |
#set(key, value) ⇒ Object
Write a frozen value to the proxied Memory.
49 50 51 52 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 49 def set(key, value) frozen_value = RactorBoundary.freeze_deep(value) @stub.set(key, frozen_value) end |
#shutdown ⇒ Object
Shut down the ractor-wrapper.
61 62 63 64 65 66 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 61 def shutdown @wrapper.async_stop @wrapper.join rescue => e warn "RactorMemoryProxy shutdown error: #{e.}" end |
#stub ⇒ Ractor::Wrapper stub
Returns the Ractor-shareable stub for use inside Ractors.
34 35 36 |
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 34 def stub @stub end |