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

Defined in:
lib/pg/em.rb

Instance Method Summary collapse

Instance Method Details

#initialize(client, deferrable, poll_method) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/pg/em.rb', line 221

def initialize(client, deferrable, poll_method)
  @client = client
  @deferrable = deferrable
  @poll_method = :"#{poll_method}_poll"
  if (timeout = client.connect_timeout) > 0
    @timer = ::EM::Timer.new(timeout) do
      begin
        detach
        @deferrable.protect do
          raise PG::Error, "timeout expired (async)"
        end
      ensure
        @client.finish unless reconnecting?
      end
    end
  end
end

#notify_readableObject



247
248
249
# File 'lib/pg/em.rb', line 247

def notify_readable
  poll_connection_and_check
end

#notify_writableObject



243
244
245
# File 'lib/pg/em.rb', line 243

def notify_writable
  poll_connection_and_check
end

#poll_connection_and_checkObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/pg/em.rb', line 251

def poll_connection_and_check
  case @client.__send__(@poll_method)
  when PG::PGRES_POLLING_READING
    self.notify_readable = true
    self.notify_writable = false
  when PG::PGRES_POLLING_WRITING
    self.notify_writable = true
    self.notify_readable = false
  when PG::PGRES_POLLING_OK, PG::PGRES_POLLING_FAILED
    @timer.cancel if @timer
    detach
    @deferrable.protect_and_succeed do
      unless @client.status == PG::CONNECTION_OK
        begin
          raise PG::Error, @client.error_message
        ensure
          @client.finish unless reconnecting?
        end
      end
      # mimic blocking connect behavior
      @client.set_default_encoding unless reconnecting?
      @client
    end
  end
end

#reconnecting?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/pg/em.rb', line 239

def reconnecting?
  @poll_method == :reset_poll
end