Class: RobotLab::RactorMemoryProxy

Inherits:
Object
  • Object
show all
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.

Examples:

memory = Memory.new
proxy  = RactorMemoryProxy.new(memory)

# From any Ractor via the stub:
proxy.set(:result, "done")
proxy.get(:result)   #=> "done"

proxy.shutdown  # call when done

Instance Method Summary collapse

Constructor Details

#initialize(memory) ⇒ RactorMemoryProxy

Returns a new instance of RactorMemoryProxy.

Parameters:

  • memory (Memory)

    the memory instance to wrap



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.

Parameters:

  • key (Symbol)

Returns:

  • (Object, nil)


41
42
43
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 41

def get(key)
  @stub.get(key)
end

#keysArray<Symbol>

List all keys currently set in the proxied Memory.

Returns:

  • (Array<Symbol>)


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.

Parameters:

  • key (Symbol)
  • value (Object)

    must be Ractor-shareable after freeze_deep

Raises:

  • (RactorBoundaryError)

    if value cannot be made shareable



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

#shutdownObject

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.message}"
end

#stubRactor::Wrapper stub

Returns the Ractor-shareable stub for use inside Ractors.

Returns:

  • (Ractor::Wrapper stub)


34
35
36
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 34

def stub
  @stub
end