rbgo 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +40 -1
- data/lib/rbgo/select_chan.rb +90 -25
- data/lib/rbgo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83acff2fb4a9fbfa2d789ae9cc43db9b8e08fa6ad9e09c47822549945f0324d8
|
4
|
+
data.tar.gz: fc88b7550a6af76531668b20b7e7287d2db4ce73436bea75acb7ee05ddcecd02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac2d5d12d5ba6832f715c2a1df977c9ef435b94ee1f4d5a4259279f1dd8d871ded805ed5a70698fd8c186065355213db9bb003963bfde908a93caf01036b2652
|
7
|
+
data.tar.gz: cda8846388ff6c7853490765111f9e49ac80e73c028ed44ec8ed72e93dd538b476f5888d2f649d07740470e42b72308904f42948c052ed77ea49e9a00e9d0491
|
data/README.md
CHANGED
@@ -29,7 +29,46 @@ select_chan(
|
|
29
29
|
on_write(chan: ch3, obj: 'world'){
|
30
30
|
#do when write success
|
31
31
|
}
|
32
|
-
){ puts 'call default block' }
|
32
|
+
){ puts 'call default block' }
|
33
|
+
|
34
|
+
|
35
|
+
timeout_ch = Chan.after(4)
|
36
|
+
job_ch1 = Chan.perform do
|
37
|
+
#do some job
|
38
|
+
sleep
|
39
|
+
end
|
40
|
+
|
41
|
+
job_ch2 = Chan.perform(timeout: 3) do
|
42
|
+
#do some job
|
43
|
+
sleep
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
select_chan(
|
48
|
+
on_read(chan: job_ch1){
|
49
|
+
p :job_ch1
|
50
|
+
},
|
51
|
+
on_read(chan: job_ch2){
|
52
|
+
p :job_ch2
|
53
|
+
},
|
54
|
+
on_read(chan: timeout_ch){
|
55
|
+
p 'timeout'
|
56
|
+
}
|
57
|
+
)
|
58
|
+
|
59
|
+
# Chan is Enumerable
|
60
|
+
|
61
|
+
ch = Chan.new
|
62
|
+
|
63
|
+
go do
|
64
|
+
10.times do
|
65
|
+
ch << Time.now
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
ch.each do|obj|
|
70
|
+
p obj
|
71
|
+
end
|
33
72
|
```
|
34
73
|
# go
|
35
74
|
create lightweight routine like golang
|
data/lib/rbgo/select_chan.rb
CHANGED
@@ -3,8 +3,25 @@ require 'thread'
|
|
3
3
|
module Rbgo
|
4
4
|
module Channel
|
5
5
|
module Chan
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def each
|
9
|
+
if block_given?
|
10
|
+
loop do
|
11
|
+
obj, ok = pop
|
12
|
+
if ok
|
13
|
+
yield obj
|
14
|
+
else
|
15
|
+
break
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
enum_for(:each)
|
20
|
+
end
|
21
|
+
end
|
6
22
|
|
7
23
|
def self.new(max = 0)
|
24
|
+
max = max.to_i
|
8
25
|
if max <= 0
|
9
26
|
NonBufferChan.new
|
10
27
|
else
|
@@ -12,6 +29,45 @@ module Rbgo
|
|
12
29
|
end
|
13
30
|
end
|
14
31
|
|
32
|
+
def self.after(seconds)
|
33
|
+
ch = new
|
34
|
+
CoRun::Routine.new(new_thread: true, queue_tag: :none) do
|
35
|
+
sleep seconds
|
36
|
+
ch << Time.now rescue nil
|
37
|
+
ch.close
|
38
|
+
end
|
39
|
+
ch
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.tick(every_seconds)
|
43
|
+
ch = new
|
44
|
+
CoRun::Routine.new(new_thread: true, queue_tag: :none) do
|
45
|
+
loop do
|
46
|
+
sleep every_seconds
|
47
|
+
begin
|
48
|
+
ch.enq(Time.now, true)
|
49
|
+
rescue ThreadError
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
ch
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.perform(timeout: nil, &blk)
|
57
|
+
ch = new
|
58
|
+
CoRun::Routine.new(new_thread: false, queue_tag: :default) do
|
59
|
+
begin
|
60
|
+
Timeout::timeout(timeout) do
|
61
|
+
blk.call
|
62
|
+
end
|
63
|
+
rescue Timeout::Error
|
64
|
+
ensure
|
65
|
+
ch.close
|
66
|
+
end
|
67
|
+
end
|
68
|
+
ch
|
69
|
+
end
|
70
|
+
|
15
71
|
private
|
16
72
|
|
17
73
|
attr_accessor :ios
|
@@ -183,38 +239,37 @@ module Rbgo
|
|
183
239
|
#
|
184
240
|
#
|
185
241
|
class BufferChan < SizedQueue
|
242
|
+
alias_method :queue_max, :max
|
243
|
+
alias_method :queue_max=, :max=
|
186
244
|
include Chan
|
187
|
-
|
188
|
-
|
189
|
-
def each
|
190
|
-
if block_given?
|
191
|
-
loop do
|
192
|
-
begin
|
193
|
-
yield pop(true)
|
194
|
-
rescue ThreadError
|
195
|
-
return
|
196
|
-
end
|
197
|
-
end
|
198
|
-
else
|
199
|
-
enum_for(:each)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
245
|
+
|
203
246
|
def initialize(max)
|
204
247
|
super(max)
|
205
|
-
@mutex
|
206
|
-
|
248
|
+
@mutex = Mutex.new
|
249
|
+
@cond = ConditionVariable.new
|
207
250
|
self.ios = []
|
208
251
|
self.register_mutex = Mutex.new
|
209
252
|
end
|
210
253
|
|
211
254
|
def push(obj, nonblock = false)
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
255
|
+
@mutex.synchronize do
|
256
|
+
begin
|
257
|
+
if nonblock
|
258
|
+
super(obj, true)
|
259
|
+
else
|
260
|
+
while length == queue_max && !closed?
|
261
|
+
@cond.wait(@mutex)
|
262
|
+
end
|
263
|
+
super(obj, false)
|
264
|
+
end
|
265
|
+
notify
|
266
|
+
@cond.broadcast
|
267
|
+
self
|
268
|
+
rescue ThreadError
|
269
|
+
raise ClosedQueueError.new if closed?
|
270
|
+
raise
|
271
|
+
end
|
272
|
+
end
|
218
273
|
end
|
219
274
|
|
220
275
|
def pop(nonblock = false)
|
@@ -223,8 +278,17 @@ module Rbgo
|
|
223
278
|
ok = true
|
224
279
|
ok = false if empty? && closed?
|
225
280
|
begin
|
226
|
-
|
281
|
+
if nonblock
|
282
|
+
res = super(true)
|
283
|
+
else
|
284
|
+
while empty? && !closed?
|
285
|
+
@cond.wait(@mutex)
|
286
|
+
end
|
287
|
+
ok = false if closed?
|
288
|
+
res = super(false)
|
289
|
+
end
|
227
290
|
notify
|
291
|
+
@cond.broadcast
|
228
292
|
rescue ThreadError
|
229
293
|
raise unless closed?
|
230
294
|
ok = false
|
@@ -243,6 +307,7 @@ module Rbgo
|
|
243
307
|
@mutex.synchronize do
|
244
308
|
super
|
245
309
|
notify
|
310
|
+
@cond.broadcast
|
246
311
|
self
|
247
312
|
end
|
248
313
|
end
|
data/lib/rbgo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rbgo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wang Yin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: system
|