Class: BatsdDash::ConnectionPool::Client

Inherits:
EventMachine::Synchrony::TCPSocket
  • Object
show all
Defined in:
lib/batsd-dash/connection_pool.rb

Instance Method Summary collapse

Instance Method Details

#async_available_listObject



72
73
74
# File 'lib/batsd-dash/connection_pool.rb', line 72

def async_available_list
  write_and_read_json "available"
end

#async_values(statistic, range) ⇒ Object



76
77
78
# File 'lib/batsd-dash/connection_pool.rb', line 76

def async_values(statistic, range)
  write_and_read_json "values #{statistic} #{range[0]} #{range[1]}"
end

#unbind(reason = nil) ⇒ Object



80
81
82
83
84
# File 'lib/batsd-dash/connection_pool.rb', line 80

def unbind(reason = nil)
  super(reason)

  ConnectionPool::start_reconnect_timer
end

#write_and_read_json(request) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/batsd-dash/connection_pool.rb', line 47

def write_and_read_json(request)
  EventMachine::DefaultDeferrable.new.tap do |df|
    response = String.new
    parser = Yajl::Parser.new
    parser.on_parse_complete = Proc.new { |json| df.succeed(json) }

    begin
      write request

      # keep reading till we hit new line
      while response[-1] != "\n"
        response << read(1)
      end

      parser.parse response

    rescue Exception => e
      # TODO handle broken pipe
      #unbind if Errno::EPIPE === e

      df.fail(e)
    end
  end
end