Class: Farce::Local::LeasePool

Inherits:
Abstract::LeasePool show all
Includes:
Scoped, Shareable::Unfreezable
Defined in:
lib/farce/local/lease_pool.rb

Overview

A shareable pool with independent capacity and resources in each scope.

Examples:

Keeping separate pools in each Fiber

pool = Farce::Local::LeasePool.new(scope: :fiber, max_size: 1) { [] }
pool.checkout { |items| items << :parent }

Fiber.new { pool.checkout(&:dup) }.resume # => []
pool.checkout(&:dup) # => [:parent]

Instance Attribute Summary

Attributes included from Scoped

#scope

Attributes inherited from Abstract::LeasePool

#max_size

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods inherited from Abstract::LeasePool

#available_count, #checked_out_count, #checkin, #checkout, #creating_count, #size, #try_checkout

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize(max_size:, scope: :ractor) { ... } ⇒ LeasePool

Construct an empty pool in each scope. Valid scopes are:

  • :ractor - The value is shared by all threads within the same Ractor.
  • :thread_group - The value is shared by all threads within the same thread group.
  • :thread - The value is shared by all fibers within the same thread.
  • :fiber_storage - The value is shared by all fibers using the same storage. Unless explicitly specified, storage is inherited from the parent fiber for blocking fibers, but not for non-blocking fibers (such as those created by Scheduler#schedule).
  • :fiber - Each fiber has its own independent value.

Parameters:

  • scope (Symbol) (defaults to: :ractor) —

    the scope of the pool

  • max_size (Integer) —

    the maximum number of resources in each scope

Yields:

  • builds a resource in the requesting scope

Yield Returns:

  • (BasicObject) —

    a new resource



25
26
27
28
29
30
# File 'lib/farce/local/lease_pool.rb', line 25

def initialize(max_size:, scope: :ractor, &factory)
  validate_configuration!(max_size, factory)
  @max_size = max_size
  @factory  = Ractor.shareable?(factory) ? factory : Ractor.shareable_proc(&factory)
  super(scope:, max_size:)
end