# File lib/monga/connections/kgio_connection.rb, line 86 def primary? @primary || false end
class Monga::Connections::KGIOConnection
Constants
- TO_RECV
Public Class Methods
connect(host, port, timeout)
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 8 def self.connect(host, port, timeout) new(host, port, timeout) end
new(host, port, timeout)
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 12 def initialize(host, port, timeout) @host, @port, @timout = host, port, timeout @connected = true @buffer = Buffer.new @tmp = "" end
Public Instance Methods
close()
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 104 def close @socket = nil @primary = false @connected = false end
connected?()
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 19 def connected? socket unless @connected @connected end
is_master?() { |primary ? :primary : :secondary| ... }
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 90 def is_master? req = Monga::Protocol::Query.new(self, "admin", "$cmd", query: {"isMaster" => 1}, limit: 1) command = req.command request_id = req.request_id socket.kgio_write command read_socket message = @buffer.first @primary = message.last.first["ismaster"] yield @primary ? :primary : :secondary rescue => e close yield nil end
primary?()
click to toggle source
read_socket()
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 65 def read_socket while @buffer.buffer_size < 4 unless socket.kgio_read(TO_RECV, @tmp) raise Errno::ECONNREFUSED.new "Nil was return. Closing connection" end @buffer.append(@tmp) size = @buffer.buffer_size if size >= 4 length = ::BinUtils.get_int32_le(@buffer.buffer) torecv = length - size if torecv > 0 socket.read(torecv, @tmp) @buffer.append(@tmp) end end end end
responses()
click to toggle source
Fake answer, as far as we are blocking, but we should support API
# File lib/monga/connections/kgio_connection.rb, line 40 def responses 0 end
send_command(msg, request_id=nil, &cb)
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 44 def send_command(msg, request_id=nil, &cb) raise Errno::ECONNREFUSED, "Connection Refused" unless socket socket.kgio_write msg if cb read_socket message = @buffer.first rid = message[2] fail "Returned Request Id is not equal to sended one (#{rid} != #{request_id}), #{message}" if rid != request_id cb.call(message) end rescue Errno::ECONNREFUSED, Errno::EPIPE => e close if cb err = Monga::Exceptions::Disconnected.new("Disconnected from #{@host}:#{@port}, #{e.message}") cb.call(err) end end
socket()
click to toggle source
# File lib/monga/connections/kgio_connection.rb, line 24 def socket @socket ||= begin sock = Kgio::TCPSocket.new(@host, @port) # MacOS doesn't support autopush sock.kgio_autopush = true unless RUBY_PLATFORM['darwin'] # check connection sock.kgio_write "" @connected = true sock end rescue => e nil end