Class: NeverBlock::DB::FiberedPostgresConnection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ FiberedPostgresConnection

Returns a new instance of FiberedPostgresConnection.



11
12
13
14
15
16
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 11

def initialize(*args)
  super(*args)
  @fd = socket
  @io = IO.new(socket)
  setnonblocking(true)	
end

Instance Attribute Details

#fdObject (readonly)

Returns the value of attribute fd.



9
10
11
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 9

def fd
  @fd
end

#ioObject (readonly)

Returns the value of attribute io.



9
10
11
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 9

def io
  @io
end

Instance Method Details

#exec(sql) ⇒ Object

Assuming the use of NeverBlock fiber extensions and that the exec is run in the context of a fiber



20
21
22
23
24
25
26
27
28
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 20

def exec(sql)
  if Fiber.respond_to? :current and Fiber.current[:neverblock]		      
    self.send_query sql
    @fiber = Fiber.current		      
    Fiber.yield 
  else		      
    super(sql)
  end		
end

#process_commandObject

The callback



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 50

def process_command
  # make sure all commands are sent
  # before attempting to read
  return unless self.flush
  self.consume_input
  unless is_busy		
    res, data = 0, []
    while res != nil
      res = self.get_result
      data << res unless res.nil?
    end
    #let the fiber continue its work		      
    @fiber.resume(data.last)
  end
end

#register_with_event_loop(loop) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/never_block/db/fibered_postgres_connection.rb', line 31

def register_with_event_loop(loop)
  if loop == :em
    unless EM.respond_to?(:notify_for_read)
      puts "invalide EM version, please download the modified gem from: (TBA) "
      exit
    end
    if EM.reactor_running?
      EM::attach(@io,EMConnectionHandler,self)
    else
      raise "REACTOR NOT RUNNING YA ZALAMA"
    end 
  elsif loop.class.name == "REV::Loop"
    loop.attach(RevConnectionHandler.new(socket))
  else
    raise "di zebala ya gahel"
  end
end