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.

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.

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



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.

Parameters:

  • key (Symbol)

Returns:

  • (Object, nil)


52
53
54
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 52

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

#keysArray<Symbol>

List all keys currently set in the proxied Memory.

Returns:

  • (Array<Symbol>)


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.

Parameters:

  • key (Symbol)
  • value (Object)

    must be Ractor-shareable after freeze_deep

Raises:



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

#shutdownvoid

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

#stubRactor::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).

Returns:

  • (Ractor::Wrapper stub)


44
45
46
# File 'lib/robot_lab/ractor_memory_proxy.rb', line 44

def stub
  @stub
end