redis_rpc 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9446d050ad21ca78739f4d986fd7703a683df9b9
4
- data.tar.gz: 2f43f268796cd00c1b77e2fe107ef16fba2ee567
2
+ SHA256:
3
+ metadata.gz: a5a0c8bebe40efe81928d485dbe2f747c5707be39d44bd093a68c23bd7530028
4
+ data.tar.gz: 5bff08a69ee054753c03df250756dd663bfe8bc268bf4b72bfbcc546b382f35b
5
5
  SHA512:
6
- metadata.gz: d81714bc20bb38db199483e30702b99155916b71ab9bec05262beb20cdb2f5d22812edae1af9308424c254de8ab8a0c3b1f0facad459217145809747b7269bf7
7
- data.tar.gz: 274515576b96b3173eccec4d8e04888ed797a732f0e250a6bddbbee474b36d047f9fd852f41e5b7bd0d0a255a94ff4b1e55972073f3d020f8c0f96bf9e9cea56
6
+ metadata.gz: 4aeacfe6adf23f5e3fcbff5396ceed744fe4c312c4b949537cb1b81cb4a19325691a643880b187980e0c81b8062c5313aac9fa8ba1273f026c490b703820f969
7
+ data.tar.gz: 6600c450251f5f1f2bb8fe89dcb96f15977cba58ab3a41878e19a1f54518b88d8fca14243fad9b2b789c3a0781288305993af7463940f7607e0e3fb78698f304
@@ -14,6 +14,7 @@ module RedisRpc
14
14
  @redis = Redis.new(url: url)
15
15
  @sub_channel = sub_channel
16
16
  @pub_channel = pub_channel
17
+ @level = level
17
18
  @timeout = timeout
18
19
  @parser = Parser.new(secret_key)
19
20
  @res = Response.new(Redis.new(url: url), pub_channel, init_log(level), @parser)
@@ -30,11 +31,11 @@ module RedisRpc
30
31
  end
31
32
 
32
33
  on.message do |channel, args|
33
- @logger.info("##{channel}: #{args}")
34
+ @logger.info("##{channel}: #{args}") if @level <= Logger::INFO
34
35
  begin
35
36
  _args = @parser.parse(args)
36
37
  @logger.error(ArgumentError.new("miss method uuid")) and return if _args[:uuid].nil?
37
- @res.sync_callback(_args, @timeout) if !@callback.exec_callback(_args)
38
+ @res.sync_callback(_args) if !@callback.exec_callback(_args)
38
39
  rescue Exception => e
39
40
  @logger.error(e)
40
41
  end
@@ -1,23 +1,22 @@
1
1
  require "redis_rpc/response.rb"
2
2
  module RedisRpc
3
3
 
4
- class Logic
4
+ class Logic
5
5
 
6
- attr_accessor :res
6
+ attr_accessor :res
7
7
 
8
- def initialize(url, callback, channel, logger, parser)
9
- @redis = Redis.new(url: url)
10
- @logger = logger
11
- @res = Response.new(@redis, channel, logger, parser)
12
- @callback = callback
13
- @parser = parser
14
- end
8
+ def initialize(url, callback, channel, logger, parser)
9
+ @logger = logger
10
+ @res = ClientResponse.new(Redis.new(url: url), channel, logger, parser)
11
+ @callback = callback
12
+ @parser = parser
13
+ end
15
14
 
16
- def exec(args, timeout)
15
+ def exec(args, timeout)
16
+ Thread.new do
17
17
  begin
18
18
  _args = @parser.parse(args)
19
- logger.error(ArgumentError.new("miss method name or uuid")) and return if _args[:uuid].nil? || _args[:method].nil?
20
-
19
+ @logger.error(ArgumentError.new("miss method name or uuid")) and return if _args[:uuid].nil? || _args[:method].nil?
21
20
  result = @callback.send(_args[:method], *_args[:params])
22
21
  @res.publish({uuid: _args[:uuid], _method: _args[:method], result: result}, timeout)
23
22
  rescue Exception => e
@@ -27,9 +26,19 @@ module RedisRpc
27
26
  @logger.error(e)
28
27
  end
29
28
  end
30
- rescue Exception => e
31
- @logger.error(e)
32
29
  end
30
+ rescue Exception => e
31
+ @logger.error(e)
32
+ end
33
+
34
+ end
35
+
36
+ class ClientResponse < Response
37
+
38
+ def publish(request, timeout)
39
+ request_str = @parser.pack(request.to_json)
40
+ @redis.publish(@channel, request_str)
41
+ end
33
42
 
34
43
  end
35
44
 
@@ -11,18 +11,22 @@ module RedisRpc
11
11
  @channel = channel
12
12
  @logger = logger
13
13
  @parser = parser
14
+ @sync_handlers = {}
14
15
  end
15
16
 
16
17
  def publish(request, timeout)
17
18
  request_str = @parser.pack(request.to_json)
18
19
  @redis.publish(@channel, request_str)
19
- SyncHandler.new(@redis, request[:uuid], request[:method], @parser.secret_key, timeout)
20
+ @sync_handlers[request[:uuid]] = SyncHandler.new(@parser.secret_key, request[:method], timeout)
21
+ Thread.new(@sync_handlers, request[:uuid], timeout) {|handlers, uuid, t| sleep(t+3); handlers.delete(uuid) }
22
+ @sync_handlers[request[:uuid]]
20
23
  end
21
24
 
22
- def sync_callback(args, timeout=5)
23
- # {uuid: uuid, _method: method, result: result, error: error}
24
- @redis.set(args[:uuid], @parser.pack(args.to_json))
25
- @redis.expire(args[:uuid], timeout.to_i)
25
+ def sync_callback(args)
26
+ # {uuid: uuid, _method: method, result: result, error: error}
27
+ unless (sync_handler = @sync_handlers.delete(args[:uuid])).nil?
28
+ sync_handler.release(args)
29
+ end
26
30
  end
27
31
 
28
32
  def catch(uuid, e)
@@ -35,31 +39,28 @@ module RedisRpc
35
39
 
36
40
  class SyncHandler
37
41
 
38
- SLEEP_TIME = 0.01
39
-
40
- def initialize(redis, uuid, _method, secret_key, timeout=30)
41
- @redis = redis
42
- @uuid = uuid
43
- @_method = _method
44
- @expires_at = Time.now + timeout
42
+ def initialize(secret_key, _method, timeout=10)
45
43
  @parser = Parser.new(secret_key)
44
+ @_method = _method
45
+ @timeout = timeout
46
+ @lock = Mutex.new
47
+ @condition = ConditionVariable.new
46
48
  end
47
49
 
48
50
  def sync
49
- while Time.now <= @expires_at
50
- result = @redis.get(@uuid)
51
- if !result.nil?
52
- @redis.del(@uuid)
53
- _args = @parser.parse(result)
54
- if !_args[:result].nil?
55
- return _args[:result]
56
- elsif !_args[:error].nil?
57
- raise(FunctionCallbackError.new(_args[:error]))
58
- end
59
- end
60
- sleep SLEEP_TIME
51
+ @lock.synchronize { @condition.wait(@lock, @timeout) }
52
+ if @response.nil?
53
+ raise(Timeout::Error.new("method: #{@_method} wait for timeout #{@timeout}s"))
54
+ elsif !@response[:result].nil?
55
+ return @response[:result]
56
+ elsif !@response[:error].nil?
57
+ raise(FunctionCallbackError.new(@response[:error]))
61
58
  end
62
- raise(Timeout::Error.new("method: #{@_method} wait for timeout"))
59
+ end
60
+
61
+ def release(res)
62
+ @response = res
63
+ @lock.synchronize { @condition.signal }
63
64
  end
64
65
 
65
66
  end
@@ -13,6 +13,7 @@ module RedisRpc
13
13
  @redis = Redis.new(url: url)
14
14
  @sub_channel = sub_channel
15
15
  @pub_channel = pub_channel
16
+ @level = level
16
17
  @timeout = timeout
17
18
  @parser = Parser.new(secret_key)
18
19
  @logic = Logic.new(url, front_object, pub_channel, init_log(level), @parser)
@@ -33,7 +34,7 @@ module RedisRpc
33
34
  end
34
35
 
35
36
  on.message do |channel, args|
36
- @logger.info("##{channel}: #{args}")
37
+ @logger.info("##{channel}: #{args}") if @level <= Logger::INFO
37
38
  @logic.exec(args, @timeout)
38
39
  end
39
40
  on.unsubscribe do |channel, subscriptions|
@@ -1,3 +1,3 @@
1
1
  module RedisRpc
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - benko
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  version: '0'
45
45
  requirements: []
46
46
  rubyforge_project:
47
- rubygems_version: 2.6.11
47
+ rubygems_version: 2.7.7
48
48
  signing_key:
49
49
  specification_version: 4
50
50
  summary: redis rpc