Module: PG::EM::Client::ConnectWatcher

Defined in:
lib/pg/em/client/connect_watcher.rb

Overview

This module is used as a handler to ::EM.watch connection socket and it performs connection handshake with postgres server asynchronously.

Author

Rafal Michalski

Instance Method Summary collapse

Instance Method Details

#initialize(client, deferrable, is_reset) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pg/em/client/connect_watcher.rb', line 11

def initialize(client, deferrable, is_reset)
  @client = client
  @deferrable = deferrable
  @is_reset = is_reset
  @poll_method = is_reset ? :reset_poll : :connect_poll
  if (timeout = client.connect_timeout) > 0
    @timer = ::EM::Timer.new(timeout) do
      detach
      @deferrable.protect do
        error = ConnectionBad.new("timeout expired (async)")
        error.instance_variable_set(:@connection, @client)
        raise error
      end
    end
  end
end

#poll_connection_and_checkObject Also known as: notify_writable, notify_readable



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg/em/client/connect_watcher.rb', line 32

def poll_connection_and_check
  case @client.__send__(@poll_method)
  when PG::PGRES_POLLING_READING
    self.notify_readable = true
    self.notify_writable = false
    return
  when PG::PGRES_POLLING_WRITING
    self.notify_writable = true
    self.notify_readable = false
    return
  when PG::PGRES_POLLING_OK
    polling_ok = true if @client.status == PG::CONNECTION_OK
  end
  @timer.cancel if @timer
  detach
  @deferrable.protect_and_succeed do
    unless polling_ok
      error = ConnectionBad.new(@client.error_message)
      error.instance_variable_set(:@connection, @client)
      raise error
    end
    @client.set_default_encoding unless reconnecting?
    @client
  end
end

#reconnecting?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/pg/em/client/connect_watcher.rb', line 28

def reconnecting?
  @is_reset
end