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

Defined in:
lib/pg/em.rb

Instance Method Summary collapse

Instance Method Details

#initialize(client, deferrable, poll_method) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/pg/em.rb', line 245

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



271
272
273
# File 'lib/pg/em.rb', line 271

def notify_readable
  poll_connection_and_check
end

#notify_writableObject



267
268
269
# File 'lib/pg/em.rb', line 267

def notify_writable
  poll_connection_and_check
end

#poll_connection_and_checkObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/pg/em.rb', line 275

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
    success = @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
      unless Encoding.default_internal.nil? || reconnecting?
        begin
          @client.internal_encoding = Encoding.default_internal
        rescue EncodingError
          warn "warning: Failed to set the default_internal encoding to #{Encoding.default_internal}: '#{@client.error_message}'"
        end
      end
      @client
    end
  end
end

#reconnecting?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/pg/em.rb', line 263

def reconnecting?
  @poll_method == :reset_poll
end