thrift_client 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,4 @@
1
1
 
2
+ v0.1.1. Support server_retry_period.
3
+
2
4
  v0.1. First release.
data/lib/thrift_client.rb CHANGED
@@ -2,69 +2,80 @@
2
2
  require 'rubygems'
3
3
  require 'thrift'
4
4
 
5
- class ThriftClient
5
+ class ThriftClient
6
6
 
7
- DEFAULTS = {
7
+ DEFAULTS = {
8
8
  :protocol => Thrift::BinaryProtocol,
9
9
  :transport => Thrift::FramedTransport,
10
- :socket_timeout => 1,
10
+ :socket_timeout => 1,
11
11
  :randomize_server_list => true,
12
12
  :exception_classes => [
13
- IOError,
14
- Thrift::Exception,
15
- Thrift::ProtocolException,
16
- Thrift::ApplicationException,
13
+ IOError,
14
+ Thrift::Exception,
15
+ Thrift::ProtocolException,
16
+ Thrift::ApplicationException,
17
17
  Thrift::TransportException],
18
18
  :raise => true,
19
- :retries => nil
19
+ :retries => nil,
20
+ :server_retry_period => nil
20
21
  }.freeze
21
-
22
+
22
23
  attr_reader :client, :client_class, :server_list, :options
23
24
 
25
+ class NoServersAvailable < StandardError; end
26
+
24
27
  def initialize(client_class, servers, options = {})
25
28
  @options = DEFAULTS.merge(options)
26
- @client_class = client_class
29
+ @client_class = client_class
27
30
  @server_list = Array(servers)
28
31
  @retries = options[:retries] || @server_list.size
29
32
  @server_list = @server_list.sort_by { rand } if @options[:randomize_server_list]
30
-
31
- @attempts = 0
32
- @live_server_list = []
33
+
34
+ @live_server_list = @server_list.dup
35
+ @last_retry = Time.now
33
36
  end
34
-
37
+
38
+ def disconnect!
39
+ @transport.close rescue nil
40
+ @client = nil
41
+ end
42
+
35
43
  private
36
-
44
+
37
45
  def method_missing(*args)
38
- tries ||= @retries
39
46
  connect! unless @client
40
- @client.send(*args)
47
+ @client.send(*args)
41
48
  rescue *@options[:exception_classes]
42
- if tries == 0
49
+ tries ||= @retries
50
+ tries -= 1
51
+ if tries.zero?
43
52
  raise if @options[:raise]
44
53
  else
45
54
  disconnect!
46
- tries -= 1
47
55
  retry
48
56
  end
49
- end
57
+ rescue NoServersAvailable
58
+ raise if @options[:raise]
59
+ end
50
60
 
51
61
  def connect!
52
62
  server = next_server.to_s.split(":")
53
63
  raise ArgumentError, 'Servers must be in the form "host:port"' if server.size != 2
54
-
64
+
55
65
  @transport = @options[:transport].new(
56
66
  Thrift::Socket.new(server.first, server.last.to_i, @options[:socket_timeout]))
57
67
  @transport.open
58
68
  @client = @client_class.new(@options[:protocol].new(@transport, false))
59
- end
60
-
61
- def disconnect!
62
- @transport.close rescue nil
63
- @client = nil
64
- end
65
-
69
+ end
70
+
66
71
  def next_server
67
- @live_server_list = @server_list.dup if @live_server_list.empty?
72
+ if @live_server_list.empty?
73
+ if @options[:server_retry_period] and Time.now < @last_retry + @options[:server_retry_period]
74
+ raise NoServersAvailable, "No live servers in #{@server_list.inspect} since #{@last_retry.inspect}."
75
+ end
76
+ @last_retry = Time.now
77
+ @live_server_list = @server_list.dup
78
+ end
68
79
  @live_server_list.pop
69
- end
80
+ end
70
81
  end
data/test/test_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  require 'test/unit'
3
- require "#{File.expand_path(File.dirname(__FILE__))}/../lib/thrift_client"
3
+ $LOAD_PATH << "#{File.expand_path(File.dirname(__FILE__))}/../lib"
4
+ require 'thrift_client'
4
5
  require 'scribe'
5
6
  begin; require 'ruby-debug'; rescue LoadError; end
@@ -5,7 +5,7 @@ class ThriftClientTest < Test::Unit::TestCase
5
5
  @entry = [ScribeThrift::LogEntry.new(:message => "something", :category => "thrift_client")]
6
6
  @servers = ["127.0.0.1:1461", "127.0.0.1:1462", "127.0.0.1:1463"]
7
7
  end
8
-
8
+
9
9
  def test_live_server
10
10
  assert_nothing_raised do
11
11
  ThriftClient.new(ScribeThrift::Client, @servers.last).Log(@entry)
@@ -17,13 +17,13 @@ class ThriftClientTest < Test::Unit::TestCase
17
17
  ThriftClient.new(ScribeThrift::Client, @servers, :randomize_server_list => false).Log(@entry)
18
18
  end
19
19
  end
20
-
20
+
21
21
  def test_dont_raise
22
22
  assert_nothing_raised do
23
23
  ThriftClient.new(ScribeThrift::Client, @servers.first, :raise => false).Log(@entry)
24
- end
24
+ end
25
25
  end
26
-
26
+
27
27
  def test_random_server_list
28
28
  @lists = []
29
29
  @lists << ThriftClient.new(ScribeThrift::Client, @servers).server_list while @lists.size < 10
@@ -35,16 +35,24 @@ class ThriftClientTest < Test::Unit::TestCase
35
35
  10.times { ThriftClient.new(ScribeThrift::Client, @servers).Log(@entry) }
36
36
  end
37
37
  end
38
-
38
+
39
39
  def test_lazy_connection
40
40
  assert_nothing_raised do
41
41
  ThriftClient.new(ScribeThrift::Client, @servers[0,2])
42
- end
42
+ end
43
43
  end
44
-
44
+
45
45
  def test_no_servers_eventually_raise
46
46
  assert_raises(Thrift::TransportException) do
47
47
  ThriftClient.new(ScribeThrift::Client, @servers[0,2]).Log(@entry)
48
48
  end
49
- end
49
+ end
50
+
51
+ def test_retry_period
52
+ client = ThriftClient.new(ScribeThrift::Client, @servers[0,2], :server_retry_period => 1)
53
+ assert_raises(Thrift::TransportException) { client.Log(@entry) }
54
+ assert_raises(ThriftClient::NoServersAvailable) { client.Log(@entry) }
55
+ sleep 1
56
+ assert_raises(Thrift::TransportException) { client.Log(@entry) }
57
+ end
50
58
  end
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{thrift_client}
5
- s.version = "0.1"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0.8") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Evan Weaver"]
9
9
  s.cert_chain = ["/Users/eweaver/p/configuration/gem_certificates/evan_weaver-original-public_cert.pem"]
10
- s.date = %q{2009-10-13}
10
+ s.date = %q{2009-10-14}
11
11
  s.description = %q{A Thrift client wrapper that encapsulates some common failover behavior.}
12
12
  s.email = %q{}
13
13
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "lib/thrift_client.rb"]
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thrift_client
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Weaver
@@ -30,7 +30,7 @@ cert_chain:
30
30
  yZ0=
31
31
  -----END CERTIFICATE-----
32
32
 
33
- date: 2009-10-13 00:00:00 -07:00
33
+ date: 2009-10-14 00:00:00 -07:00
34
34
  default_executable:
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
metadata.gz.sig CHANGED
Binary file