Class: Concurrent::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/configuration.rb

Overview

A gem-level configuration object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Create a new configuration object.



19
20
21
22
23
24
# File 'lib/concurrent/configuration.rb', line 19

def initialize
  @global_task_pool      = Delay.new { new_task_pool }
  @global_operation_pool = Delay.new { new_operation_pool }
  @global_timer_set      = Delay.new { Concurrent::TimerSet.new }
  @logger                = no_logger
end

Instance Attribute Details

#loggerObject

a proc defining how to log messages, its interface has to be:

lambda { |level, progname, message = nil, &block| _ }


16
17
18
# File 'lib/concurrent/configuration.rb', line 16

def logger
  @logger
end

Instance Method Details

#global_operation_poolThreadPoolExecutor

Global thread pool optimized for long operations.

Returns:



41
42
43
# File 'lib/concurrent/configuration.rb', line 41

def global_operation_pool
  @global_operation_pool.value
end

#global_operation_pool=(executor) ⇒ ThreadPoolExecutor

Global thread pool optimized for long operations.

A global thread pool must be set as soon as the gem is loaded. Setting a new thread pool once tasks and operations have been post can lead to unpredictable results. The first time a task/operation is post a new thread pool will be created using the default configuration. Once set the thread pool cannot be changed. Thus, explicitly setting the thread pool must occur before any tasks/operations are post else an exception will be raised.

Parameters:

  • executor (Executor)

    the executor to be used for this thread pool

Returns:

Raises:



87
88
89
90
# File 'lib/concurrent/configuration.rb', line 87

def global_operation_pool=(executor)
  @global_operation_pool.reconfigure { executor } or
      raise ConfigurationError.new('global operation pool was already set')
end

#global_task_poolThreadPoolExecutor

Global thread pool optimized for short tasks.

Returns:



34
35
36
# File 'lib/concurrent/configuration.rb', line 34

def global_task_pool
  @global_task_pool.value
end

#global_task_pool=(executor) ⇒ ThreadPoolExecutor

Global thread pool optimized for short tasks.

A global thread pool must be set as soon as the gem is loaded. Setting a new thread pool once tasks and operations have been post can lead to unpredictable results. The first time a task/operation is post a new thread pool will be created using the default configuration. Once set the thread pool cannot be changed. Thus, explicitly setting the thread pool must occur before any tasks/operations are post else an exception will be raised.

Parameters:

  • executor (Executor)

    the executor to be used for this thread pool

Returns:

Raises:



68
69
70
71
# File 'lib/concurrent/configuration.rb', line 68

def global_task_pool=(executor)
  @global_task_pool.reconfigure { executor } or
      raise ConfigurationError.new('global task pool was already set')
end

#global_timer_setThreadPoolExecutor

Global thread pool optimized for timers

Returns:

See Also:

  • timer


50
51
52
# File 'lib/concurrent/configuration.rb', line 50

def global_timer_set
  @global_timer_set.value
end

#new_operation_poolObject



102
103
104
105
106
107
108
109
110
# File 'lib/concurrent/configuration.rb', line 102

def new_operation_pool
  Concurrent::ThreadPoolExecutor.new(
      min_threads:     [2, Concurrent.processor_count].max,
      max_threads:     [2, Concurrent.processor_count].max,
      idletime:        10 * 60, # 10 minutes
      max_queue:       [20, Concurrent.processor_count * 15].max,
      overflow_policy: :abort # raise an exception
  )
end

#new_task_poolObject



92
93
94
95
96
97
98
99
100
# File 'lib/concurrent/configuration.rb', line 92

def new_task_pool
  Concurrent::ThreadPoolExecutor.new(
      min_threads:     [2, Concurrent.processor_count].max,
      max_threads:     [20, Concurrent.processor_count * 15].max,
      idletime:        2 * 60, # 2 minutes
      max_queue:       0, # unlimited
      overflow_policy: :abort # raise an exception
  )
end

#no_loggerObject

if assigned to #logger, it will log nothing.



27
28
29
# File 'lib/concurrent/configuration.rb', line 27

def no_logger
  lambda { |level, progname, message = nil, &block| }
end