Class: Mongo::Socket
- Inherits:
-
Object
- Object
- Mongo::Socket
- Includes:
- Socket::Constants
- Defined in:
- lib/mongo/socket.rb,
lib/mongo/socket/ssl.rb,
lib/mongo/socket/tcp.rb,
lib/mongo/socket/unix.rb
Overview
Provides additional data around sockets for the driver’s use.
Defined Under Namespace
Constant Summary collapse
- SSL_ERROR =
Error message for SSL related exceptions.
'MongoDB may not be configured with SSL support'.freeze
- TIMEOUT_ERROR =
Deprecated.
Error message for timeouts on socket calls.
'Socket request timed out'.freeze
- TIMEOUT_PACK =
The pack directive for timeouts.
'l_2'.freeze
- WRITE_CHUNK_SIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Write data to the socket in chunks of this size.
65536
Instance Attribute Summary collapse
-
#family ⇒ Integer
readonly
Family The type of host family.
-
#options ⇒ Hash
readonly
The options.
-
#socket ⇒ Socket
readonly
Socket The wrapped socket.
-
#timeout ⇒ Float
readonly
Timeout The socket timeout.
Instance Method Summary collapse
-
#alive? ⇒ true, false
deprecated
Deprecated.
Use #connectable? on the connection instead.
-
#close ⇒ true
Close the socket.
- #connectable? ⇒ true deprecated Deprecated.
-
#eof? ⇒ Boolean
Tests if this socket has reached EOF.
-
#gets(*args) ⇒ Object
Delegates gets to the underlying socket.
-
#read(length) ⇒ Object
Will read all data from the socket for the provided number of bytes.
-
#readbyte ⇒ Object
Read a single byte from the socket.
-
#write(*args) ⇒ Integer
Writes data to the socket instance.
Instance Attribute Details
#family ⇒ Integer (readonly)
Returns family The type of host family.
49 50 51 |
# File 'lib/mongo/socket.rb', line 49 def family @family end |
#options ⇒ Hash (readonly)
Returns The options.
55 56 57 |
# File 'lib/mongo/socket.rb', line 55 def @options end |
#socket ⇒ Socket (readonly)
Returns socket The wrapped socket.
52 53 54 |
# File 'lib/mongo/socket.rb', line 52 def socket @socket end |
#timeout ⇒ Float (readonly)
Returns timeout The socket timeout.
58 59 60 |
# File 'lib/mongo/socket.rb', line 58 def timeout @timeout end |
Instance Method Details
#alive? ⇒ true, false
Use #connectable? on the connection instead.
Is the socket connection alive?
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mongo/socket.rb', line 68 def alive? sock_arr = [ @socket ] if Kernel::select(sock_arr, nil, sock_arr, 0) # The eof? call is supposed to return immediately since select # indicated the socket is readable. However, if @socket is an SSL # socket, eof? can block anyway - see RUBY-2140. begin Timeout.timeout(0.1) do eof? end rescue ::Timeout::Error true end else true end end |
#close ⇒ true
Close the socket.
94 95 96 97 |
# File 'lib/mongo/socket.rb', line 94 def close @socket.close rescue nil true end |
#connectable? ⇒ true
For backwards compatibilty only, do not use.
205 206 207 |
# File 'lib/mongo/socket.rb', line 205 def connectable? true end |
#eof? ⇒ Boolean
Tests if this socket has reached EOF. Primarily used for liveness checks.
194 195 196 197 198 |
# File 'lib/mongo/socket.rb', line 194 def eof? @socket.eof? rescue IOError, SystemCallError true end |
#gets(*args) ⇒ Object
Delegates gets to the underlying socket.
109 110 111 |
# File 'lib/mongo/socket.rb', line 109 def gets(*args) handle_errors { @socket.gets(*args) } end |
#read(length) ⇒ Object
Will read all data from the socket for the provided number of bytes. If no data is returned, an exception will be raised.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mongo/socket.rb', line 126 def read(length) handle_errors do data = read_from_socket(length) unless (data.length > 0 || length == 0) raise IOError, "Expected to read > 0 bytes but read 0 bytes" end while data.length < length chunk = read_from_socket(length - data.length) unless (chunk.length > 0 || length == 0) raise IOError, "Expected to read > 0 bytes but read 0 bytes" end data << chunk end data end end |
#readbyte ⇒ Object
Read a single byte from the socket.
151 152 153 |
# File 'lib/mongo/socket.rb', line 151 def readbyte handle_errors { @socket.readbyte } end |
#write(*args) ⇒ Integer
Writes data to the socket instance.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/mongo/socket.rb', line 165 def write(*args) handle_errors do # This method used to forward arguments to @socket.write in a # single call like so: # # @socket.write(*args) # # Turns out, when each buffer to be written is large (e.g. 32 MiB), # this write call would take an extremely long time (20+ seconds) # while using 100% CPU. Splitting the writes into chunks produced # massively better performance (0.05 seconds to write the 32 MiB of # data on the same hardware). Unfortunately splitting the data, # one would assume, results in it being copied, but this seems to be # a much more minor issue compared to CPU cost of writing large buffers. args.each do |buf| buf = buf.to_s i = 0 while i < buf.length chunk = buf[i...i+WRITE_CHUNK_SIZE] @socket.write(chunk) i += WRITE_CHUNK_SIZE end end end end |