Class: NeverBlock::DB::FiberedMysqlConnection

Inherits:
Mysql
  • Object
show all
Includes:
FiberedDBConnection
Defined in:
lib/never_block/db/fibered_mysql_connection.rb

Overview

A modified mysql connection driver. It builds on the original pg driver. This driver is able to register the socket at a certain backend (EM) and then whenever the query is executed within the scope of a friendly fiber. It will be done in async mode and the fiber will yield

Instance Method Summary collapse

Methods included from FiberedDBConnection

#event_loop_connection_close, #register_with_event_loop, #remove_unregister_from_event_loop_callbacks, #resume_command, #unregister_from_event_loop

Constructor Details

#initialize(*args) ⇒ FiberedMysqlConnection

Initializes the connection and remembers the connection params



15
16
17
18
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 15

def initialize(*args)
  @connection_params = args
  super(*@connection_params)
end

Instance Method Details

#query(sql) ⇒ Object Also known as: exec

Assuming the use of NeverBlock fiber extensions and that the exec is run in the context of a fiber. One that have the value :neverblock set to true. All neverblock IO classes check this value, setting it to false will force the execution in a blocking way.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 33

def query(sql)
  if NB.event_loop_available? && NB.neverblocking?
    raise ::NB::NBError.new("FiberedMysqlConnection: The running fiber is attached to a connection other than the current one") if (c = Fiber.current[Fiber.current[:current_pool_key]]) && c != self
    begin
      send_query sql
      Fiber.yield register_with_event_loop
      get_result
    rescue Exception => e
      if error = ['not connected', 'gone away', 'Lost connection'].detect{|msg| e.message.include? msg}
        event_loop_connection_close
        unregister_from_event_loop
        remove_unregister_from_event_loop_callbacks
        #connect
      end
      raise e
    end
  else
    super(sql)
  end
end

#real_connect(*args) ⇒ Object Also known as: connect

Does a normal real_connect if arguments are passed. If no arguments are passed it uses the ones it remembers



22
23
24
25
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 22

def real_connect(*args)
  @connection_params = args unless args.empty?
  super(*@connection_params)
end