Class: NeverBlock::DB::FiberedPostgresConnection

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

Overview

A modified postgres connection driver builds on the original pg driver. This driver is able to register the socket at a certain backend (EM or Rev) 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

#register_with_event_loop, #resume_command, #unregister_from_event_loop

Instance Method Details

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

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 23

def exec(sql)
  begin
    if Fiber.respond_to? :current and Fiber.current[:neverblock]          
      send_query sql
      @fiber = Fiber.current          
      Fiber.yield 
      while is_busy
        consume_input
        Fiber.yield if is_busy
      end
      res, data = 0, []
      while res != nil
        res = self.get_result
        data << res unless res.nil?
      end
      data.last          
    else          
      super(sql)
    end
  rescue Exception => e
    reset if e.message.include? "not connected"
    raise e
  end   
end

#resetObject

reset the connection and reattach to the event loop



53
54
55
56
57
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 53

def reset
  unregister_from_event_loop
  super
  register_with_event_loop(@loop)    
end