Class: AbstractThriftClient

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift_client/abstract_thrift_client.rb

Direct Known Subclasses

ThriftClient

Defined Under Namespace

Classes: Server

Constant Summary

DISCONNECT_ERRORS =
[
  IOError,
  Thrift::Exception,
  Thrift::ApplicationException,
  Thrift::TransportException
]
DEFAULT_WRAPPED_ERRORS =
[
  Thrift::ApplicationException,
  Thrift::TransportException,
]
DEFAULTS =
{
  :protocol => Thrift::BinaryProtocol,
  :protocol_extra_params => [],
  :transport => Thrift::Socket,
  :transport_wrapper => Thrift::FramedTransport,
  :raise => true,
  :defaults => {},
  :exception_classes => DISCONNECT_ERRORS,
  :retries => 0,
  :server_retry_period => 1,
  :server_max_requests => nil,
  :retry_overrides => {},
  :wrapped_exception_classes => DEFAULT_WRAPPED_ERRORS,
  :connect_timeout => 0.1,
  :timeout => 1,
  :timeout_overrides => {}
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AbstractThriftClient) initialize(client_class, servers, options = {})



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/thrift_client/abstract_thrift_client.rb', line 48

def initialize(client_class, servers, options = {})
  @options = DEFAULTS.merge(options)
  @options[:server_retry_period] ||= 0
  @client_class = client_class
  @server_list = Array(servers).collect{|s| Server.new(s)}.sort_by { rand }
  @current_server = @server_list.first

  @client_methods = []
  @client_class.instance_methods.each do |method_name|
    if method_name != 'send_message' && method_name =~ /^send_(.*)$/
      instance_eval("def #{$1}(*args); handled_proxy(:'#{$1}', *args); end", __FILE__, __LINE__)
      @client_methods << $1
    end
  end
  @request_count = 0
  @options[:wrapped_exception_classes].each do |exception_klass|
    name = exception_klass.to_s.split('::').last
    begin
      @client_class.const_get(name)
    rescue NameError
      @client_class.const_set(name, Class.new(exception_klass))
    end
  end
end

Instance Attribute Details

- (Object) client (readonly)

Returns the value of attribute client



46
47
48
# File 'lib/thrift_client/abstract_thrift_client.rb', line 46

def client
  @client
end

- (Object) client_class (readonly)

Returns the value of attribute client_class



46
47
48
# File 'lib/thrift_client/abstract_thrift_client.rb', line 46

def client_class
  @client_class
end

- (Object) client_methods (readonly)

Returns the value of attribute client_methods



46
47
48
# File 'lib/thrift_client/abstract_thrift_client.rb', line 46

def client_methods
  @client_methods
end

- (Object) current_server (readonly)

Returns the value of attribute current_server



46
47
48
# File 'lib/thrift_client/abstract_thrift_client.rb', line 46

def current_server
  @current_server
end

- (Object) options (readonly)

Returns the value of attribute options



46
47
48
# File 'lib/thrift_client/abstract_thrift_client.rb', line 46

def options
  @options
end

- (Object) server_list (readonly)

Returns the value of attribute server_list



46
47
48
# File 'lib/thrift_client/abstract_thrift_client.rb', line 46

def server_list
  @server_list
end

Instance Method Details

- (Object) connect!

Force the client to connect to the server. Not necessary to be called as the connection will be made on the first RPC method call.



80
81
82
83
84
85
# File 'lib/thrift_client/abstract_thrift_client.rb', line 80

def connect!
  @current_server = next_live_server
  @connection = Connection::Factory.create(@options[:transport], @options[:transport_wrapper], @current_server.connection_string, @options[:connect_timeout])
  @connection.connect!
  @client = @client_class.new(@options[:protocol].new(@connection.transport, *@options[:protocol_extra_params]))
end

- (Object) disconnect!



87
88
89
90
91
92
# File 'lib/thrift_client/abstract_thrift_client.rb', line 87

def disconnect!
  @connection.close rescue nil #TODO
  @client = nil
  @current_server = nil
  @request_count = 0
end

- (Object) inspect



73
74
75
# File 'lib/thrift_client/abstract_thrift_client.rb', line 73

def inspect
  "<#{self.class}(#{client_class}) @current_server=#{@current_server}>"
end