fastbeans 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fastbeans/client.rb +3 -36
- data/lib/fastbeans/connection.rb +66 -0
- data/lib/fastbeans/response.rb +24 -1
- data/lib/fastbeans/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fab0fdaf53dfe7c4f0f821c7ccf713f937034f50
|
4
|
+
data.tar.gz: d14e754ccef7d5ee62ca7e78079dd155401f509f
|
5
5
|
!binary "U0hBNTEy":
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ec9d3d4080268b7604dbd51c223b370f1eeeb823fb5e7bc6bc930ca4b0b8e582dd31e2eb576fcb27375ded8b28dc66ed3633c9c65b04f0f37b56700af8e14a2
|
7
|
+
data.tar.gz: 571af395f94112bc12bf2ef2d42666fe68aed6234b932e3a934fd0686865343efa51f164ffb7d7642d4bfad14637777ac0bd7408ad3db158069b12318ab10534
|
data/lib/fastbeans/client.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'msgpack'
|
2
|
-
require 'thread'
|
3
2
|
require 'rufus-lru'
|
4
3
|
require 'fastbeans/connection'
|
5
4
|
require 'fastbeans/response'
|
6
5
|
require 'connection_pool'
|
7
|
-
module Fastbeans
|
8
6
|
|
7
|
+
module Fastbeans
|
9
8
|
class Client
|
10
9
|
CALL_CACHE_SIZE=100
|
11
|
-
MAX_RETRIES=3
|
12
10
|
|
13
11
|
attr_reader :call_cache
|
14
12
|
|
@@ -31,23 +29,8 @@ module Fastbeans
|
|
31
29
|
|
32
30
|
def call(*data)
|
33
31
|
Fastbeans.benchmark("Calling: #{data.inspect}") do
|
34
|
-
|
35
|
-
|
36
|
-
call_without_retries(*data)
|
37
|
-
rescue Fastbeans::RemoteConnectionFailed => e
|
38
|
-
Fastbeans.debug(e)
|
39
|
-
if retries < MAX_RETRIES
|
40
|
-
Fastbeans.debug("Retrying (#{retries} out of #{MAX_RETRIES} retries)")
|
41
|
-
retries += 1
|
42
|
-
begin
|
43
|
-
reconnect!
|
44
|
-
rescue => e
|
45
|
-
raise RemoteConnectionDead, e.message
|
46
|
-
end
|
47
|
-
retry
|
48
|
-
else
|
49
|
-
raise RemoteConnectionDead, "#{e.message} (#{retries} retries)"
|
50
|
-
end
|
32
|
+
pool.with do |conn|
|
33
|
+
conn.call(*data)
|
51
34
|
end
|
52
35
|
end
|
53
36
|
end
|
@@ -55,21 +38,5 @@ module Fastbeans
|
|
55
38
|
def cached_call(*data)
|
56
39
|
@call_cache[data] ||= call(*data)
|
57
40
|
end
|
58
|
-
|
59
|
-
def call_without_retries(*data)
|
60
|
-
raw_resp = pool.with do |conn|
|
61
|
-
payload = MessagePack.pack(data).force_encoding("BINARY")
|
62
|
-
conn.write([payload.bytesize].pack("N"))
|
63
|
-
conn.write(payload)
|
64
|
-
MessagePack.load(conn.socket)
|
65
|
-
end
|
66
|
-
resp = Response.new(data, raw_resp)
|
67
|
-
resp.payload
|
68
|
-
rescue IOError, Errno::EPIPE, MessagePack::MalformedFormatError => e
|
69
|
-
ne = RemoteConnectionFailed.new(e.message)
|
70
|
-
ne.orig_exc = e
|
71
|
-
raise ne
|
72
|
-
end
|
73
41
|
end
|
74
|
-
|
75
42
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module Fastbeans
|
4
|
+
class Connection
|
5
|
+
MAX_RETRIES=3
|
6
|
+
|
7
|
+
attr_reader :socket
|
8
|
+
|
9
|
+
def initialize(host, port)
|
10
|
+
Fastbeans.debug("Connecting to #{host}:#{port}")
|
11
|
+
@host, @port = host, port
|
12
|
+
connect!(@host, @port)
|
13
|
+
end
|
14
|
+
|
15
|
+
def connect!(host, port)
|
16
|
+
@socket = TCPSocket.new(host, port)
|
17
|
+
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
18
|
+
end
|
19
|
+
|
20
|
+
def disconnect!
|
21
|
+
if @socket
|
22
|
+
@socket.close rescue nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def reconnect!
|
27
|
+
disconnect!
|
28
|
+
connect!(@host, @port)
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(*data)
|
32
|
+
retries = 0
|
33
|
+
begin
|
34
|
+
call_without_retries(*data)
|
35
|
+
rescue Fastbeans::RemoteConnectionFailed => e
|
36
|
+
Fastbeans.debug(e)
|
37
|
+
if retries < MAX_RETRIES
|
38
|
+
Fastbeans.debug("Retrying (#{retries} out of #{MAX_RETRIES} retries)")
|
39
|
+
retries += 1
|
40
|
+
begin
|
41
|
+
reconnect!
|
42
|
+
rescue => e
|
43
|
+
raise RemoteConnectionDead, e.message
|
44
|
+
end
|
45
|
+
retry
|
46
|
+
else
|
47
|
+
raise RemoteConnectionDead, "#{e.message} (#{retries} retries)"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def call_without_retries(*data)
|
53
|
+
payload = MessagePack.pack(data).force_encoding("BINARY")
|
54
|
+
@socket.write([payload.bytesize].pack("N"))
|
55
|
+
@socket.write(payload)
|
56
|
+
raw_resp = MessagePack.load(@socket)
|
57
|
+
resp = Response.new(data, raw_resp)
|
58
|
+
resp.payload
|
59
|
+
rescue IOError, Errno::EPIPE, MessagePack::MalformedFormatError => e
|
60
|
+
ne = RemoteConnectionFailed.new(e.message)
|
61
|
+
ne.orig_exc = e
|
62
|
+
raise ne
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
data/lib/fastbeans/response.rb
CHANGED
@@ -21,9 +21,32 @@ module Fastbeans
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def to_exception
|
24
|
-
name = @raw_response["fastbeans-error"]
|
24
|
+
name = camelize(underscore(@raw_response["fastbeans-error"]))
|
25
25
|
Fastbeans.exception(name).new(@raw_response["error-information"])
|
26
26
|
end
|
27
|
+
|
28
|
+
def camelize(term, uppercase_first_letter = true)
|
29
|
+
string = term.to_s
|
30
|
+
acronym_regex = /(?=a)b/
|
31
|
+
if uppercase_first_letter
|
32
|
+
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
|
33
|
+
else
|
34
|
+
string = string.sub(/^(?:#{acronym_regex}(?=\b|[A-Z_])|\w)/) { $&.downcase }
|
35
|
+
end
|
36
|
+
string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
|
37
|
+
end
|
38
|
+
|
39
|
+
def underscore(camel_cased_word)
|
40
|
+
word = camel_cased_word.to_s.dup
|
41
|
+
acronym_regex = /(?=a)b/
|
42
|
+
word.gsub!(/::/, '/')
|
43
|
+
word.gsub!(/(?:([A-Za-z\d])|^)(#{acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
|
44
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
45
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
46
|
+
word.tr!("-", "_")
|
47
|
+
word.downcase!
|
48
|
+
word
|
49
|
+
end
|
27
50
|
end
|
28
51
|
|
29
52
|
end
|
data/lib/fastbeans/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fastbeans
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dima Sabanin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: msgpack
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- fastbeans.gemspec
|
96
96
|
- lib/fastbeans.rb
|
97
97
|
- lib/fastbeans/client.rb
|
98
|
+
- lib/fastbeans/connection.rb
|
98
99
|
- lib/fastbeans/errors.rb
|
99
100
|
- lib/fastbeans/response.rb
|
100
101
|
- lib/fastbeans/version.rb
|