Module: NeverBlock::DB::FiberedDBConnection

Included in:
FiberedMysqlConnection, FiberedPostgresConnection
Defined in:
lib/never_block/db/fibered_db_connection.rb

Instance Method Summary collapse

Instance Method Details

#event_loop_connection_closeObject

Closes the connection using event loop



46
47
48
49
# File 'lib/never_block/db/fibered_db_connection.rb', line 46

def event_loop_connection_close
  key = em_connection_with_pool_key
  @fiber[key].close_connection if @fiber[key]
end

#register_with_event_loopObject

Attaches the connection socket to an event loop and adds a callback to the fiber’s callbacks that unregisters the connection from event loop Raises NB::NBError



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/never_block/db/fibered_db_connection.rb', line 8

def register_with_event_loop
  #puts ">>>>>register_with_event_loop"
  if EM.reactor_running?
    @fiber = Fiber.current
    #puts ">>>>>register_with_event_loop fiber #{@fiber.inspect}"
    # When there's no previous em_connection
    key = em_connection_with_pool_key
    unless @fiber[key]
      @fiber[key] = EM::watch(socket,EMConnectionHandler,self) { |c| c.notify_readable = true }
      @fiber[:callbacks] << self.method(:unregister_from_event_loop)
      @fiber[:em_keys] << key
    end
  else
    raise ::NB::NBError.new("FiberedDBConnection: EventMachine reactor not running")
  end
end

#remove_unregister_from_event_loop_callbacksObject

Removes the unregister_from_event_loop callback from the fiber’s callbacks. It should be used when errors occur in an already registered connection



41
42
43
# File 'lib/never_block/db/fibered_db_connection.rb', line 41

def remove_unregister_from_event_loop_callbacks
  @fiber[:callbacks].delete self.method(:unregister_from_event_loop)
end

#resume_commandObject

The callback, this is called whenever there is data available at the socket



53
54
55
# File 'lib/never_block/db/fibered_db_connection.rb', line 53

def resume_command
  @fiber.resume if @fiber
end

#unregister_from_event_loopObject

Unattaches the connection socket from the event loop



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/never_block/db/fibered_db_connection.rb', line 26

def unregister_from_event_loop
  #puts ">>>>>unregister_from_event_loop #{self.inspect} #{@fiber.inspect}"
  key = @fiber[:em_keys].pop
  if em_c = @fiber[key]
    em_c.detach
    @fiber[key] = nil
    true
  else
    false
  end
end