Skip to content

Commit

Permalink
(libuv:reactor) replace default threadpool
Browse files Browse the repository at this point in the history
to prevent internal libuv tasks from being blocked by application tasks
  • Loading branch information
Stephen von Takach committed May 16, 2018
1 parent 860a503 commit ab48cb5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/libuv/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Reactor


CRITICAL = ::Mutex.new
THREAD_POOL = ::Concurrent::FixedThreadPool.new(8)


module ClassMethods
Expand Down Expand Up @@ -425,9 +426,19 @@ def on_program_interrupt(&callback)
# @param callback [Proc] the callback to be called in the thread pool
# @return [::Libuv::Work]
# @raise [ArgumentError] if block is not given
def work(&callback)
assert_block(callback)
Work.new(@reactor, &callback)
def work
ref
d = defer
THREAD_POOL.post do
begin
d.resolve(yield)
rescue Exception => e
d.reject(e)
end
end
promise = d.promise
promise.finally { unref }
promise
end

# Lookup a hostname
Expand Down

0 comments on commit ab48cb5

Please sign in to comment.