Class: RedisRpc::Client
- Inherits:
-
Object
- Object
- RedisRpc::Client
- Defined in:
- lib/redis-rpc.rb
Instance Method Summary collapse
- #get_timeout_at ⇒ Object
-
#initialize(redis_server, message_queue, timeout: 0) ⇒ Client
constructor
A new instance of Client.
- #respond_to?(method_name) ⇒ Boolean
- #send(method_name, *args, **kwargs) ⇒ Object (also: #method_missing)
- #send! ⇒ Object
Constructor Details
#initialize(redis_server, message_queue, timeout: 0) ⇒ Client
Returns a new instance of Client.
47 48 49 50 51 |
# File 'lib/redis-rpc.rb', line 47 def initialize(redis_server, , timeout: 0) @redis_server = redis_server @message_queue = @timeout = timeout end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/redis-rpc.rb', line 92 def send(method_name, *args, **kwargs) raise MalformedRequestException, 'block not allowed over RPC' if block_given? # request setup function_call = { 'name' => method_name.to_s, 'args' => args, 'kwargs' => kwargs } response_queue = @message_queue + ':rpc:' + rand_string rpc_request = { 'function_call' => function_call, 'response_queue' => response_queue, 'timeout_at' => get_timeout_at, } rpc_raw_request = rpc_request.to_json # transport @redis_server.rpush @message_queue, rpc_raw_request , rpc_raw_response = @redis_server.blpop response_queue, timeout: @timeout raise TimeoutException if rpc_raw_response.nil? # response handling rpc_response = JSON.parse(rpc_raw_response) raise RemoteException.new(rpc_response['exception'], rpc_response['backtrace']) if rpc_response.has_key? 'exception' raise MalformedResponseException, rpc_response unless rpc_response.has_key? 'return_value' # noinspection RubyUnnecessaryReturnStatement return rpc_response['return_value'] rescue TimeoutException, SignalException # stale request cleanup @redis_server.lrem @message_queue, 0, rpc_raw_request raise end |
Instance Method Details
#get_timeout_at ⇒ Object
55 56 57 58 |
# File 'lib/redis-rpc.rb', line 55 def get_timeout_at # allow mock to manipulate timeout to verify safety behavior Time.now.to_i + @timeout + 60 end |
#respond_to?(method_name) ⇒ Boolean
94 95 96 |
# File 'lib/redis-rpc.rb', line 94 def respond_to?(method_name) send(:respond_to?, method_name) end |
#send(method_name, *args, **kwargs) ⇒ Object Also known as: method_missing
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/redis-rpc.rb', line 60 def send(method_name, *args, **kwargs) raise MalformedRequestException, 'block not allowed over RPC' if block_given? # request setup function_call = { 'name' => method_name.to_s, 'args' => args, 'kwargs' => kwargs } response_queue = @message_queue + ':rpc:' + rand_string rpc_request = { 'function_call' => function_call, 'response_queue' => response_queue, 'timeout_at' => get_timeout_at, } rpc_raw_request = rpc_request.to_json # transport @redis_server.rpush @message_queue, rpc_raw_request , rpc_raw_response = @redis_server.blpop response_queue, timeout: @timeout raise TimeoutException if rpc_raw_response.nil? # response handling rpc_response = JSON.parse(rpc_raw_response) raise RemoteException.new(rpc_response['exception'], rpc_response['backtrace']) if rpc_response.has_key? 'exception' raise MalformedResponseException, rpc_response unless rpc_response.has_key? 'return_value' # noinspection RubyUnnecessaryReturnStatement return rpc_response['return_value'] rescue TimeoutException, SignalException # stale request cleanup @redis_server.lrem @message_queue, 0, rpc_raw_request raise end |
#send! ⇒ Object
53 |
# File 'lib/redis-rpc.rb', line 53 alias :send! :send |