Class: MsgpackRpcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/msgpack_rpc_client.rb,
lib/msgpack_rpc_client/version.rb

Overview

MessagePack-RPC client

Defined Under Namespace

Classes: Error

Constant Summary collapse

DEFAULT_MAX_RETRIES =
10
DEFAULT_MAX_CONNECT_RETRIES =
5
DEFAULT_CONNECT_RETRY_WAIT =

seconds

0.1
MAX_MSGID =
1_000_000_000
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MsgpackRpcClient

Initialize client and establish connection to server.

(While it may seem beneficial to not connect in the constructor and wait for the first RPC call, I believe it’s better to fail early.

  • host, port, use_ssl - configure your connection

  • logger - logger (default nil, set to Rails.logger in a Rails app)

Parameters that are better left alone:

  • max_retries - number of times to retry sending a request

  • max_connect_retries - number of times to retry connecting to the server

  • connect_retry_wait - wait between connection retry attempts

TODO: once we are long past the <2.0.0 legacy, replace with named args



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/msgpack_rpc_client.rb', line 30

def initialize(options={})
  @host = options.fetch(:host)
  @port = options.fetch(:port)
  @use_ssl = options.fetch(:use_ssl, false)
  @logger = options.fetch(:logger, nil)
  @max_retries = options.fetch(:max_retries, DEFAULT_MAX_RETRIES)
  @max_connect_retries = options.fetch(:max_connect_retries, DEFAULT_MAX_CONNECT_RETRIES)
  @connect_retry_wait = options.fetch(:connect_retry_wait, DEFAULT_CONNECT_RETRY_WAIT)
  @msgid = 1
  @call_mutex = Mutex.new
  init_socket
end

Instance Attribute Details

#connect_retry_waitObject (readonly)

Returns the value of attribute connect_retry_wait.



14
15
16
# File 'lib/msgpack_rpc_client.rb', line 14

def connect_retry_wait
  @connect_retry_wait
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/msgpack_rpc_client.rb', line 14

def host
  @host
end

#max_connect_retriesObject (readonly)

Returns the value of attribute max_connect_retries.



14
15
16
# File 'lib/msgpack_rpc_client.rb', line 14

def max_connect_retries
  @max_connect_retries
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



14
15
16
# File 'lib/msgpack_rpc_client.rb', line 14

def max_retries
  @max_retries
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/msgpack_rpc_client.rb', line 14

def port
  @port
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



14
15
16
# File 'lib/msgpack_rpc_client.rb', line 14

def use_ssl
  @use_ssl
end

Instance Method Details

#call(method_name, *params) ⇒ Object

Call an RPC method. Will reconnect if the server is down. Threadsafe.

  • Params is anything serializable with Messagepack.

  • Hashes in response will be deserialized with symbolized keys



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/msgpack_rpc_client.rb', line 47

def call(method_name, *params)
  request = nil
  response = nil

  @call_mutex.synchronize do
    request = [0, @msgid, method_name, params]
    @msgid = (@msgid % MAX_MSGID) + 1
    response = make_request_with_retries(request)
  end

  if response[0] != 1
    raise MsgpackRpcClient::Error, 'Response does not bear the proper type flag - something is very wrong'
  end
  if response[1] != request[1]
    raise MsgpackRpcClient::Error, 'Response message id does not match request message id - something is very wrong'
  end
  if response[2] != nil
    raise MsgpackRpcClient::Error, "Server responded with error: #{response[2]}"
  end

  response[3]
end