pigato 0.2.27 → 0.3.0

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
2
  SHA1:
3
- metadata.gz: 4447f773500f2557cdd2ae53ed0e83985602108b
4
- data.tar.gz: c7351e169c89359e324f3e3e088861a04addcdaa
3
+ metadata.gz: a968103e83dfa7c74641b521b44ef40631de9a10
4
+ data.tar.gz: 2704c9177783f08d2c2d31bbc9ce42cd58c8f581
5
5
  SHA512:
6
- metadata.gz: 74ed8e928abb8a2add104bb3a3a89407dbeada35d04b20f287f48c17f6325e908387102bf35e51832b304247c0607b8c8fc09a28f3e13256ccf8949ed733d30d
7
- data.tar.gz: b6b9948d4cb9e936b68638509e2e20f9dd7a40d0b29f73c0224891f18077d577a2f1d55df1ed3b4c5655c12ba31b5939409af4a4326304d1241ba03672f2faed
6
+ metadata.gz: cb2ca87e271ded59c9674624edfe6bde43535fb05884327fe69d5e543ae4d046e584e03933ffb869050306b8fe02757a4d6077acc560a8b8728ebf8309c3371f
7
+ data.tar.gz: f860dee15833e70138bb240766348b0a1cec1ca941d3ea0b71ff99e0be8a079c798cb18e4084cad38ea3d23201ed3ea8490c8efb3ad697a6e6b640d65da37872
@@ -1,23 +1,29 @@
1
1
  #!/usr/bin/env ruby
2
- require 'pry-remote'
3
2
 
4
3
  require "rubygems"
5
4
  require "#{File.dirname(__FILE__)}/../lib/pigato.rb"
6
5
  #require "pigato"
7
6
  require 'thread'
8
7
 
9
- client = Pigato::Client.new('tcp://localhost:55555')
10
- client.start
8
+ ts = []
11
9
 
12
- Process.daemon
10
+ [0,1,2,3,4,5,6].each do |tid|
11
+ ts << Thread.new {
12
+ client = Pigato::Client.new('tcp://127.0.0.1:55555')
13
+ client.start
13
14
 
14
- client.start
15
- requests = 1000
16
- d1 = Time.now
17
- requests.times do |i|
18
- begin
19
- client.request('echo', 'Hello world1')
20
- end
15
+ requests = 10000
16
+ d1 = Time.now
17
+ requests.times do |i|
18
+ begin
19
+ client.request('echo', 'Hello world1')
20
+ end
21
+ end
22
+ d2 = Time.now
23
+ puts "#{requests} requests/replies processed (#{(d2 - d1) * 1000} milliseconds)"
24
+ }
25
+ end
26
+
27
+ ts.each do |to|
28
+ to.join
21
29
  end
22
- d2 = Time.now
23
- puts "#{requests} requests/replies processed (#{(d2 - d1) * 1000} milliseconds)"
@@ -4,9 +4,24 @@ require "rubygems"
4
4
  require "#{File.dirname(__FILE__)}/../lib/pigato.rb"
5
5
  #require "pigato"
6
6
 
7
- worker = Pigato::Worker.new('tcp://localhost:55555', 'echo')
7
+ ts = []
8
8
 
9
- loop do
10
- request = worker.recv
11
- worker.reply request
9
+ [0, 1, 2, 3, 4, 5, 6].each do |tid|
10
+ ts << Thread.new {
11
+ worker = Pigato::Worker.new('tcp://127.0.0.1:55555', 'echo')
12
+ worker.start
13
+
14
+ loop do
15
+ request = worker.recv
16
+ if !request.nil?
17
+ worker.reply request
18
+ else
19
+ sleep 0.1
20
+ end
21
+ end
22
+ }
23
+ end
24
+
25
+ ts.each do |to|
26
+ to.join
12
27
  end
@@ -0,0 +1,88 @@
1
+ class Pigato::Base
2
+
3
+ @@sockets = {}
4
+ @@mtxs = {}
5
+ @@mtx = Mutex.new
6
+
7
+ def init
8
+ @iid = SecureRandom.uuid
9
+ end
10
+
11
+ def get_thread_id
12
+ tid = get_proc_id() + "#" + Thread.current.object_id.to_s
13
+ tid
14
+ end
15
+
16
+ def get_proc_id
17
+ pid = "#" + Process.pid.to_s
18
+ pid
19
+ end
20
+
21
+ def get_iid
22
+ iid = get_thread_id + '#' + @iid
23
+ iid
24
+ end
25
+
26
+ def get_socket
27
+ socket = @@sockets[get_iid]
28
+ socket
29
+ end
30
+
31
+ def get_mtx
32
+ tid = get_thread_id
33
+
34
+ if @@mtxs[tid].nil?
35
+ @@mtxs[tid] = Mutex.new
36
+ end
37
+
38
+ return @@mtxs[tid]
39
+ end
40
+
41
+ def sock_create
42
+ @@mtx.synchronize {
43
+ pid = get_proc_id()
44
+
45
+ ctx = ZMQ::context
46
+ if ctx == nil then
47
+ ctx = ZMQ::Context.new
48
+ ctx.linger = 0
49
+ end
50
+
51
+ socket = ctx.socket ZMQ::DEALER
52
+ socket.identity = SecureRandom.uuid
53
+ socket.connect @broker
54
+
55
+ if @conf[:timeout] then
56
+ socket.rcvtimeo = @conf[:timeout];
57
+ end
58
+
59
+ @@sockets[get_iid] = socket
60
+ }
61
+ end
62
+
63
+ def sock_close
64
+ @@mtx.synchronize {
65
+ pid = get_proc_id()
66
+
67
+ iid = get_iid
68
+
69
+ socket = @@sockets[iid]
70
+ if socket
71
+ begin
72
+ socket.close
73
+ rescue
74
+ end
75
+ @@sockets.delete(iid)
76
+ end
77
+ }
78
+ end
79
+
80
+ def start
81
+ @active = 1
82
+ end
83
+
84
+ def stop
85
+ @active = 0
86
+ end
87
+
88
+ end
data/lib/pigato/client.rb CHANGED
@@ -1,11 +1,13 @@
1
- require 'thread'
1
+ require "#{File.dirname(__FILE__)}/base.rb"
2
2
 
3
- class Pigato::Client
3
+ class Pigato::Client < Pigato::Base
4
+
5
+ @@mtx = Mutex.new
6
+ @@ctxs = {}
7
+ @@sockets = {}
4
8
 
5
9
  def initialize broker, conf = {}
6
10
  @broker = broker
7
- @ctxs = {}
8
- @sockets = {}
9
11
 
10
12
  @conf = {
11
13
  :autostart => false,
@@ -14,26 +16,18 @@ class Pigato::Client
14
16
 
15
17
  @conf.merge!(conf)
16
18
 
19
+ init
20
+
17
21
  if @conf[:autostart]
18
22
  start
19
23
  end
20
24
  end
21
25
 
22
- def get_proc_id
23
- pid = "#" + Process.pid.to_s
24
- pid
25
- end
26
-
27
- def get_thread_id
28
- tid = "#" + get_proc_id() + "#" + Thread.current.object_id.to_s
29
- tid
30
- end
31
-
32
26
  def request service, request, opts = {}
33
- return nil if @sockets[get_thread_id()] == nil
34
-
35
- socket = @sockets[get_thread_id()]
27
+ iid = get_iid
28
+ return nil if @@sockets[iid] == nil
36
29
 
30
+ socket = @@sockets[iid]
37
31
  request = [Oj.dump(request), Oj.dump(opts)]
38
32
 
39
33
  rid = SecureRandom.uuid
@@ -56,8 +50,10 @@ class Pigato::Client
56
50
  end
57
51
 
58
52
  def _recv rid
59
- socket = @sockets[get_thread_id()]
53
+ iid = get_iid
54
+ socket = @@sockets[iid]
60
55
  socket.rcvtimeo = @conf[:timeout]
56
+
61
57
  data = []
62
58
  d1 = Time.now
63
59
  msg = socket.recv_message()
@@ -74,35 +70,15 @@ class Pigato::Client
74
70
  end
75
71
 
76
72
  def start
77
- reconnect_to_broker
78
- end
79
-
80
- def stop
81
- tid = get_thread_id()
82
- if @sockets[tid]
83
- @sockets[tid].close
84
- @sockets.delete(tid)
85
- end
86
-
87
- pid = get_proc_id()
88
- if @ctxs[pid]
89
- @ctxs[pid].destroy
90
- @ctxs.delete(pid)
91
- end
92
- end
93
-
94
- def reconnect_to_broker
95
73
  stop
96
-
97
- ctx = ZMQ::Context.new
98
- ctx.linger = 0
99
- @ctxs[get_proc_id()] = ctx
100
-
101
- socket = ctx.socket ZMQ::DEALER
102
- socket.identity = SecureRandom.uuid
103
- socket.connect @broker
104
- @sockets[get_thread_id()] = socket
74
+ sock_create
75
+ super
105
76
  rescue ZMQ::Error => e
106
77
  puts e
107
78
  end
79
+
80
+ def stop
81
+ sock_close
82
+ super
83
+ end
108
84
  end
@@ -1,3 +1,3 @@
1
1
  module Pigato
2
- VERSION = "0.2.27"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/pigato/worker.rb CHANGED
@@ -1,35 +1,54 @@
1
- class Pigato::Worker
1
+ require "#{File.dirname(__FILE__)}/base.rb"
2
+
3
+ class Pigato::Worker < Pigato::Base
2
4
 
3
5
  HEARTBEAT_LIVENESS = 3 # 3-5 is reasonable
4
6
 
5
- def initialize broker, service
7
+ def initialize broker, service, conf = {}
6
8
  @broker = broker
7
9
  @service = service
8
- @heartbeat_at = 0 # When to send HEARTBEAT (relative to time.time(), so in seconds)
9
- @liveness = 0 # How many attempts left
10
- @timeout = 2500
11
- @heartbeat = 2500 # Heartbeat delay, msecs
12
- @reconnect = 2500 # Reconnect delay, msecs
13
10
 
11
+ @conf = {
12
+ :autostart => false,
13
+ :timeout => 2500,
14
+ :heartbeat => 2500,
15
+ :reconnect => 2500
16
+ }
17
+
18
+ @conf.merge!(conf)
19
+
20
+ @liveness = 0
21
+ @heartbeat_at = 0
14
22
  @reply_to = nil
15
23
  @reply_rid = nil
16
24
  @reply_service = nil
25
+
26
+ init
17
27
 
18
- reconnect_to_broker
28
+ if @conf[:autostart]
29
+ start
30
+ end
19
31
  end
20
32
 
21
33
  def reply reply
22
34
  reply = [@reply_to, '', @reply_rid, '0'].concat([Oj.dump(reply)])
23
- send_to_broker Pigato::W_REPLY, reply
35
+ send Pigato::W_REPLY, reply
24
36
  end
25
37
 
26
38
  def recv
39
+
27
40
  loop do
41
+
42
+ iid = get_iid
43
+
44
+ socket = get_socket
45
+ return nil if socket.nil?
46
+
28
47
  @reply_rid = nil
29
48
  @reply_to = nil
30
49
  @reply_service = nil
31
50
 
32
- msg = @socket.recv_message
51
+ msg = socket.recv_message
33
52
 
34
53
  if msg && msg.size
35
54
  @liveness = HEARTBEAT_LIVENESS
@@ -55,54 +74,51 @@ class Pigato::Worker
55
74
  when Pigato::W_HEARTBEAT
56
75
  # do nothing
57
76
  when Pigato::W_DISCONNECT
58
- reconnect_to_broker
77
+ start
59
78
  else
60
79
  end
61
80
  else
62
81
  @liveness -= 1
63
82
  if @liveness == 0
64
- sleep 0.001*@reconnect
65
- reconnect_to_broker
83
+ sleep 0.001 * @conf[:reconnect]
84
+ start
66
85
  end
67
86
  end
68
87
 
69
88
  if Time.now > @heartbeat_at
70
- send_to_broker Pigato::W_HEARTBEAT
71
- @heartbeat_at = Time.now + 0.001 * @heartbeat
89
+ send Pigato::W_HEARTBEAT
90
+ @heartbeat_at = Time.now + 0.001 * @conf[:heartbeat]
72
91
  end
73
92
 
74
93
  end
75
94
  end
76
95
 
77
- def reconnect_to_broker
78
- if @socket
79
- @socket.close
80
- end
81
- if @ctx
82
- @ctx.destroy
83
- end
84
-
85
- @ctx = ZMQ::Context.new
86
- @socket = @ctx.socket ZMQ::DEALER
87
- @ctx.linger = 0
88
- @socket.identity = SecureRandom.uuid
89
- @socket.connect @broker
90
- @socket.rcvtimeo = @timeout;
91
- send_to_broker Pigato::W_READY, @service
96
+ def start
97
+ stop
98
+ sock_create
99
+ send Pigato::W_READY, @service
100
+ super
92
101
  @liveness = HEARTBEAT_LIVENESS
93
- @heartbeat_at = Time.now + 0.001 * @heartbeat
102
+ @heartbeat_at = Time.now + 0.001 * @conf[:heartbeat]
94
103
  end
95
104
 
96
- def send_to_broker command, data = nil
105
+ def stop
106
+ sock_close
107
+ super
108
+ end
109
+
110
+ def send command, data = nil
97
111
  if data.nil?
98
112
  data = []
99
113
  elsif not data.is_a?(Array)
100
114
  data = [data]
101
115
  end
102
116
 
117
+ socket = get_socket
118
+
103
119
  data = [Pigato::W_WORKER, command].concat data
104
120
  msg = ZMQ::Message.new
105
121
  data.reverse.each{|p| msg.push(ZMQ::Frame(p))}
106
- @socket.send_message msg
122
+ socket.send_message msg
107
123
  end
108
124
  end
data/lib/pigato.rb CHANGED
@@ -5,3 +5,5 @@ require_relative 'pigato/version.rb'
5
5
  require_relative 'pigato/proto.rb'
6
6
  require_relative 'pigato/client.rb'
7
7
  require_relative 'pigato/worker.rb'
8
+
9
+ require 'thread'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pigato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.27
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Ardoino
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-03 00:00:00.000000000 Z
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,6 +84,7 @@ files:
84
84
  - examples/echo_client.rb
85
85
  - examples/echo_worker.rb
86
86
  - lib/pigato.rb
87
+ - lib/pigato/base.rb
87
88
  - lib/pigato/client.rb
88
89
  - lib/pigato/proto.rb
89
90
  - lib/pigato/version.rb