leela_client 0.0.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/leela_client.rb CHANGED
@@ -20,5 +20,3 @@ require "leela_client/metrics"
20
20
  require "leela_client/ring"
21
21
  require "leela_client/transport"
22
22
  require "leela_client/version"
23
-
24
- module LeelaClient; end
@@ -24,11 +24,10 @@ module LeelaClient
24
24
 
25
25
  def transport(servers)
26
26
  servers = servers.map do |addr|
27
- host, port = addr.split(":", 2)
28
- [Resolv.getaddress(host), (port || 6968).to_i]
27
+ [Resolv.getaddress(addr), addr]
29
28
  end
30
29
 
31
- UDPTransport.new MD5Ring.from_list(servers)
30
+ Transport.new(MD5Ring.from_list(servers))
32
31
  end
33
32
  end
34
33
  end
@@ -23,7 +23,7 @@ module LeelaClient
23
23
 
24
24
  def serialize
25
25
  size = @key.size
26
- now = @timestamp.to_f.to_s
26
+ time = @timestamp.to_f.to_s
27
27
  if (@value.integer?)
28
28
  value = @value.to_f.to_s
29
29
  elsif (@value.nan?)
@@ -36,7 +36,12 @@ module LeelaClient
36
36
  value = @value.to_s
37
37
  end
38
38
 
39
- "#{@type} #{size}|#{@key} #{value} #{now};"
39
+ "#{@type} #{size}|#{@key} #{value} #{time};"
40
+ end
41
+
42
+ def serialize_json
43
+ time = @timestamp.to_f.to_s
44
+ {:name => @key, :type => @type, :value => @value, :timestamp => time}
40
45
  end
41
46
 
42
47
  def size
@@ -14,12 +14,70 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
+ require "cgi"
18
+ require "net/http"
19
+ require "json"
20
+
17
21
  module LeelaClient
18
- class UDPTransport
19
- MAXPAYLOAD = 1472
22
+
23
+ class Transport
20
24
 
21
25
  def initialize(ring)
22
26
  @ring = ring
27
+ end
28
+
29
+ def send(metrics, opts={:method => :udp})
30
+ if (opts[:method] == :http)
31
+ HTTPTransport.new(@ring, opts).send(metrics)
32
+ elsif (opts[:method] == :udp)
33
+ UDPTransport.new(@ring, opts).send(metrics)
34
+ else
35
+ raise(RuntimeError.new "unknown method: #{opts[:method]}")
36
+ end
37
+ end
38
+ end
39
+
40
+ class HTTPTransport
41
+
42
+ def initialize(ring, opts)
43
+ @ring = ring
44
+ @opts = opts
45
+ end
46
+
47
+ def send(metrics)
48
+ sent = 0
49
+ proto = @opts[:ssl] ? "https" : "http"
50
+ port = @opts[:port] || (opts[:ssl] ? 443 : 80)
51
+ LeelaClient::LoadBalancer.group(@ring, metrics).each do |addr, ms|
52
+ ms.each do |m|
53
+ uri = URI("#{proto}://#{addr[1]}:#{port}/v1/" + ::CGI::escape(m.key))
54
+ res = ::Net::HTTP.start(addr[1],
55
+ uri.port,
56
+ :use_ssl => uri.scheme == "https",
57
+ :open_timeout => @opts[:timeout] || 5,
58
+ :read_timeout => @opts[:timeout] || 5,
59
+ :ssl_timeout => @opts[:timeout] || 5) do |http|
60
+ req = ::Net::HTTP::Post.new(uri.path)
61
+ req.body = ::JSON::dump(m.serialize_json)
62
+ req.content_type = "application/json"
63
+ http.request(req)
64
+ end
65
+ if (res.code.to_i == 201)
66
+ sent += 1
67
+ end
68
+ end
69
+ end
70
+ return(sent)
71
+ end
72
+ end
73
+
74
+ class UDPTransport
75
+ MAXPAYLOAD = 1472
76
+ DEFAULT_PORT = 6968
77
+
78
+ def initialize(ring, opts)
79
+ @ring = ring
80
+ @opts = opts
23
81
  @sock = UDPSocket.new
24
82
  end
25
83
 
@@ -28,12 +86,14 @@ module LeelaClient
28
86
  end
29
87
 
30
88
  def send(metrics)
89
+ sent = 0
31
90
  LeelaClient::LoadBalancer.group_limit(@ring, metrics, MAXPAYLOAD).each do |addr, mms|
32
91
  mms.each do |ms|
33
- sent = @sock.send(serialize_list(ms), 0, addr[0], addr[1])
34
- raise if (sent > MAXPAYLOAD)
92
+ @sock.send(serialize_list(ms), 0, addr[0], @opts[:port] || DEFAULT_PORT)
93
+ sent += ms.size # yeah, hopefully!
35
94
  end
36
95
  end
96
+ return(sent)
37
97
  end
38
98
  end
39
99
  end
@@ -1,3 +1,6 @@
1
1
  module LeelaClient
2
- VERSION = "0.0.5"
2
+ VERSION = "1.0.0"
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
3
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leela_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-18 00:00:00.000000000 Z
13
+ date: 2013-05-08 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: A client for leela server
16
16
  email:
@@ -20,13 +20,13 @@ executables: []
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
+ - ./lib/leela_client.rb
24
+ - ./lib/leela_client/metrics.rb
25
+ - ./lib/leela_client/ring.rb
26
+ - ./lib/leela_client/api.rb
23
27
  - ./lib/leela_client/version.rb
24
28
  - ./lib/leela_client/transport.rb
25
- - ./lib/leela_client/api.rb
26
- - ./lib/leela_client/ring.rb
27
29
  - ./lib/leela_client/lb.rb
28
- - ./lib/leela_client/metrics.rb
29
- - ./lib/leela_client.rb
30
30
  homepage: https://github.com/locaweb/leela-client
31
31
  licenses: []
32
32
  post_install_message:
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 1.8.23
50
+ rubygems_version: 1.8.24
51
51
  signing_key:
52
52
  specification_version: 3
53
53
  summary: A client for leela server