Class: Mongo::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Loggable
Defined in:
lib/mongo/connection.rb

Overview

This class models the socket connections and their behavior.

Since:

  • 2.0.0

Defined Under Namespace

Classes: InvalidMessageSize

Constant Summary

TIMEOUT =

The default time in seconds to timeout a connection attempt.

Since:

  • 2.0.0

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log

Constructor Details

#initialize(server, options = {}) ⇒ Connection

Initialize a new socket connection from the client to the server.

Examples:

Create the connection.

Connection.new(server)

Parameters:

  • server (Mongo::Server)

    The server the connection is for.

  • options (Hash) (defaults to: {})

    The connection options.

Since:

  • 2.0.0



130
131
132
133
134
135
136
137
# File 'lib/mongo/connection.rb', line 130

def initialize(server, options = {})
  @address = server.address
  @options = options.freeze
  @server = server
  @ssl_options = options.reject { |k, v| !k.to_s.start_with?('ssl') }
  @socket = nil
  setup_authentication!
end

Instance Attribute Details

#addressMongo::Server::Address (readonly)

Returns address The address to connect to.

Returns:

Since:

  • 2.0.0



34
35
36
# File 'lib/mongo/connection.rb', line 34

def address
  @address
end

#authenticatorMongo::Auth::CR, ... (readonly)

Returns authenticator The authentication strategy.

Returns:

Since:

  • 2.0.0



31
32
33
# File 'lib/mongo/connection.rb', line 31

def authenticator
  @authenticator
end

#optionsHash (readonly)

Returns options The passed in options.

Returns:

  • (Hash)

    options The passed in options.

Since:

  • 2.0.0



37
38
39
# File 'lib/mongo/connection.rb', line 37

def options
  @options
end

Instance Method Details

#authenticated?true, false

Is this connection authenticated. Will return true if authorization details were provided and authentication passed.

Examples:

Is the connection authenticated?

connection.authenticated?

Returns:

  • (true, false)

    If the connection is authenticated.

Since:

  • 2.0.0



53
54
55
# File 'lib/mongo/connection.rb', line 53

def authenticated?
  !!@authenticated
end

#connect!true

Note:

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

Tell the underlying socket to establish a connection to the host.

Examples:

Connect to the host.

connection.connect!

Returns:

  • (true)

    If the connection succeeded.

Since:

  • 2.0.0



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mongo/connection.rb', line 68

def connect!
  unless socket
    @socket = address.socket(timeout, ssl_options)
    @socket.connect!
    if authenticator # @todo: durran: and auth enabled?
      authenticator.(self)
      @authenticated = true
    end
  end
  true
end

#disconnect!true

Note:

This method mutates the connection by setting the socket to nil if the closing succeeded.

Disconnect the connection.

Examples:

Disconnect from the host.

connection.disconnect!

Returns:

  • (true)

    If the disconnect succeeded.

Since:

  • 2.0.0



91
92
93
94
95
96
97
98
# File 'lib/mongo/connection.rb', line 91

def disconnect!
  if socket
    socket.close
    @socket = nil
    @authenticated = false
  end
  true
end

#dispatch(messages) ⇒ Protocol::Reply

Note:

This method is named dispatch since 'send' is a core Ruby method on all objects.

Dispatch the provided messages to the connection. If the last message requires a response a reply will be returned.

Examples:

Dispatch the messages.

connection.dispatch([ insert, command ])

Parameters:

  • messages (Array<Message>)

    The messages to dispatch.

Returns:

Since:

  • 2.0.0



114
115
116
117
118
119
# File 'lib/mongo/connection.rb', line 114

def dispatch(messages)
  log(:debug, 'MONGODB', messages) do |msgs|
    write(msgs)
    msgs.last.replyable? ? read : nil
  end
end

#timeoutFloat

Get the connection timeout.

Examples:

Get the connection timeout.

connection.timeout

Returns:

  • (Float)

    The connection timeout in seconds.

Since:

  • 2.0.0



147
148
149
# File 'lib/mongo/connection.rb', line 147

def timeout
  @timeout ||= options[:socket_timeout] || TIMEOUT
end