Class: THTP::Client

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/thtp/client.rb,
lib/thtp/client/middleware.rb,
lib/thtp/client/instrumentation.rb

Overview

A thrift-over-HTTP client library implementing persistent connections and extensibility via middlewares

Defined Under Namespace

Modules: Instrumentation, Middleware Classes: Dispatcher

Instance Method Summary collapse

Methods included from Utils

#args_class, #canonical_name, #deserialize, #deserialize_buffer, #deserialize_stream, #elapsed_ms, #extract_rpcs, #get_time, #jsonify, #result_class, #serialize, #serialize_buffer, #serialize_stream

Constructor Details

#initialize(service, protocol: Thrift::CompactProtocol, host: '0.0.0.0', port: nil, ssl: false, open_timeout: 1, rpc_timeout: 15, pool_size: 5, pool_timeout: 5) ⇒ Client

Returns a new instance of Client.

Parameters:

  • service (Class)

    The Thrift service whose schema to use for de/serialisation



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/thtp/client.rb', line 96

def initialize(service, protocol: Thrift::CompactProtocol,
               host: '0.0.0.0', port: nil, ssl: false,
               open_timeout: 1, rpc_timeout: 15,
               pool_size: 5, pool_timeout: 5)
  uri_class = ssl ? URI::HTTPS : URI::HTTP
  # set up HTTP connections in a thread-safe pool
  connection = ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
    Patron::Session.new(
      base_url: uri_class.build(host: host, port: port, path: "/#{canonical_name(service)}/"),
      connect_timeout: open_timeout,
      timeout: rpc_timeout,
      headers: {
        'Content-Type' => Encoding.content_type(protocol),
        'User-Agent' => self.class.name,
      },
    )
  end
  # allow middleware insertion for purposes such as instrumentation or validation
  @stack = MiddlewareStack.new(service, Dispatcher.new(service, connection, protocol))
  extract_rpcs(service).each { |rpc| define_singleton_method(rpc, &@stack.method(rpc)) }
end

Instance Method Details

#use(middleware_class, *middleware_args) ⇒ Object

delegate to RPC dispatcher stack



119
120
121
# File 'lib/thtp/client.rb', line 119

def use(middleware_class, *middleware_args)
  @stack.use(middleware_class, *middleware_args)
end