Class: ThriftRack::HttpClientTransport
- Inherits:
-
Thrift::BaseTransport
- Object
- Thrift::BaseTransport
- ThriftRack::HttpClientTransport
- Defined in:
- lib/thrift_rack/http_client_transport.rb
Defined Under Namespace
Classes: RespCodeError
Class Attribute Summary collapse
-
.default ⇒ Object
Returns the value of attribute default.
Instance Method Summary collapse
- #add_headers(headers) ⇒ Object
- #flush ⇒ Object
-
#initialize(url, opts = {}) ⇒ HttpClientTransport
constructor
A new instance of HttpClientTransport.
- #open? ⇒ Boolean
- #read(sz) ⇒ Object
- #retry_request_with_503 ⇒ Object
- #write(buf) ⇒ Object
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
.default ⇒ Object
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 |
#flush ⇒ Object
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
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_503 ⇒ Object
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 |