class ThriftRack::HttpClientTransport

Attributes

default[RW]
response_headers[RW]

Public Class Methods

new(url, opts = {}) click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 11
def initialize(url, opts = {})
  @headers = {'Content-Type' => 'application/x-thrift'}
  @outbuf = Thrift::Bytes.empty_byte_buffer
  @response_headers = {}
  @url = url
end
new_http(name, max_requests: 100) click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 70
def new_http(name, max_requests: 100)
  http = Net::HTTP::Persistent.new(name: name)
  http.max_requests = max_requests
  http.verify_mode = 0
  http
end

Public Instance Methods

add_headers(headers) click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 22
def add_headers(headers)
  @headers = @headers.merge(headers)
end
flush() click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 26
def flush
  self.response_headers = {}
  uri = URI(@url)
  post = Net::HTTP::Post.new uri.path
  post.body = @outbuf
  post.initialize_http_header(@headers)
  resp = retry_request_with_503{ThriftRack::HttpClientTransport.default.request(uri, post)}
  data = resp.body
  self.response_headers = resp.header
  resp_code = resp.code.to_i
  if resp_code != 200
    if resp_code == 409
      raise ProcessedRequest, @url
    elsif resp_code == 509
      raise ServerDowngradingError, @url
    else
      raise RespCodeError, "#{resp.code} on #{@url} with body #{data}"
    end
  end
  data = Thrift::Bytes.force_binary_encoding(data)
  @inbuf = StringIO.new data
ensure
  @outbuf = Thrift::Bytes.empty_byte_buffer
end
open?() click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 18
def open?; true end
read(sz) click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 19
def read(sz); @inbuf.read sz end
retry_request_with_503() { || ... } click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 51
def retry_request_with_503
  resp = nil
  3.times do |i|
    resp = yield
    return resp unless resp.code.to_i == 503

    sleep(0.1 * i)
    ThriftRack::HttpClientTransport.default.reconnect
  end
  resp
end
write(buf) click to toggle source
# File lib/thrift_rack/http_client_transport.rb, line 20
def write(buf); @outbuf << Thrift::Bytes.force_binary_encoding(buf) end