Class: ThriftRack::HttpClientTransport

Inherits:
Thrift::BaseTransport
  • Object
show all
Defined in:
lib/thrift_rack/http_client_transport.rb

Defined Under Namespace

Classes: ProcessedRequest, RespCodeError, ServerDowngradingError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts = {}) ⇒ HttpClientTransport

Returns a new instance of HttpClientTransport.



11
12
13
14
15
16
# 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

Class Attribute Details

.defaultObject

Returns the value of attribute default.



64
65
66
# File 'lib/thrift_rack/http_client_transport.rb', line 64

def default
  @default
end

Instance Attribute Details

#response_headersObject

Returns the value of attribute response_headers.



10
11
12
# File 'lib/thrift_rack/http_client_transport.rb', line 10

def response_headers
  @response_headers
end

Class Method Details

.new_http(name, max_requests: 100) ⇒ Object



70
71
72
73
74
75
# 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

Instance Method Details

#add_headers(headers) ⇒ Object



22
23
24
# File 'lib/thrift_rack/http_client_transport.rb', line 22

def add_headers(headers)
  @headers = @headers.merge(headers)
end

#flushObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)


18
# File 'lib/thrift_rack/http_client_transport.rb', line 18

def open?; true end

#read(sz) ⇒ Object



19
# File 'lib/thrift_rack/http_client_transport.rb', line 19

def read(sz); @inbuf.read sz end

#retry_request_with_503Object



51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



20
# File 'lib/thrift_rack/http_client_transport.rb', line 20

def write(buf); @outbuf << Thrift::Bytes.force_binary_encoding(buf) end