polyphony 0.36 → 0.38
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/CHANGELOG.md +9 -0
- data/Gemfile +0 -11
- data/Gemfile.lock +1 -3
- data/Rakefile +4 -0
- data/TODO.md +12 -10
- data/docs/index.md +2 -1
- data/examples/core/xx-fork-cleanup.rb +22 -0
- data/ext/gyro/async.c +27 -13
- data/ext/gyro/child.c +29 -15
- data/ext/gyro/fiber.c +3 -1
- data/ext/gyro/gyro.c +0 -6
- data/ext/gyro/gyro.h +6 -0
- data/ext/gyro/io.c +24 -9
- data/ext/gyro/queue.c +21 -21
- data/ext/gyro/selector.c +23 -0
- data/ext/gyro/signal.c +24 -9
- data/ext/gyro/thread.c +12 -2
- data/ext/gyro/timer.c +33 -18
- data/lib/polyphony.rb +27 -36
- data/lib/polyphony/adapters/fs.rb +1 -4
- data/lib/polyphony/adapters/process.rb +29 -25
- data/lib/polyphony/adapters/trace.rb +129 -124
- data/lib/polyphony/core/channel.rb +36 -36
- data/lib/polyphony/core/exceptions.rb +29 -29
- data/lib/polyphony/core/global_api.rb +92 -91
- data/lib/polyphony/core/resource_pool.rb +84 -84
- data/lib/polyphony/core/sync.rb +17 -17
- data/lib/polyphony/core/thread_pool.rb +49 -37
- data/lib/polyphony/core/throttler.rb +25 -25
- data/lib/polyphony/extensions/core.rb +3 -3
- data/lib/polyphony/extensions/fiber.rb +269 -267
- data/lib/polyphony/extensions/openssl.rb +1 -1
- data/lib/polyphony/extensions/socket.rb +2 -1
- data/lib/polyphony/extensions/thread.rb +3 -3
- data/lib/polyphony/net.rb +71 -67
- data/lib/polyphony/version.rb +1 -1
- data/polyphony.gemspec +0 -3
- data/test/stress.rb +17 -12
- data/test/test_thread.rb +1 -0
- data/test/test_thread_pool.rb +2 -2
- data/test/test_throttler.rb +0 -1
- metadata +3 -16
@@ -1,119 +1,120 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
require_relative '../extensions/core'
|
4
|
+
require_relative '../extensions/fiber'
|
5
|
+
require_relative './exceptions'
|
6
|
+
require_relative './throttler'
|
7
|
+
|
8
|
+
module Polyphony
|
9
|
+
# Global API methods to be included in ::Object
|
10
|
+
module GlobalAPI
|
11
|
+
def after(interval, &block)
|
12
|
+
spin do
|
13
|
+
sleep interval
|
14
|
+
block.()
|
15
|
+
end
|
16
|
+
end
|
10
17
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
def cancel_after(interval, &block)
|
19
|
+
fiber = ::Fiber.current
|
20
|
+
canceller = spin do
|
21
|
+
sleep interval
|
22
|
+
fiber.schedule Polyphony::Cancel.new
|
23
|
+
end
|
24
|
+
block ? cancel_after_wrap_block(canceller, &block) : canceller
|
17
25
|
end
|
18
|
-
end
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
fiber.schedule Exceptions::Cancel.new
|
27
|
+
def cancel_after_wrap_block(canceller, &block)
|
28
|
+
block.call
|
29
|
+
ensure
|
30
|
+
canceller.stop
|
25
31
|
end
|
26
|
-
block ? cancel_after_wrap_block(canceller, &block) : canceller
|
27
|
-
end
|
28
32
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
canceller.stop
|
33
|
-
end
|
33
|
+
def spin(tag = nil, &block)
|
34
|
+
Fiber.current.spin(tag, caller, &block)
|
35
|
+
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
def spin_loop(tag = nil, rate: nil, &block)
|
38
|
+
if rate
|
39
|
+
Fiber.current.spin(tag, caller) do
|
40
|
+
throttled_loop(rate, &block)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
Fiber.current.spin(tag, caller) { loop(&block) }
|
44
|
+
end
|
45
|
+
end
|
38
46
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
47
|
+
def every(interval)
|
48
|
+
timer = Gyro::Timer.new(interval, interval)
|
49
|
+
loop do
|
50
|
+
timer.await
|
51
|
+
yield
|
43
52
|
end
|
44
|
-
|
45
|
-
|
53
|
+
ensure
|
54
|
+
timer.stop
|
46
55
|
end
|
47
|
-
end
|
48
56
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
57
|
+
def move_on_after(interval, with_value: nil, &block)
|
58
|
+
fiber = ::Fiber.current
|
59
|
+
unless block
|
60
|
+
return spin do
|
61
|
+
sleep interval
|
62
|
+
fiber.schedule with_value
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
move_on_after_with_block(fiber, interval, with_value, &block)
|
54
67
|
end
|
55
|
-
ensure
|
56
|
-
timer.stop
|
57
|
-
end
|
58
68
|
|
59
|
-
|
60
|
-
|
61
|
-
unless block
|
62
|
-
return spin do
|
69
|
+
def move_on_after_with_block(fiber, interval, with_value, &block)
|
70
|
+
canceller = spin do
|
63
71
|
sleep interval
|
64
|
-
fiber.schedule with_value
|
72
|
+
fiber.schedule Polyphony::MoveOn.new(with_value)
|
65
73
|
end
|
74
|
+
block.call
|
75
|
+
rescue Polyphony::MoveOn => e
|
76
|
+
e.value
|
77
|
+
ensure
|
78
|
+
canceller.stop
|
66
79
|
end
|
67
80
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
def move_on_after_with_block(fiber, interval, with_value, &block)
|
72
|
-
canceller = spin do
|
73
|
-
sleep interval
|
74
|
-
fiber.schedule Exceptions::MoveOn.new(with_value)
|
81
|
+
def receive
|
82
|
+
Fiber.current.receive
|
75
83
|
end
|
76
|
-
block.call
|
77
|
-
rescue Exceptions::MoveOn => e
|
78
|
-
e.value
|
79
|
-
ensure
|
80
|
-
canceller.stop
|
81
|
-
end
|
82
84
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
def receive_pending
|
88
|
-
Fiber.current.receive_pending
|
89
|
-
end
|
85
|
+
def receive_pending
|
86
|
+
Fiber.current.receive_pending
|
87
|
+
end
|
90
88
|
|
91
|
-
|
92
|
-
|
93
|
-
|
89
|
+
def supervise(*args, &block)
|
90
|
+
Fiber.current.supervise(*args, &block)
|
91
|
+
end
|
94
92
|
|
95
|
-
|
96
|
-
|
93
|
+
def sleep(duration = nil)
|
94
|
+
return sleep_forever unless duration
|
97
95
|
|
98
|
-
|
99
|
-
|
100
|
-
|
96
|
+
timer = Gyro::Timer.new(duration, 0)
|
97
|
+
timer.await
|
98
|
+
end
|
101
99
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
100
|
+
def sleep_forever
|
101
|
+
Thread.current.fiber_ref
|
102
|
+
suspend
|
103
|
+
ensure
|
104
|
+
Thread.current.fiber_unref
|
105
|
+
end
|
108
106
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
107
|
+
def throttled_loop(rate, count: nil, &block)
|
108
|
+
throttler = Polyphony::Throttler.new(rate)
|
109
|
+
if count
|
110
|
+
count.times { throttler.(&block) }
|
111
|
+
else
|
112
|
+
loop { throttler.(&block) }
|
113
|
+
end
|
114
|
+
ensure
|
115
|
+
throttler.stop
|
115
116
|
end
|
116
|
-
ensure
|
117
|
-
throttler.stop
|
118
117
|
end
|
119
118
|
end
|
119
|
+
|
120
|
+
Object.include Polyphony::GlobalAPI
|
@@ -1,107 +1,107 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
module Polyphony
|
4
|
+
# Implements a limited resource pool
|
5
|
+
class ResourcePool
|
6
|
+
attr_reader :limit, :size
|
7
|
+
|
8
|
+
# Initializes a new resource pool
|
9
|
+
# @param opts [Hash] options
|
10
|
+
# @param &block [Proc] allocator block
|
11
|
+
def initialize(opts, &block)
|
12
|
+
@allocator = block
|
13
|
+
|
14
|
+
@stock = []
|
15
|
+
@queue = []
|
16
|
+
|
17
|
+
@limit = opts[:limit] || 4
|
18
|
+
@size = 0
|
19
|
+
end
|
14
20
|
|
15
|
-
|
16
|
-
|
21
|
+
def available
|
22
|
+
@stock.size
|
23
|
+
end
|
17
24
|
|
18
|
-
|
19
|
-
|
20
|
-
|
25
|
+
def acquire
|
26
|
+
Gyro.ref
|
27
|
+
resource = wait_for_resource
|
28
|
+
return unless resource
|
21
29
|
|
22
|
-
|
23
|
-
|
24
|
-
|
30
|
+
yield resource
|
31
|
+
ensure
|
32
|
+
Gyro.unref
|
33
|
+
release(resource) if resource
|
34
|
+
end
|
25
35
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
36
|
+
def wait_for_resource
|
37
|
+
fiber = Fiber.current
|
38
|
+
@queue << fiber
|
39
|
+
ready_resource = from_stock
|
40
|
+
return ready_resource if ready_resource
|
30
41
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
42
|
+
suspend
|
43
|
+
ensure
|
44
|
+
@queue.delete(fiber)
|
45
|
+
end
|
36
46
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
47
|
+
def release(resource)
|
48
|
+
if resource.__discarded__
|
49
|
+
@size -= 1
|
50
|
+
elsif resource
|
51
|
+
return_to_stock(resource)
|
52
|
+
dequeue
|
53
|
+
end
|
54
|
+
end
|
42
55
|
|
43
|
-
|
44
|
-
|
45
|
-
@queue.delete(fiber)
|
46
|
-
end
|
56
|
+
def dequeue
|
57
|
+
return if @queue.empty? || @stock.empty?
|
47
58
|
|
48
|
-
|
49
|
-
if resource.__discarded__
|
50
|
-
@size -= 1
|
51
|
-
elsif resource
|
52
|
-
return_to_stock(resource)
|
53
|
-
dequeue
|
59
|
+
@queue.shift.schedule(@stock.shift)
|
54
60
|
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def dequeue
|
58
|
-
return if @queue.empty? || @stock.empty?
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
+
def return_to_stock(resource)
|
63
|
+
@stock << resource
|
64
|
+
end
|
62
65
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
+
def from_stock
|
67
|
+
@stock.shift || (@size < @limit && allocate)
|
68
|
+
end
|
66
69
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
+
def method_missing(sym, *args, &block)
|
71
|
+
acquire { |r| r.send(sym, *args, &block) }
|
72
|
+
end
|
70
73
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
+
def respond_to_missing?(*_args)
|
75
|
+
true
|
76
|
+
end
|
74
77
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
+
# Extension to allow discarding of resources
|
79
|
+
module ResourceExtensions
|
80
|
+
def __discarded__
|
81
|
+
@__discarded__
|
82
|
+
end
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
@__discarded__
|
84
|
+
def __discard__
|
85
|
+
@__discarded__ = true
|
86
|
+
end
|
83
87
|
end
|
84
88
|
|
85
|
-
|
86
|
-
|
89
|
+
# Allocates a resource
|
90
|
+
# @return [any] allocated resource
|
91
|
+
def allocate
|
92
|
+
@size += 1
|
93
|
+
@allocator.().tap { |r| r.extend ResourceExtensions }
|
87
94
|
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# Allocates a resource
|
91
|
-
# @return [any] allocated resource
|
92
|
-
def allocate
|
93
|
-
@size += 1
|
94
|
-
@allocator.().tap { |r| r.extend ResourceExtensions }
|
95
|
-
end
|
96
95
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
96
|
+
def <<(resource)
|
97
|
+
@size += 1
|
98
|
+
resource.extend ResourceExtensions
|
99
|
+
@stock << resource
|
100
|
+
dequeue
|
101
|
+
end
|
103
102
|
|
104
|
-
|
105
|
-
|
103
|
+
def preheat!
|
104
|
+
(@limit - @size).times { @stock << allocate }
|
105
|
+
end
|
106
106
|
end
|
107
|
-
end
|
107
|
+
end
|
data/lib/polyphony/core/sync.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
module Polyphony
|
4
|
+
# Implements mutex lock for synchronizing access to a shared resource
|
5
|
+
class Mutex
|
6
|
+
def initialize
|
7
|
+
@waiting_fibers = Gyro::Queue.new
|
8
|
+
end
|
4
9
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
10
|
+
def synchronize
|
11
|
+
fiber = Fiber.current
|
12
|
+
@waiting_fibers << fiber
|
13
|
+
suspend if @waiting_fibers.size > 1
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
@waiting_fibers.delete(fiber)
|
17
|
+
@waiting_fibers.first&.schedule
|
18
|
+
snooze
|
19
|
+
end
|
9
20
|
end
|
10
|
-
|
11
|
-
def synchronize
|
12
|
-
fiber = Fiber.current
|
13
|
-
@waiting_fibers << fiber
|
14
|
-
suspend if @waiting_fibers.size > 1
|
15
|
-
yield
|
16
|
-
ensure
|
17
|
-
@waiting_fibers.delete(fiber)
|
18
|
-
@waiting_fibers.first&.schedule
|
19
|
-
snooze
|
20
|
-
end
|
21
|
-
end
|
21
|
+
end
|
@@ -1,52 +1,64 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
export_default :ThreadPool
|
4
|
-
|
5
3
|
require 'etc'
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
module Polyphony
|
6
|
+
# Implements a pool of threads
|
7
|
+
class ThreadPool
|
8
|
+
attr_reader :size
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def self.process(&block)
|
11
|
+
@default_pool ||= self.new
|
12
|
+
@default_pool.process(&block)
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
def self.reset
|
16
|
+
return unless @default_pool
|
17
|
+
|
18
|
+
@default_pool.stop
|
19
|
+
@default_pool = nil
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
def initialize(size = Etc.nprocessors)
|
23
|
+
@size = size
|
24
|
+
@task_queue = Gyro::Queue.new
|
25
|
+
@threads = (1..@size).map { Thread.new { thread_loop } }
|
26
|
+
end
|
24
27
|
|
25
|
-
|
26
|
-
|
27
|
-
async.await
|
28
|
-
end
|
28
|
+
def process(&block)
|
29
|
+
setup unless @task_queue
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
async = Fiber.current.auto_async
|
32
|
+
@task_queue << [block, async]
|
33
|
+
async.await
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
+
def cast(&block)
|
37
|
+
setup unless @task_queue
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
@task_queue << [block, nil]
|
40
|
+
self
|
41
|
+
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
def busy?
|
44
|
+
!@task_queue.empty?
|
45
|
+
end
|
46
|
+
|
47
|
+
def thread_loop
|
48
|
+
loop { run_queued_task }
|
49
|
+
end
|
50
|
+
|
51
|
+
def run_queued_task
|
52
|
+
(block, watcher) = @task_queue.pop
|
53
|
+
result = block.()
|
54
|
+
watcher&.signal(result)
|
55
|
+
rescue Exception => e
|
56
|
+
watcher ? watcher.signal(e) : raise(e)
|
57
|
+
end
|
44
58
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
rescue Exception => e
|
50
|
-
watcher ? watcher.signal(e) : raise(e)
|
59
|
+
def stop
|
60
|
+
@threads.each(&:kill)
|
61
|
+
@threads.each(&:join)
|
62
|
+
end
|
51
63
|
end
|
52
64
|
end
|