Class: Fastbeans::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fastbeans/client.rb

Constant Summary collapse

CALL_CACHE_SIZE =
100
MAX_RETRIES =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 12345, cache_size = nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/fastbeans/client.rb', line 13

def initialize(host="127.0.0.1", port=12345, cache_size=nil)
  @host, @port = host, port
  @cache_size ||= CALL_CACHE_SIZE
  @call_cache = Rufus::Lru::SynchronizedHash.new(@cache_size)
  connect!(host, port)
end

Instance Attribute Details

#call_cacheObject (readonly)

Returns the value of attribute call_cache.



11
12
13
# File 'lib/fastbeans/client.rb', line 11

def call_cache
  @call_cache
end

Instance Method Details

#cached_call(*data) ⇒ Object



60
61
62
# File 'lib/fastbeans/client.rb', line 60

def cached_call(*data)
  @call_cache[data] ||= call(*data)
end

#call(*data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastbeans/client.rb', line 37

def call(*data)
  Fastbeans.benchmark("Calling: #{data.inspect}") do
    retries = 0
    begin
      call_without_retries(*data)
    rescue Fastbeans::RemoteConnectionFailed => e
      Fastbeans.debug(e)
      if retries < MAX_RETRIES
        Fastbeans.debug("Retrying (#{retries} out of #{MAX_RETRIES} retries)")
        retries += 1
        begin
          reconnect!
        rescue => e
          raise RemoteConnectionDead, e.message
        end
        retry
      else
        raise RemoteConnectionDead, "#{e.message} (#{retries} retries)"
      end
    end
  end
end

#call_without_retries(*data) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fastbeans/client.rb', line 64

def call_without_retries(*data)
  resp = @mutex.synchronize do
    payload = MessagePack.pack(data).force_encoding("BINARY")
    @sock.write([payload.bytesize].pack("N"))
    @sock.write(payload)
    MessagePack.load(@sock)
  end
  if resp == 0xDEAD
    raise RemoteException.new("Remote exception on #{data.inspect} through #{@sock.inspect}")
  else
    resp
  end
rescue IOError, Errno::EPIPE, MessagePack::MalformedFormatError => e
  ne = RemoteConnectionFailed.new(e.message)
  ne.orig_exc = e
  raise ne
end

#clear_call_cache!Object



20
21
22
# File 'lib/fastbeans/client.rb', line 20

def clear_call_cache!
  @call_cache.clear
end

#connect!(host, port) ⇒ Object



30
31
32
33
34
35
# File 'lib/fastbeans/client.rb', line 30

def connect!(host, port)
  Fastbeans.debug("Connecting to #{host}:#{port}")
  @sock = TCPSocket.new(host, port)
  @sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  @mutex = Mutex.new
end

#disconnect!Object



82
83
84
85
86
# File 'lib/fastbeans/client.rb', line 82

def disconnect!
  if @sock
    @sock.close rescue nil
  end
end

#reconnect!Object



24
25
26
27
# File 'lib/fastbeans/client.rb', line 24

def reconnect!
  disconnect!
  connect!(@host, @port)
end