Class: Farce::Local::LeaseMap

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

Overview

A shareable lease map with independently initialized resources in each scope.

Examples:

Keeping independent resources in each Fiber

leases = Farce::Local::LeaseMap.new(scope: :fiber) { { jobs: [] } }
leases.checkout(:jobs) { |jobs| jobs << :parent }
Fiber.new { leases.checkout(:jobs, &:dup) }.resume # => []
leases.checkout(:jobs, &:dup) # => [:parent]

Instance Attribute Summary

Attributes included from Scoped

#scope

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods inherited from Abstract::LeaseMap

#[], #[]=, #auto_lease, #available?, #checked_out?, #checkin, #checkout, #clear, #compare_keys_by_identity?, #compare_values_by_identity?, #delete, #duplicable?, #each, #each_key, #each_value, #fetch, #getkey, #key?, #keys, #lease_for, #owned?, #shareable_keys?, #shareable_values?, #size, #store_if_absent, #to_a, #to_h, #try_checkout, #values

Methods inherited from Abstract::Map

#[], #[]=, #as_extended_json, #as_json, #assert_valid_keys, #assoc, #bson_type, #clear, #compare_by_identity?, #compare_keys_by_identity?, #compare_values_by_identity?, #deconstruct_keys, #delete, #dig, #each, #each_key, #each_pair, #each_value, #empty?, #fetch, #fetch_values, #getkey, #has_key?, #key, #key?, #keys, #length, #rassoc, #shareable_keys?, #shareable_values?, #size, #store, #store_if_absent, #to_a, #to_bson, #to_bson_normalized_value, #to_cbor, #to_h, #to_hash, #to_json, #to_msgpack, #to_query, #to_s, #value?, #values_at, #weak_keys?, #weak_values?

Constructor Details

#initialize(scope: :ractor, normalize_keys: nil) { ... } ⇒ LeaseMap

Create a map that initializes once on first use 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 lease map

Yields:

  • builds the initial mapping for each initialized scope

Yield Returns:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# File 'lib/farce/local/lease_map.rb', line 23

def initialize(scope: :ractor, normalize_keys: nil, &initializer)
  raise ArgumentError, "a resource constructor block is required" unless initializer

  @initializer = Ractor.shareable?(initializer) ? initializer : Ractor.shareable_proc(&initializer)
  super(scope:, normalize_keys:)
end