Class: Aerospike::Connection
- Inherits:
-
Object
- Object
- Aerospike::Connection
- Defined in:
- lib/aerospike/cluster/connection.rb
Overview
:nodoc:
Instance Method Summary collapse
- #close ⇒ Object
- #connect(host, port, timeout) ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(host, port, timeout = 30) ⇒ Connection
constructor
A new instance of Connection.
- #read(buffer, length) ⇒ Object
- #timeout=(timeout) ⇒ Object
- #valid? ⇒ Boolean
- #write(buffer, length) ⇒ Object
Constructor Details
#initialize(host, port, timeout = 30) ⇒ Connection
Returns a new instance of Connection.
25 26 27 28 29 30 31 32 33 |
# File 'lib/aerospike/cluster/connection.rb', line 25 def initialize(host, port, timeout = 30) connect(host, port, timeout).tap do |socket| @socket = socket @timeout = timeout end self end |
Instance Method Details
#close ⇒ Object
94 95 96 97 |
# File 'lib/aerospike/cluster/connection.rb', line 94 def close @socket.close if @socket @socket = nil end |
#connect(host, port, timeout) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aerospike/cluster/connection.rb', line 35 def connect(host, port, timeout) socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) @sockaddr = Socket.sockaddr_in(port, host) begin socket.connect_nonblock(@sockaddr) socket rescue IO::WaitWritable, Errno::EINPROGRESS # Block until the socket is ready, then try again IO.select(nil, [socket], nil, timeout.to_f) begin socket.connect_nonblock(@sockaddr) rescue Errno::EISCONN rescue => e socket.close raise e end socket end end |
#connected? ⇒ Boolean
86 87 88 |
# File 'lib/aerospike/cluster/connection.rb', line 86 def connected? @socket != nil end |
#read(buffer, length) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/aerospike/cluster/connection.rb', line 70 def read(buffer, length) total = 0 while total < length begin bytes = @socket.recv_nonblock(length - total) buffer.write_binary(bytes, total) if bytes.bytesize > 0 total += bytes.bytesize rescue IO::WaitReadable, Errno::EAGAIN IO.select([@socket], nil) retry rescue => e Aerospike::Exceptions::Connection.new("#{e}") end end end |
#timeout=(timeout) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/aerospike/cluster/connection.rb', line 99 def timeout=(timeout) if timeout > 0 && timeout != @timeout @timeout = timeout if IO.select([@socket], [@socket], [@socket], timeout.to_f) begin # Verify there is now a good connection @socket.connect_nonblock(@sockaddr) rescue Errno::EISCONN # operation successful rescue => e # An unexpected exception was raised - the connection is no good. close raise Aerospike::Exceptions::Connection.new("#{e}") end end end end |
#valid? ⇒ Boolean
90 91 92 |
# File 'lib/aerospike/cluster/connection.rb', line 90 def valid? @socket != nil end |
#write(buffer, length) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/aerospike/cluster/connection.rb', line 55 def write(buffer, length) total = 0 while total < length begin written = @socket.write_nonblock(buffer.read(total, length - total)) total += written rescue IO::WaitWritable, Errno::EAGAIN IO.select(nil, [@socket]) retry rescue => e Aerospike::Exceptions::Connection.new("#{e}") end end end |