Method: Mongo::Server::Connection#connect!

Defined in:
lib/mongo/server/connection.rb

#connect!true

Note:

This method mutates the connection object by setting a socket if one previously did not exist.

Establishes a network connection to the target address.

If the connection is already established, this method does nothing.

Examples:

Connect to the host.

connection.connect!

Returns:

  • (true)

    If the connection succeeded.

Since:

  • 2.0.0



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mongo/server/connection.rb', line 203

def connect!
  if error?
    raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is perished. Reconnecting closed or errored connections is no longer supported"
  end

  if closed?
    raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is closed. Reconnecting closed or errored connections is no longer supported"
  end

  unless @socket
    # When @socket is assigned, the socket should have handshaken and
    # authenticated and be usable.
    @socket, @description, @compressor = do_connect

    if server.load_balancer?
      if Lint.enabled?
        unless service_id
          raise Error::InternalDriverError, "The connection is to a load balancer and it must have service_id set here, but does not"
        end
      end
      @generation = connection_pool.generation_manager.generation(service_id: service_id)
    end

    publish_cmap_event(
      Monitoring::Event::Cmap::ConnectionReady.new(address, id)
    )

    @close_event_published = false
  end
  true
end