Class: Farce::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/farce/config.rb,
lib/farce/integrations/active_support/duplicable.rb

Overview

Configuration for Farce. Will be frozen by Farce once it is accessed, to prevent accidental changes and make it shareable across Ractors.

require "farce"

Farce.config do |c|
  c.fiber_scheduler = :select
end

Instance Attribute Summary collapse

ActiveSupport Integration collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ Config

Returns a new instance of Config.

Yields:

  • (config) —

    block to modify the configuration

Yield Parameters:

  • config (Config) —

    the configuration object to configure



41
42
43
44
45
46
47
# File 'lib/farce/config.rb', line 41

def initialize
  yield self if block_given?
  self.autoload_integrations = ENV.fetch("FARCE_AUTOLOAD_INTEGRATIONS", true) if autoload_integrations.nil?
  self.fiber_scheduler       = ENV["FARCE_FIBER_SCHEDULER"] unless instance_variable_defined?(:@fiber_scheduler)
  self.main_thread_pool_size       ||= ENV.fetch("FARCE_MAIN_THREAD_POOL_SIZE", 4)
  self.additional_thread_pool_size ||= ENV.fetch("FARCE_ADDITIONAL_THREAD_POOL_SIZE", 2)
end

Instance Attribute Details

#additional_thread_pool_size ⇒ BasicObject

Maximum workers in a pool created on another Ractor. Workers start lazily. Can also be set via FARCE_ADDITIONAL_THREAD_POOL_SIZE. Defaults to 2.



37
38
39
# File 'lib/farce/config.rb', line 37

def additional_thread_pool_size
  @additional_thread_pool_size
end

#autoload_integrations ⇒ Boolean

Automatically load integrations when their dependencies are loaded. Defaults to true. Can also be set via FARCE_AUTOLOAD_INTEGRATIONS (true/false or 1/0). Set before requiring Farce to also disable startup integration loading.

Returns:

  • (Boolean)


29
30
31
# File 'lib/farce/config.rb', line 29

def autoload_integrations
  @autoload_integrations
end

#main_thread_pool_size ⇒ BasicObject

Maximum workers in a pool created on the main Ractor. Workers start lazily. Can also be set via FARCE_MAIN_THREAD_POOL_SIZE. Defaults to 4.



33
34
35
# File 'lib/farce/config.rb', line 33

def main_thread_pool_size
  @main_thread_pool_size
end

Instance Method Details

#duplicable? ⇒ Boolean

Note:

This methods is only available if ActiveSupport has been loaded.

Returns true.

Returns:

  • (Boolean) —

    true



12
# File 'lib/farce/integrations/active_support/duplicable.rb', line 12

def duplicable? = true

#fiber_scheduler(&constructor) ⇒ Symbol, ...

Default fiber scheduler for Pool and Scheduler. Explicit constructor blocks override it.

Uses FARCE_FIBER_SCHEDULER unless assigned explicitly, including an explicit nil.

Built-in choices are :auto, :native, :jvm, :select, :kqueue, :epoll, :io_uring, and :nio. Strings are also accepted. nil or an empty string are treated like :auto.

Other names resolve constants, such as carbon_fiber to CarbonFiber. If the constant is not defined, it attempts to require the corresponding file. Modules must define their own Scheduler class.

Classes are constructed without arguments in each worker. Procs must be shareable. Custom schedulers must support the executor used by the Pool or Scheduler.

Returns:

  • (Symbol, Class, Proc) —

    normalized choice



92
93
94
95
# File 'lib/farce/config.rb', line 92

def fiber_scheduler(&constructor)
  self.fiber_scheduler = constructor if constructor
  @fiber_scheduler
end

#fiber_scheduler=(value) ⇒ BasicObject

Sets the default scheduler. Use config.fiber_scheduler { MyScheduler.new } or assign a proc to configure construction. Invalid assignments preserve the old value.

Raises:

  • (FrozenError)


99
100
101
102
# File 'lib/farce/config.rb', line 99

def fiber_scheduler=(value)
  raise FrozenError, "can't modify frozen Config" if frozen?
  @fiber_scheduler = normalize_fiber_scheduler(value)
end

#fiber_scheduler_implementation ⇒ Symbol

Implementation used by Farce's built-in scheduler.

Returns:

  • (Symbol) —

    one of :native, :select, or :jvm



106
107
108
109
110
111
112
113
# File 'lib/farce/config.rb', line 106

def fiber_scheduler_implementation
  case @fiber_scheduler
  when :native, :jvm, :select     then @fiber_scheduler
  when :kqueue, :epoll, :io_uring then :native
  when :nio                       then :jvm
  else detected_fiber_scheduler_implementation
  end
end

#freeze ⇒ BasicObject

Called internally once the configuration is applied.



147
148
149
150
151
152
# File 'lib/farce/config.rb', line 147

def freeze
  return self if frozen?
  super # need to call this first to avoid infinite recursion in the next line
  ::Ractor.make_shareable(self) if defined?(::Ractor.make_shareable)
  self
end

#io_backend ⇒ Symbol

IO backend used by the built-in scheduler. Explicit backend: options override it. Availability is checked when a scheduler is created.

Returns:

  • (Symbol) —

    the configured backend or :auto



118
119
120
121
122
123
# File 'lib/farce/config.rb', line 118

def io_backend
  case @fiber_scheduler
  when :kqueue, :epoll, :io_uring, :nio, :select then @fiber_scheduler
  else :auto
  end
end