Class: ThriftRack::HttpClientTransport

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

Defined Under Namespace

Classes: RespCodeError

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HttpClientTransport.



7
8
9
10
11
# File 'lib/thrift_rack/http_client_transport.rb', line 7

def initialize(url, opts = {})
  @headers = {'Content-Type' => 'application/x-thrift'}
  @outbuf = Thrift::Bytes.empty_byte_buffer
  @url = url
end

Class Attribute Details

.defaultObject

Returns the value of attribute default.



50
51
52
# File 'lib/thrift_rack/http_client_transport.rb', line 50

def default
  @default
end

Instance Method Details

#add_headers(headers) ⇒ Object



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

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

#flushObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/thrift_rack/http_client_transport.rb', line 22

def flush
  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
  raise RespCodeError.new("#{resp.code} on #{@url} with body #{data}") unless resp.code.to_i == 200
  data = Thrift::Bytes.force_binary_encoding(data)
  @inbuf = StringIO.new data
ensure
  @outbuf = Thrift::Bytes.empty_byte_buffer
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?; true end

#read(sz) ⇒ Object



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

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

#retry_request_with_503Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/thrift_rack/http_client_transport.rb', line 36

def retry_request_with_503
  resp = nil
  3.times do |i|
    resp = yield
    if resp.code.to_i != 503
      return resp
    end
    sleep(0.1 * i)
    ThriftRack::HttpClientTransport.default.reconnect
  end
  resp
end

#write(buf) ⇒ Object



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

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