class Ractor::Wrapper

An experimental class that wraps a non-shareable object, allowing multiple Ractors to access it concurrently.

WARNING: This is a highly experimental library, and currently not recommended for production use. (As of Ruby 3.0.0, the same can be said of Ractors in general.)

## What is Ractor::Wrapper?

Ractors for the most part cannot access objects concurrently with other Ractors unless the object is shareable (that is, deeply immutable along with a few other restrictions.) If multiple Ractors need to interact with a shared resource that is stateful or otherwise not Ractor-shareable, that resource must itself be implemented and accessed as a Ractor.

`Ractor::Wrapper` makes it possible for such a shared resource to be implemented as an object and accessed using ordinary method calls. It does this by “wrapping” the object in a Ractor, and mapping method calls to message passing. This may make it easier to implement such a resource with a simple class rather than a full-blown Ractor with message passing, and it may also useful for adapting existing legacy object-based implementations.

Given a shared resource object, `Ractor::Wrapper` starts a new Ractor and “runs” the object within that Ractor. It provides you with a stub object on which you can invoke methods. The wrapper responds to these method calls by sending messages to the internal Ractor, which invokes the shared object and then sends back the result. If the underlying object is thread-safe, you can configure the wrapper to run multiple threads that can run methods concurrently. Or, if not, the wrapper can serialize requests to the object.

## Example usage

The following example shows how to share a single `Faraday::Conection` object among multiple Ractors. Because `Faraday::Connection` is not itself thread-safe, this example serializes all calls to it.

require "faraday"

# Create a Faraday connection and a wrapper for it.
connection = Faraday.new "http://example.com"
wrapper = Ractor::Wrapper.new(connection)

# At this point, the connection object cannot be accessed directly
# because it has been "moved" to the wrapper's internal Ractor.
#     connection.get("/whoops")  # <= raises an error

# However, any number of Ractors can now access it through the wrapper.
# By default, access to the object is serialized; methods will not be
# invoked concurrently.
r1 = Ractor.new(wrapper) do |w|
  10.times do
    w.stub.get("/hello")
  end
  :ok
end
r2 = Ractor.new(wrapper) do |w|
  10.times do
    w.stub.get("/ruby")
  end
  :ok
end

# Wait for the two above Ractors to finish.
r1.take
r2.take

# After you stop the wrapper, you can retrieve the underlying
# connection object and access it directly again.
wrapper.async_stop
connection = wrapper.recover_object
connection.get("/finally")

## Features

## Caveats

Ractor::Wrapper is subject to some limitations (and bugs) of Ractors, as of Ruby 3.0.0.

Constants

VERSION

The version of the ractor-wrapper gem

@return [String]

Attributes

logging[R]

Return whether logging is enabled for this wrapper.

@return [Boolean]

name[R]

Return the name of this wrapper.

@return [String, nil]

stub[R]

Return the wrapper stub. This is an object that responds to the same methods as the wrapped object, providing an easy way to call a wrapper.

@return [Ractor::Wrapper::Stub]

threads[R]

Return the number of threads used by the wrapper.

@return [Integer]

Public Class Methods

new(object, threads: 1, move: false, move_arguments: nil, move_return: nil, logging: false, name: nil) { |self| ... } click to toggle source

Create a wrapper around the given object.

If you pass an optional block, the wrapper itself will be yielded to it at which time you can set additional configuration options. (The configuration is frozen once the object is constructed.)

@param object [Object] The non-shareable object to wrap. @param threads [Integer] The number of worker threads to run.

Defaults to 1, which causes the worker to serialize calls.
# File lib/ractor/wrapper.rb, line 118
def initialize(object,
               threads: 1,
               move: false,
               move_arguments: nil,
               move_return: nil,
               logging: false,
               name: nil)
  @method_settings = {}
  self.threads = threads
  self.logging = logging
  self.name = name
  configure_method(move: move, move_arguments: move_arguments, move_return: move_return)
  yield self if block_given?
  @method_settings.freeze

  maybe_log("Starting server")
  @ractor = ::Ractor.new(name: name) { Server.new.run }
  opts = {
    object: object,
    threads: @threads,
    method_settings: @method_settings,
    name: @name,
    logging: @logging,
  }
  @ractor.send(opts, move: true)

  maybe_log("Server ready")
  @stub = Stub.new(self)
  freeze
end

Public Instance Methods

async_stop() click to toggle source

Request that the wrapper stop. All currently running calls will complete before the wrapper actually terminates. However, any new calls will fail.

This metnod is idempotent and can be called multiple times (even from different ractors).

@return [self]

# File lib/ractor/wrapper.rb, line 294
def async_stop
  maybe_log("Stopping #{name}")
  @ractor.send(Message.new(:stop))
  self
rescue ::Ractor::ClosedError
  # Ignore to allow stops to be idempotent.
  self
end
call(method_name, *args, **kwargs) click to toggle source

A lower-level interface for calling methods through the wrapper.

@param method_name [Symbol] The name of the method to call @param args [arguments] The positional arguments @param kwargs [keywords] The keyword arguments @return [Object] The return value

# File lib/ractor/wrapper.rb, line 268
def call(method_name, *args, **kwargs)
  request = Message.new(:call, data: [method_name, args, kwargs])
  transaction = request.transaction
  move = method_settings(method_name).move_arguments?
  maybe_log("Sending method #{method_name} (move=#{move}, transaction=#{transaction})")
  @ractor.send(request, move: move)
  reply = ::Ractor.receive_if { |msg| msg.is_a?(Message) && msg.transaction == transaction }
  case reply.type
  when :result
    maybe_log("Received result for method #{method_name} (transaction=#{transaction})")
    reply.data
  when :error
    maybe_log("Received exception for method #{method_name} (transaction=#{transaction})")
    raise reply.data
  end
end
configure_method(method_name = nil, move: false, move_arguments: nil, move_return: nil) click to toggle source

Configure the move semantics for the given method (or the default settings if no method name is given.) That is, determine whether arguments, return values, and/or exceptions are copied or moved when communicated with the wrapper. By default, all objects are copied.

This method can be called only during an initialization block. All settings are frozen once the wrapper is active.

@param method_name [Symbol, nil] The name of the method being configured,

or `nil` to set defaults for all methods not configured explicitly.

@param move [Boolean] Whether to move all communication. This value, if

given, is used if `move_arguments`, `move_return`, or
`move_exceptions` are not set.

@param move_arguments [Boolean] Whether to move arguments. @param move_return [Boolean] Whether to move return values.

# File lib/ractor/wrapper.rb, line 208
def configure_method(method_name = nil,
                     move: false,
                     move_arguments: nil,
                     move_return: nil)
  method_name = method_name.to_sym unless method_name.nil?
  @method_settings[method_name] =
    MethodSettings.new(move: move, move_arguments: move_arguments, move_return: move_return)
end
logging=(value) click to toggle source

Enable or disable internal debug logging.

This method can be called only during an initialization block. All settings are frozen once the wrapper is active.

@param value [Boolean]

# File lib/ractor/wrapper.rb, line 174
def logging=(value)
  @logging = value ? true : false
end
method_settings(method_name) click to toggle source

Return the method settings for the given method name. This returns the default method settings if the given method is not configured explicitly by name.

@param method_name [Symbol,nil] The method name, or `nil` to return the

defaults.

@return [MethodSettings]

# File lib/ractor/wrapper.rb, line 255
def method_settings(method_name)
  method_name = method_name.to_sym
  @method_settings[method_name] || @method_settings[nil]
end
name=(value) click to toggle source

Set the name of this wrapper. This is shown in logging, and is also used as the name of the wrapping Ractor.

This method can be called only during an initialization block. All settings are frozen once the wrapper is active.

@param value [String, nil]

# File lib/ractor/wrapper.rb, line 187
def name=(value)
  @name = value ? value.to_s.freeze : nil
end
recovered_object() click to toggle source

Retrieves the original object that was wrapped. This should be called only after a stop request has been issued using {#async_stop}, and may block until the wrapper has fully stopped.

Only one ractor may call this method; any additional calls will fail.

@return [Object] The original wrapped object

# File lib/ractor/wrapper.rb, line 312
def recovered_object
  @ractor.take
end
threads=(value) click to toggle source

Set the number of threads to run in the wrapper. If the underlying object is thread-safe, this allows concurrent calls to it. If the underlying object is not thread-safe, you should leave this set to its default of 1, which effectively causes calls to be serialized.

This method can be called only during an initialization block. All settings are frozen once the wrapper is active.

@param value [Integer]

# File lib/ractor/wrapper.rb, line 160
def threads=(value)
  value = value.to_i
  value = 1 if value < 1
  @threads = value
end

Private Instance Methods

maybe_log(str) click to toggle source
# File lib/ractor/wrapper.rb, line 318
def maybe_log(str)
  return unless logging
  time = ::Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%L")
  $stderr.puts("[#{time} Ractor::Wrapper/#{name}]: #{str}")
  $stderr.flush
end