Class: ThriftRack::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift_rack/client.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, client_klass, request_id = nil) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
10
# File 'lib/thrift_rack/client.rb', line 4

def initialize(url, client_klass, request_id = nil)
  @request_id = request_id || "no-request"
  @url = url
  @transport = ThriftRack::HttpClientTransport.new(url)
  protocol = protocol_factory.get_protocol(@transport)
  @client = client_klass.new(protocol)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/thrift_rack/client.rb', line 20

def method_missing(method, *params)
  return super unless @client.respond_to?(method)
  self.class_eval do
    define_method method.to_sym do |*args|
      begin
        rpc_id = SecureRandom.uuid
        request_at = Time.now
        @transport.add_headers({"X-Request-ID" => @request_id, "X-Rpc-ID" => rpc_id, "X-Rpc-Func" => method.to_s, "X-From" => ThriftRack::Client.app_name || "unknown"})
        @client.send(method, *args)
      ensure
        end_time = Time.now
        self.logger.info(JSON.dump({
          request_at: request_at.iso8601(6),
          request_id: @request_id,
          rpc_id: rpc_id,
          duration: ((end_time - request_at) * 1000).round(4),
          path: URI(@url).path,
          func: method,
        }))
      end
    end
  end
  self.public_send(method, *params)
end

Class Attribute Details

.app_nameObject

Returns the value of attribute app_name.



58
59
60
# File 'lib/thrift_rack/client.rb', line 58

def app_name
  @app_name
end

.pool_sizeObject

Returns the value of attribute pool_size.



58
59
60
# File 'lib/thrift_rack/client.rb', line 58

def pool_size
  @pool_size
end

Instance Method Details

#loggerObject



12
13
14
# File 'lib/thrift_rack/client.rb', line 12

def logger
  @logger ||= (defined? Rails) ? rails_logger : std_logger
end

#protocol_factoryObject



16
17
18
# File 'lib/thrift_rack/client.rb', line 16

def protocol_factory
  Thrift::CompactProtocolFactory.new
end