Class: Garcon::CachedThreadPool
- Inherits:
-
ThreadPoolExecutor
- Object
- ThreadPoolExecutor
- Garcon::CachedThreadPool
- Defined in:
- lib/garcon/task/thread_pool/cached.rb
Overview
A thread pool that dynamically grows and shrinks to fit the current workload. New threads are created as needed, existing threads are reused, and threads that remain idle for too long are killed and removed from the pool. These pools are particularly suited to applications that perform a high volume of short-lived tasks.
On creation a ‘CachedThreadPool` has zero running threads. New threads are created on the pool as new operations are `#post`. The size of the pool will grow until `#max_length` threads are in the pool or until the number of threads exceeds the number of running and pending operations. When a new operation is post to the pool the first available idle thread will be tasked with the new operation.
Should a thread crash for any reason the thread will immediately be removed from the pool. Similarly, threads which remain idle for an extended period of time will be killed and reclaimed. Thus these thread pools are very efficient at reclaiming unused resources.
Constant Summary
Constants inherited from ThreadPoolExecutor
ThreadPoolExecutor::DEFAULT_MAX_POOL_SIZE, ThreadPoolExecutor::DEFAULT_MAX_QUEUE_SIZE, ThreadPoolExecutor::DEFAULT_MIN_POOL_SIZE, ThreadPoolExecutor::DEFAULT_THREAD_IDLETIMEOUT
Constants included from RubyExecutor
Instance Attribute Summary
Attributes inherited from ThreadPoolExecutor
#completed_task_count, #idletime, #largest_length, #max_length, #max_queue, #min_length, #scheduled_task_count
Attributes included from Executor
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ CachedThreadPool
constructor
Create a new thread pool.
Methods inherited from ThreadPoolExecutor
#can_overflow?, #length, #queue_length, #remaining_capacity, #status
Methods included from RubyExecutor
#<<, #kill, #post, #running?, #shutdown, #shutdown?, #shuttingdown?, #wait_for_termination
Methods included from Executor
#auto_terminate?, #can_overflow?, #serialized?
Constructor Details
#initialize(opts = {}) ⇒ CachedThreadPool
Create a new thread pool.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/garcon/task/thread_pool/cached.rb', line 54 def initialize(opts = {}) fallback = opts.fetch(:fallback, :abort) unless FALLBACK_POLICY.include?(fallback) raise ArgumentError, "#{fallback} is not a valid fallback policy" end opts = opts.merge( min_threads: 0, max_threads: DEFAULT_MAX_POOL_SIZE, fallback: fallback, max_queue: DEFAULT_MAX_QUEUE_SIZE, idletime: DEFAULT_THREAD_IDLETIMEOUT) super(opts) end |