Sha256: f766a5f739bd42cabd3cc4a2b54c0e31c4287ea5b5f62c47d4342ed2c58ea2b9
Contents?: true
Size: 1.17 KB
Versions: 9
Compression:
Stored size: 1.17 KB
Contents
require "concurrent" require "concurrent/atomics" require "concurrent/executors" require "thread" # Simple wrapper for a FixedThreadPool that rejects new jobs if all the threads are busy, rather # than blocking. Also provides a way to wait for all jobs to finish without shutting down. module LaunchDarkly class NonBlockingThreadPool def initialize(capacity) @capacity = capacity @pool = Concurrent::FixedThreadPool.new(capacity) @semaphore = Concurrent::Semaphore.new(capacity) end # Attempts to submit a job, but only if a worker is available. Unlike the regular post method, # this returns a value: true if the job was submitted, false if all workers are busy. def post if !@semaphore.try_acquire(1) return end @pool.post do begin yield ensure @semaphore.release(1) end end end # Waits until no jobs are executing, without shutting down the pool. def wait_all @semaphore.acquire(@capacity) @semaphore.release(@capacity) end def shutdown @pool.shutdown end def wait_for_termination @pool.wait_for_termination end end end
Version data entries
9 entries across 9 versions & 1 rubygems