aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/rack/thread_pool.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/plum/rack/thread_pool.rb')
-rw-r--r--lib/plum/rack/thread_pool.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/plum/rack/thread_pool.rb b/lib/plum/rack/thread_pool.rb
new file mode 100644
index 0000000..9e07943
--- /dev/null
+++ b/lib/plum/rack/thread_pool.rb
@@ -0,0 +1,35 @@
+# -*- frozen-string-literal: true -*-
+module Plum
+ module Rack
+ class ThreadPool
+ def initialize(size = 20)
+ @workers = Set.new
+ @jobs = Queue.new
+
+ size.times { |i|
+ spawn_worker
+ }
+ end
+
+ # returns cancel token
+ def acquire(tag = nil, err = nil, &blk)
+ @jobs << [blk, err]
+ end
+
+ private
+ def spawn_worker
+ t = Thread.new {
+ while true
+ job, err = @jobs.pop
+ begin
+ job.call
+ rescue => e
+ err << e if err
+ end
+ end
+ }
+ @workers << t
+ end
+ end
+ end
+end