Class: Thrifter::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/thrifter.rb

Defined Under Namespace

Classes: DirectDispatcher, Dispatcher

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ Client

Returns a new instance of Client.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/thrifter.rb', line 146

def initialize(client = nil)
  if !client
    fail ArgumentError, 'config.uri not set!' unless config.uri

    @uri = URI(config.uri)

    fail ArgumentError, 'URI did not contain port' unless @uri.port
  end

  @pool = InstrumentedPool.new(statsd: config.statsd, size: config.pool_size.to_i, timeout: config.pool_timeout.to_f) do
    stack = MiddlewareStack.new

    stack.use config.stack

    # Insert metrics here so metrics are as close to the network
    # as possible. This excludes time in any middleware an
    # application may have configured.
    stack.use ClientMetrics, config.statsd
    stack.use RpcMetrics, config.statsd

    if client
      stack.use DirectDispatcher, client
    else
      socket = Thrift::Socket.new @uri.host, @uri.port, config.rpc_timeout.to_f
      transport = config.transport.new socket
      protocol = config.protocol.new transport

      stack.use Dispatcher, transport, client_class.new(protocol), config
    end

    stack.finalize!
  end
end

Class Attribute Details

.configObject

Returns the value of attribute config.



117
118
119
# File 'lib/thrifter.rb', line 117

def config
  @config
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



122
123
124
# File 'lib/thrifter.rb', line 122

def configure
  yield config
end

.inherited(base) ⇒ Object

NOTE: the inherited hook is better than doing singleton methods for config. This works when Thrifter is used like a struct MyClient = Thrifter.build(MyService) or like delegate class MyClient < Thrifter.build(MyService). The end result is each class has it’s own configuration instance.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/thrifter.rb', line 131

def inherited(base)
  base.config = Configuration.new
  base.configure do |config|
    config.keep_alive = true
    config.transport = Thrift::FramedTransport
    config.protocol = Thrift::BinaryProtocol
    config.pool_size = 12
    config.pool_timeout = 2
    config.rpc_timeout = 2
    config.statsd = NullStatsd.new
    config.stack = MiddlewareStack.new
  end
end