Module: NeverBlock

Defined in:
lib/never_block.rb,
lib/neverblock/core/pool.rb,
lib/neverblock/io/db/pool.rb,
lib/neverblock/core/reactor.rb,
lib/neverblock/io/db/connection.rb,
lib/neverblock/io/db/drivers/mysql.rb,
lib/neverblock/io/db/fibered_connection_pool.rb,
lib/neverblock/io/db/fibered_mysql_connection.rb

Defined Under Namespace

Modules: DB, Pool Classes: Fiber, FiberPool, NBError

Constant Summary collapse

@@reactors =
{}
@@queue =
Queue.new
@@thread_pool =
[]

Class Method Summary collapse

Class Method Details

.defer(io, action, args) ⇒ Object



45
46
47
48
# File 'lib/neverblock/core/reactor.rb', line 45

def self.defer(io, action, args)
  @@queue << [io, action, args, NB::Fiber.current, NB.reactor]
  NB::Fiber.yield      
end

.neverblock(nb = true, &block) ⇒ Object

The given block will run its queries either in blocking or non-blocking mode based on the first parameter



16
17
18
19
20
21
# File 'lib/never_block.rb', line 16

def self.neverblock(nb = true, &block)
  status = NB::Fiber.current[:neverblock]
  NB::Fiber.current[:neverblock] = nb
  block.call
  NB::Fiber.current[:neverblock] = status
end

.neverblocking?Boolean

Checks if we should be working in a non-blocking mode

Returns:

  • (Boolean)


10
11
12
# File 'lib/never_block.rb', line 10

def self.neverblocking?
  NB::Fiber.respond_to?(:current) && NB::Fiber.current.respond_to?('[]') && NB::Fiber.current[:neverblock] && NB.reactor.running?
end

.reactorObject



26
27
28
# File 'lib/neverblock/core/reactor.rb', line 26

def self.reactor
  @@reactors[Thread.current.object_id] ||= ::Reactor::Base.new
end

.sleep(time) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/neverblock/core/reactor.rb', line 37

def self.sleep(time)
  NB::Fiber.yield if time.nil?
  return if time <= 0 
  fiber = NB::Fiber.current
  NB.reactor.add_timer(time){fiber.resume}
  NB::Fiber.yield
end

.wait(mode, io) ⇒ Object



30
31
32
33
34
35
# File 'lib/neverblock/core/reactor.rb', line 30

def self.wait(mode, io)
  fiber = NB::Fiber.current
  NB.reactor.attach(mode, io){fiber.resume}
  NB::Fiber.yield
  NB.reactor.detach(mode, io)
end