fastbeans 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
- metadata.gz: 81dd5cefa84f07df8a30be547a2d1f59d9a45fb1
4
- data.tar.gz: 0f4246c4f140470ce697400708f34fb10dc3f3ca
3
+ metadata.gz: 89ea5a3305ef11598f9c2bba6d899d2d72eb621f
4
+ data.tar.gz: aa524d721321b2b66b26756cca5c38bc298bfcdf
5
5
  !binary "U0hBNTEy":
6
- metadata.gz: 34fb78c25ae1d453793b48679a6c53ff31558e15ac584333d8354ffe98d162524b00a4700bf5c575d298b922cb435b602fe2d5422cb4aca6a74b42f9d68bdc8c
7
- data.tar.gz: 641b69d5cf561bfddaca4ca165e405f04db666fc6cd60599368ceeef8db13d1f768f0aaf3db3d3bc850a509b9a86f73322c05fc9eac3160e1c6c28f225826ca5
6
+ metadata.gz: 8a5001e30ab099d5a221b437d8aba58f135a9fe12a32cc48b7add08c337caf36949e4788094e2ef9929146722d5e4520a2a9d2d7b1edf78a04e719c8b728c90b
7
+ data.tar.gz: 654b8cd94201a680eb3a0a070c212edbfde8e628db67ce9f3c6a1291b31c9efee722f0680b60379837e955c31e2bf80729eaa284067a97b30498b4a4d9ff0cef
@@ -6,10 +6,11 @@ module Fastbeans
6
6
 
7
7
  class Client
8
8
  CALL_CACHE_SIZE=100
9
-
9
+ MAX_RETRIES=3
10
+
10
11
  attr_reader :call_cache
11
12
 
12
- def initialize(host, port, cache_size=nil)
13
+ def initialize(host="127.0.0.1", port=12345, cache_size=nil)
13
14
  @host, @port = host, port
14
15
  @cache_size ||= CALL_CACHE_SIZE
15
16
  @call_cache = Rufus::Lru::SynchronizedHash.new(@cache_size)
@@ -25,27 +26,33 @@ module Fastbeans
25
26
  connect!(@host, @port)
26
27
  end
27
28
 
29
+
28
30
  def connect!(host, port)
31
+ Fastbeans.debug("Connecting to #{host}:#{port}")
29
32
  @sock = TCPSocket.new(host, port)
30
33
  @sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
31
34
  @mutex = Mutex.new
32
35
  end
33
36
 
34
37
  def call(*data)
35
- retries = 0
36
- begin
37
- call_without_retries(*data)
38
- rescue Fastbeans::RemoteConnectionFailed => e
39
- if retries < 3
40
- retries += 1
41
- begin
42
- reconnect!
43
- rescue => e
44
- raise RemoteConnectionDead, e.message
38
+ Fastbeans.benchmark("Calling: #{data.inspect}") do
39
+ retries = 0
40
+ begin
41
+ call_without_retries(*data)
42
+ rescue Fastbeans::RemoteConnectionFailed => e
43
+ debug(e)
44
+ if retries < MAX_RETRIES
45
+ debug("Retrying (#{retries} out of #{MAX_RETRIES} retries)")
46
+ retries += 1
47
+ begin
48
+ reconnect!
49
+ rescue => e
50
+ raise RemoteConnectionDead, e.message
51
+ end
52
+ retry
53
+ else
54
+ raise RemoteConnectionDead, "#{e.message} (#{retries} retries)"
45
55
  end
46
- retry
47
- else
48
- raise RemoteConnectionDead, "#{e.message} (#{retries} retries)"
49
56
  end
50
57
  end
51
58
  end
@@ -56,7 +63,7 @@ module Fastbeans
56
63
 
57
64
  def call_without_retries(*data)
58
65
  resp = @mutex.synchronize do
59
- payload = MessagePack.pack(data).force_encoding("CP1252") # fucking kill me please
66
+ payload = MessagePack.pack(data).force_encoding("BINARY")
60
67
  @sock.write([payload.bytesize].pack("N"))
61
68
  @sock.write(payload)
62
69
  MessagePack.load(@sock)
@@ -1,3 +1,3 @@
1
1
  module Fastbeans
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/fastbeans.rb CHANGED
@@ -3,4 +3,26 @@ require "fastbeans/errors"
3
3
  require "fastbeans/client"
4
4
 
5
5
  module Fastbeans
6
+ class << self
7
+ # Debug mode
8
+ attr_accessor :debug_mode
9
+
10
+ def debug(str_or_exc)
11
+ case str_or_exc
12
+ when String
13
+ STDERR.puts("[#{Time.now}] #{str}") if self.debug_mode
14
+ when Exception
15
+ debug("Exception: #{str_or_exc.message}\n#{str_or_exc.backtrace.join("\n")}")
16
+ end
17
+ end
18
+
19
+ def benchmark(str, &blk)
20
+ debug(str)
21
+ t1 = Time.now
22
+ blk.call
23
+ ensure
24
+ t2 = Time.now
25
+ debug("Time spent: #{t2-t1}s")
26
+ end
27
+ end
6
28
  end
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.1.0
4
+ version: 0.1.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-06 00:00:00.000000000 Z
11
+ date: 2013-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -108,3 +108,4 @@ signing_key:
108
108
  specification_version: 4
109
109
  summary: Ruby piece of Ruby/Clojure RPC system extracted from beanstalkapp.com
110
110
  test_files: []
111
+ has_rdoc: