Class: AbstractThriftClient

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

Direct Known Subclasses

ThriftClient

Constant Summary collapse

DISCONNECT_ERRORS =
[
  IOError,
  Thrift::ProtocolException,
  Thrift::TransportException
]
APPLICATION_ERRORS =
[
  Thrift::ApplicationException
]
DEFAULTS =

DEFAULT_BEFORE_METHOD = Proc.new {|method_name| puts “prepare call #{method_name} method.”} DEFAULT_AFTER_METHOD = Proc.new {|method_name| puts “called #{method_name} method.”} DEFAULT_ON_EXCEPTION = Proc.new {|method_name, e| puts “call #{method_name} method ouccur exception.”; raise e}

{
  :protocol => Thrift::CompactProtocol,
  :transport => Thrift::Socket,
  :transport_wrapper => Thrift::FramedTransport,
  :disconnect_exception_classes => DISCONNECT_ERRORS,
  :application_exception_classes => APPLICATION_ERRORS,
  :size => 1,
  :timeout => nil,
  :client_class => nil,
  :servers => "127.0.0.1:9090",
  :multiplexed => false,
  :retry => 0,
  :before_method => nil,
  :after_method => nil,
  :on_exception => nil
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AbstractThriftClient

Returns a new instance of AbstractThriftClient.



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

def initialize(options = {})
  user_options = initialize_options(options)

  servers = parse_servers(options["servers"])

  #initialize options
  @options = DEFAULTS.merge(user_options)

  #initialize servers info
  @server_list = servers.collect do |s|
    Server.new(s, @options)
  end.sort_by { rand }

  @callbacks = {}

  #initialize client methods
  @client_methods = []
  @options[:client_class].instance_methods.each do |method_name|
    if method_name != 'send_message' && method_name =~ /^send_(.*)$/
      instance_eval("def #{$1}(*args); handle_method(:'#{$1}', *args); end", __FILE__, __LINE__)
      @client_methods << $1
    end
  end
  @retry = @options[:retry]
  @state = 1
end

Instance Method Details

#add_callback(callback_type, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/thrift_client/abstract_thrift_client.rb', line 71

def add_callback(callback_type, &block)
  case callback_type
  when :before_method, :after_method, :on_exception
    @callbacks[callback_type] ||= []
    @callbacks[callback_type].push(block)
  else
    raise ArgumentError.new("Unknown callback type #{callback_type},only support before_method|after_method|on_exception")
  end
  return self
end

#destroyObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/thrift_client/abstract_thrift_client.rb', line 122

def destroy
  @state = -1
  @server_list.each { |server|
    begin
      server.destroy if server
    rescue Exception => e
      puts "destroy server #{server} error, #{e.message}, #{e.backtrace}"
    end
  }
end

#handle_method(method_name, *args) ⇒ Object

retry two times



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/thrift_client/abstract_thrift_client.rb', line 83

def handle_method(method_name, *args)
  check_state
  server = next_server
  do_callbacks(:before_method, method_name)
  result = server.with do |client|
    tries = 0
    begin
      ensure_connected client
      client.send(method_name, *args)
    rescue *@options[:application_exception_classes] => e
      if(@callbacks[:on_exception] && @callbacks[:on_exception].length > 0)
        do_callbacks(:on_exception, method_name, e)
      else
        raise e
      end
    rescue *@options[:disconnect_exception_classes] => e
      # if e instance_of? Thrift::TransportException && e.type == Thrift::TransportException::TIMED_OUT
      begin
        if client.connected?
          client.disconnect
        end
      rescue Exception => e
        puts "disconnect error"
      end
      if tries < @retry # retry two times
        tries += 1
        retry
      end
      if(@callbacks[:on_exception] && @callbacks[:on_exception].length > 0)
        do_callbacks(:on_exception, method_name, e)
      else
        raise e
      end
    end
  end
  do_callbacks(:after_method, method_name)
  result
end