polyphony 0.22 → 0.23
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/Gemfile.lock +9 -1
- data/TODO.md +13 -38
- data/docs/summary.md +19 -5
- data/docs/technical-overview/faq.md +12 -0
- data/examples/core/01-spinning-up-coprocesses.rb +2 -6
- data/examples/core/02-awaiting-coprocesses.rb +3 -1
- data/examples/core/03-interrupting.rb +3 -1
- data/examples/core/04-no-auto-run.rb +1 -3
- data/examples/core/cancel.rb +1 -1
- data/examples/core/channel_echo.rb +3 -1
- data/examples/core/defer.rb +3 -1
- data/examples/core/enumerator.rb +3 -1
- data/examples/core/error_bubbling.rb +35 -0
- data/examples/core/fork.rb +1 -1
- data/examples/core/genserver.rb +1 -1
- data/examples/core/lock.rb +3 -1
- data/examples/core/move_on.rb +1 -1
- data/examples/core/move_on_twice.rb +1 -1
- data/examples/core/move_on_with_ensure.rb +1 -1
- data/examples/core/move_on_with_value.rb +1 -1
- data/examples/core/multiple_spin.rb +3 -1
- data/examples/core/nested_cancel.rb +1 -1
- data/examples/core/nested_multiple_spin.rb +3 -1
- data/examples/core/nested_spin.rb +3 -1
- data/examples/core/pulse.rb +1 -1
- data/examples/core/resource.rb +1 -1
- data/examples/core/resource_cancel.rb +2 -2
- data/examples/core/resource_delegate.rb +1 -1
- data/examples/core/sleep.rb +1 -1
- data/examples/core/sleep_spin.rb +3 -1
- data/examples/core/snooze.rb +1 -1
- data/examples/core/spin_error.rb +2 -1
- data/examples/core/spin_error_backtrace.rb +1 -1
- data/examples/core/spin_uncaught_error.rb +3 -1
- data/examples/core/supervisor.rb +1 -1
- data/examples/core/supervisor_with_cancel_scope.rb +1 -1
- data/examples/core/supervisor_with_error.rb +3 -1
- data/examples/core/supervisor_with_manual_move_on.rb +1 -1
- data/examples/core/suspend.rb +1 -1
- data/examples/core/thread.rb +3 -3
- data/examples/core/thread_cancel.rb +6 -3
- data/examples/core/thread_pool.rb +8 -52
- data/examples/core/thread_pool_perf.rb +63 -0
- data/examples/core/throttle.rb +3 -1
- data/examples/core/timeout.rb +1 -1
- data/examples/core/wait_for_signal.rb +4 -2
- data/examples/fs/read.rb +1 -1
- data/examples/http/http2_raw.rb +1 -1
- data/examples/http/http_get.rb +1 -1
- data/examples/http/http_server.rb +2 -1
- data/examples/http/http_server_graceful.rb +3 -1
- data/examples/http/http_ws_server.rb +0 -2
- data/examples/http/https_wss_server.rb +0 -2
- data/examples/http/websocket_secure_server.rb +0 -2
- data/examples/http/websocket_server.rb +0 -2
- data/examples/interfaces/redis_channels.rb +3 -1
- data/examples/interfaces/redis_pubsub.rb +3 -1
- data/examples/interfaces/redis_pubsub_perf.rb +3 -1
- data/examples/io/backticks.rb +1 -1
- data/examples/io/cat.rb +1 -1
- data/examples/io/echo_client.rb +1 -1
- data/examples/io/echo_client_from_stdin.rb +3 -1
- data/examples/io/echo_pipe.rb +1 -1
- data/examples/io/echo_server.rb +1 -1
- data/examples/io/echo_server_with_timeout.rb +1 -1
- data/examples/io/echo_stdin.rb +1 -1
- data/examples/io/httparty_multi.rb +1 -1
- data/examples/io/io_read.rb +1 -1
- data/examples/io/irb.rb +1 -1
- data/examples/io/net-http.rb +1 -1
- data/examples/io/open.rb +1 -1
- data/examples/io/system.rb +1 -1
- data/examples/io/tcpserver.rb +1 -1
- data/examples/io/tcpsocket.rb +1 -1
- data/examples/performance/multi_snooze.rb +1 -1
- data/examples/performance/snooze.rb +18 -10
- data/ext/gyro/async.c +16 -9
- data/ext/gyro/child.c +2 -2
- data/ext/gyro/gyro.c +17 -10
- data/ext/gyro/gyro.h +2 -2
- data/ext/gyro/io.c +2 -2
- data/ext/gyro/signal.c +33 -35
- data/ext/gyro/timer.c +6 -73
- data/lib/polyphony.rb +6 -8
- data/lib/polyphony/core/cancel_scope.rb +32 -21
- data/lib/polyphony/core/coprocess.rb +26 -23
- data/lib/polyphony/core/global_api.rb +86 -0
- data/lib/polyphony/core/resource_pool.rb +1 -1
- data/lib/polyphony/core/supervisor.rb +47 -13
- data/lib/polyphony/core/thread.rb +10 -36
- data/lib/polyphony/core/thread_pool.rb +6 -26
- data/lib/polyphony/extensions/core.rb +30 -100
- data/lib/polyphony/extensions/io.rb +10 -7
- data/lib/polyphony/extensions/openssl.rb +18 -28
- data/lib/polyphony/http/client/agent.rb +15 -11
- data/lib/polyphony/http/client/http2.rb +1 -1
- data/lib/polyphony/version.rb +1 -1
- data/polyphony.gemspec +1 -0
- data/test/coverage.rb +45 -0
- data/test/helper.rb +15 -5
- data/test/test_async.rb +4 -4
- data/test/test_cancel_scope.rb +109 -0
- data/test/test_coprocess.rb +80 -36
- data/test/{test_core.rb → test_global_api.rb} +67 -13
- data/test/test_gyro.rb +1 -5
- data/test/test_io.rb +2 -2
- data/test/test_resource_pool.rb +19 -0
- data/test/test_signal.rb +10 -5
- data/test/test_supervisor.rb +168 -0
- data/test/test_timer.rb +31 -5
- metadata +23 -4
- data/lib/polyphony/auto_run.rb +0 -19
data/examples/core/pulse.rb
CHANGED
data/examples/core/resource.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
|
-
require 'polyphony
|
4
|
+
require 'polyphony'
|
5
5
|
|
6
6
|
resource_count = 0
|
7
7
|
Pool = Polyphony::ResourcePool.new(limit: 3) do
|
@@ -27,4 +27,4 @@ end
|
|
27
27
|
end
|
28
28
|
|
29
29
|
t0 = Time.now
|
30
|
-
|
30
|
+
throttled_loop(0.1) { puts "uptime: #{Time.now - t0}" }
|
data/examples/core/sleep.rb
CHANGED
data/examples/core/sleep_spin.rb
CHANGED
data/examples/core/snooze.rb
CHANGED
data/examples/core/spin_error.rb
CHANGED
data/examples/core/supervisor.rb
CHANGED
data/examples/core/suspend.rb
CHANGED
data/examples/core/thread.rb
CHANGED
@@ -7,7 +7,7 @@ def lengthy_op
|
|
7
7
|
IO.orig_read(__FILE__)
|
8
8
|
end
|
9
9
|
|
10
|
-
X =
|
10
|
+
X = 10000
|
11
11
|
|
12
12
|
def blocking
|
13
13
|
t0 = Time.now
|
@@ -18,8 +18,8 @@ end
|
|
18
18
|
|
19
19
|
def threaded
|
20
20
|
t0 = Time.now
|
21
|
-
data = Polyphony::Thread.
|
22
|
-
X.times { Polyphony::Thread.
|
21
|
+
data = Polyphony::Thread.spawn { lengthy_op }.await
|
22
|
+
X.times { Polyphony::Thread.spawn { lengthy_op } }
|
23
23
|
puts "read threaded #{data.bytesize} bytes (#{Time.now - t0}s)"
|
24
24
|
end
|
25
25
|
|
@@ -1,13 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
|
-
require 'polyphony
|
4
|
+
require 'polyphony'
|
5
5
|
|
6
6
|
@op_count = 0
|
7
7
|
|
8
8
|
def lengthy_op
|
9
9
|
100.times do
|
10
|
-
|
10
|
+
orig_sleep 0.01
|
11
11
|
@op_count += 1
|
12
12
|
end
|
13
13
|
@op_count
|
@@ -15,11 +15,14 @@ end
|
|
15
15
|
|
16
16
|
spin do
|
17
17
|
cancel_after(0.1) do
|
18
|
-
data = Polyphony::Thread.
|
18
|
+
data = Polyphony::Thread.process { lengthy_op }
|
19
19
|
puts "slept #{data} times"
|
20
20
|
end
|
21
21
|
rescue Exception => e
|
22
22
|
puts "error: #{e}"
|
23
23
|
ensure
|
24
|
+
sleep 0.1
|
24
25
|
puts "slept #{@op_count} times"
|
25
26
|
end
|
27
|
+
|
28
|
+
suspend
|
@@ -1,61 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
|
-
require 'polyphony
|
4
|
+
require 'polyphony'
|
5
5
|
|
6
6
|
def lengthy_op
|
7
|
-
data = IO.
|
8
|
-
data.
|
9
|
-
# Socket.getaddrinfo('debian.org', 80)
|
10
|
-
# Digest::SHA256.digest(IO.read('doc/Promise.html'))
|
7
|
+
data = IO.orig_read(__FILE__)
|
8
|
+
data.bytesize
|
11
9
|
end
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
X.times { lengthy_op }
|
18
|
-
native_perf = X / (Time.now - t0)
|
19
|
-
puts "native performance: #{native_perf}"
|
20
|
-
# puts "*" * 40
|
21
|
-
|
22
|
-
begin
|
23
|
-
1.times do
|
24
|
-
t0 = Time.now
|
25
|
-
X.times do
|
26
|
-
Polyphony::ThreadPool.process { lengthy_op }
|
27
|
-
end
|
28
|
-
async_perf = X / (Time.now - t0)
|
29
|
-
puts format(
|
30
|
-
'seq thread pool performance: %g (X %0.2f)',
|
31
|
-
async_perf,
|
32
|
-
async_perf / native_perf
|
33
|
-
)
|
34
|
-
end
|
35
|
-
|
36
|
-
acc = 0
|
37
|
-
count = 0
|
38
|
-
10.times do |_i|
|
39
|
-
t0 = Time.now
|
40
|
-
supervise do |s|
|
41
|
-
X.times do
|
42
|
-
s.spin { Polyphony::ThreadPool.process { lengthy_op } }
|
43
|
-
end
|
44
|
-
end
|
45
|
-
thread_pool_perf = X / (Time.now - t0)
|
46
|
-
acc += thread_pool_perf
|
47
|
-
count += 1
|
48
|
-
end
|
49
|
-
avg_perf = acc / count
|
50
|
-
puts format(
|
51
|
-
'avg thread pool performance: %g (X %0.2f)',
|
52
|
-
avg_perf,
|
53
|
-
avg_perf / native_perf
|
54
|
-
)
|
55
|
-
rescue Exception => e
|
56
|
-
p e
|
57
|
-
puts e.backtrace.join("\n")
|
58
|
-
end
|
11
|
+
10.times do |_i|
|
12
|
+
spin {
|
13
|
+
p [_i, 2, Polyphony::ThreadPool.process { lengthy_op }]
|
14
|
+
}
|
59
15
|
end
|
60
16
|
|
61
|
-
|
17
|
+
suspend
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony'
|
5
|
+
|
6
|
+
def lengthy_op
|
7
|
+
data = IO.orig_read(__FILE__)
|
8
|
+
data.clear
|
9
|
+
# Socket.getaddrinfo('debian.org', 80)
|
10
|
+
# Digest::SHA256.digest(IO.read('doc/Promise.html'))
|
11
|
+
end
|
12
|
+
|
13
|
+
X = 100
|
14
|
+
|
15
|
+
def compare_performance
|
16
|
+
t0 = Time.now
|
17
|
+
X.times { lengthy_op }
|
18
|
+
native_perf = X / (Time.now - t0)
|
19
|
+
puts "native performance: #{native_perf}"
|
20
|
+
# puts "*" * 40
|
21
|
+
|
22
|
+
begin
|
23
|
+
1.times do
|
24
|
+
t0 = Time.now
|
25
|
+
X.times do
|
26
|
+
Polyphony::ThreadPool.process { lengthy_op }
|
27
|
+
end
|
28
|
+
async_perf = X / (Time.now - t0)
|
29
|
+
puts format(
|
30
|
+
'seq thread pool performance: %g (X %0.2f)',
|
31
|
+
async_perf,
|
32
|
+
async_perf / native_perf
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
acc = 0
|
37
|
+
count = 0
|
38
|
+
10.times do |_i|
|
39
|
+
t0 = Time.now
|
40
|
+
supervise do |s|
|
41
|
+
X.times do
|
42
|
+
s.spin { Polyphony::ThreadPool.process { lengthy_op } }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
thread_pool_perf = X / (Time.now - t0)
|
46
|
+
acc += thread_pool_perf
|
47
|
+
count += 1
|
48
|
+
end
|
49
|
+
avg_perf = acc / count
|
50
|
+
puts format(
|
51
|
+
'avg thread pool performance: %g (X %0.2f)',
|
52
|
+
avg_perf,
|
53
|
+
avg_perf / native_perf
|
54
|
+
)
|
55
|
+
rescue Exception => e
|
56
|
+
p e
|
57
|
+
puts e.backtrace.join("\n")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
spin { compare_performance }
|
62
|
+
|
63
|
+
suspend
|
data/examples/core/throttle.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
|
-
require 'polyphony
|
4
|
+
require 'polyphony'
|
5
5
|
|
6
6
|
spin do
|
7
7
|
throttled_loop(3) { STDOUT << '.' }
|
@@ -14,3 +14,5 @@ end
|
|
14
14
|
spin do
|
15
15
|
throttled_loop(interval: 1) { STDOUT << '*' }
|
16
16
|
end
|
17
|
+
|
18
|
+
suspend
|
data/examples/core/timeout.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/setup'
|
4
|
-
require 'polyphony
|
4
|
+
require 'polyphony'
|
5
5
|
|
6
6
|
waiter = spin do
|
7
7
|
puts 'Waiting for HUP'
|
@@ -11,4 +11,6 @@ end
|
|
11
11
|
|
12
12
|
sleep 1
|
13
13
|
puts 'Sending HUP'
|
14
|
-
Process.kill('SIGHUP', Process.pid)
|
14
|
+
Process.kill('SIGHUP', Process.pid)
|
15
|
+
|
16
|
+
suspend
|