polyphony 0.23 → 0.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/Gemfile.lock +4 -10
  4. data/README.md +0 -4
  5. data/TODO.md +5 -56
  6. data/docs/README.md +4 -7
  7. data/examples/core/{channel_echo.rb → xx-channels.rb} +0 -0
  8. data/examples/core/{defer.rb → xx-deferring-an-operation.rb} +0 -0
  9. data/examples/core/{genserver.rb → xx-erlang-style-genserver.rb} +2 -2
  10. data/examples/core/{fork.rb → xx-forking.rb} +2 -0
  11. data/examples/core/xx-move_on.rb +23 -0
  12. data/examples/core/{pulse.rb → xx-recurrent-timer.rb} +3 -2
  13. data/examples/core/{resource_cancel.rb → xx-resource_cancel.rb} +1 -2
  14. data/examples/core/{resource_delegate.rb → xx-resource_delegate.rb} +0 -0
  15. data/examples/core/{wait_for_signal.rb → xx-signals.rb} +0 -0
  16. data/examples/core/{sleep.rb → xx-sleeping.rb} +0 -0
  17. data/examples/core/{spin_error_backtrace.rb → xx-spin_error_backtrace.rb} +5 -2
  18. data/examples/core/{supervisor.rb → xx-supervisors.rb} +0 -0
  19. data/examples/core/{thread_cancel.rb → xx-thread_cancel.rb} +0 -0
  20. data/examples/core/{thread_pool.rb → xx-thread_pool.rb} +0 -0
  21. data/examples/core/{throttle.rb → xx-throttling.rb} +0 -0
  22. data/examples/core/{timeout.rb → xx-timeout.rb} +0 -0
  23. data/examples/core/{lock.rb → xx-using-a-mutex.rb} +0 -0
  24. data/examples/io/{backticks.rb → xx-backticks.rb} +0 -0
  25. data/examples/io/{echo_client.rb → xx-echo_client.rb} +0 -1
  26. data/examples/io/{echo_client_from_stdin.rb → xx-echo_client_from_stdin.rb} +0 -1
  27. data/examples/io/{echo_pipe.rb → xx-echo_pipe.rb} +0 -0
  28. data/examples/io/{echo_server.rb → xx-echo_server.rb} +0 -0
  29. data/examples/io/{echo_server_with_timeout.rb → xx-echo_server_with_timeout.rb} +0 -0
  30. data/examples/io/{echo_stdin.rb → xx-echo_stdin.rb} +0 -0
  31. data/examples/io/xx-httparty.rb +13 -0
  32. data/examples/io/{irb.rb → xx-irb.rb} +0 -0
  33. data/examples/io/{net-http.rb → xx-net-http.rb} +0 -0
  34. data/examples/io/{open.rb → xx-open.rb} +0 -1
  35. data/examples/io/{system.rb → xx-system.rb} +0 -0
  36. data/examples/io/{tcpserver.rb → xx-tcpserver.rb} +0 -0
  37. data/examples/io/{tcpsocket.rb → xx-tcpsocket.rb} +0 -1
  38. data/examples/{fs/read.rb → performance/fs_read.rb} +0 -0
  39. data/examples/{core → performance}/mem-usage.rb +1 -0
  40. data/examples/performance/multi_snooze.rb +2 -0
  41. data/examples/performance/snooze.rb +2 -0
  42. data/examples/performance/thread-vs-fiber/polyphony_server.rb +5 -3
  43. data/examples/performance/thread-vs-fiber/threaded_server.rb +1 -1
  44. data/examples/{io/httparty_multi.rb → performance/thread-vs-fiber/xx-httparty_multi.rb} +16 -13
  45. data/examples/{io/httparty_threaded.rb → performance/thread-vs-fiber/xx-httparty_threaded.rb} +2 -2
  46. data/examples/performance/thread.rb +27 -0
  47. data/examples/{core → performance}/thread_pool_perf.rb +0 -0
  48. data/ext/gyro/extconf.rb +1 -0
  49. data/lib/polyphony/extensions/core.rb +0 -5
  50. data/lib/polyphony/version.rb +1 -1
  51. data/polyphony.gemspec +3 -9
  52. metadata +59 -167
  53. data/bin/poly +0 -11
  54. data/examples/core/cancel.rb +0 -13
  55. data/examples/core/enumerator.rb +0 -15
  56. data/examples/core/error_bubbling.rb +0 -35
  57. data/examples/core/fiber_error.rb +0 -9
  58. data/examples/core/fiber_error_with_backtrace.rb +0 -73
  59. data/examples/core/move_on.rb +0 -11
  60. data/examples/core/move_on_twice.rb +0 -16
  61. data/examples/core/move_on_with_ensure.rb +0 -13
  62. data/examples/core/move_on_with_value.rb +0 -14
  63. data/examples/core/multiple_spin.rb +0 -18
  64. data/examples/core/nested_cancel.rb +0 -40
  65. data/examples/core/nested_multiple_spin.rb +0 -20
  66. data/examples/core/nested_spin.rb +0 -19
  67. data/examples/core/pingpong.rb +0 -21
  68. data/examples/core/resource.rb +0 -30
  69. data/examples/core/sleep_spin.rb +0 -21
  70. data/examples/core/snooze.rb +0 -32
  71. data/examples/core/spin_error.rb +0 -17
  72. data/examples/core/spin_uncaught_error.rb +0 -16
  73. data/examples/core/supervisor_with_cancel_scope.rb +0 -23
  74. data/examples/core/supervisor_with_error.rb +0 -24
  75. data/examples/core/supervisor_with_manual_move_on.rb +0 -23
  76. data/examples/core/suspend.rb +0 -13
  77. data/examples/core/thread.rb +0 -27
  78. data/examples/http/config.ru +0 -7
  79. data/examples/http/cuba.ru +0 -22
  80. data/examples/http/happy_eyeballs.rb +0 -37
  81. data/examples/http/http2_raw.rb +0 -135
  82. data/examples/http/http_client.rb +0 -28
  83. data/examples/http/http_get.rb +0 -33
  84. data/examples/http/http_parse_experiment.rb +0 -123
  85. data/examples/http/http_proxy.rb +0 -83
  86. data/examples/http/http_server.js +0 -24
  87. data/examples/http/http_server.rb +0 -28
  88. data/examples/http/http_server_forked.rb +0 -29
  89. data/examples/http/http_server_graceful.rb +0 -27
  90. data/examples/http/http_server_simple.rb +0 -11
  91. data/examples/http/http_server_throttled.rb +0 -15
  92. data/examples/http/http_ws_server.rb +0 -37
  93. data/examples/http/https_raw_client.rb +0 -12
  94. data/examples/http/https_server.rb +0 -22
  95. data/examples/http/https_wss_server.rb +0 -39
  96. data/examples/http/rack_server.rb +0 -12
  97. data/examples/http/rack_server_https.rb +0 -19
  98. data/examples/http/rack_server_https_forked.rb +0 -27
  99. data/examples/http/websocket_secure_server.rb +0 -27
  100. data/examples/http/websocket_server.rb +0 -24
  101. data/examples/http/ws_page.html +0 -34
  102. data/examples/http/wss_page.html +0 -34
  103. data/examples/io/cat.rb +0 -12
  104. data/examples/io/httparty.rb +0 -10
  105. data/examples/io/io_read.rb +0 -9
  106. data/lib/ev_ext.bundle +0 -0
  107. data/lib/polyphony/http.rb +0 -16
  108. data/lib/polyphony/http/client/agent.rb +0 -131
  109. data/lib/polyphony/http/client/http1.rb +0 -129
  110. data/lib/polyphony/http/client/http2.rb +0 -180
  111. data/lib/polyphony/http/client/response.rb +0 -32
  112. data/lib/polyphony/http/client/site_connection_manager.rb +0 -109
  113. data/lib/polyphony/http/server.rb +0 -49
  114. data/lib/polyphony/http/server/http1.rb +0 -268
  115. data/lib/polyphony/http/server/http2.rb +0 -78
  116. data/lib/polyphony/http/server/http2_stream.rb +0 -136
  117. data/lib/polyphony/http/server/rack.rb +0 -64
  118. data/lib/polyphony/http/server/request.rb +0 -118
  119. data/lib/polyphony/websocket.rb +0 -59
  120. data/test/test_http_server.rb +0 -313
data/bin/poly DELETED
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative('../lib/polyphony/http')
4
-
5
- app_path = ARGV.first || './config.ru'
6
- app = Polyphony::HTTP::Rack.load(app_path)
7
- opts = { reuse_addr: true, dont_linger: true }
8
-
9
- puts "listening on port 1234"
10
- puts "pid: #{Process.pid}"
11
- Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts, &app)
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- begin
7
- puts 'going to sleep...'
8
- cancel_after(1) do
9
- sleep(60)
10
- end
11
- rescue Polyphony::Cancel
12
- puts 'cancelled'
13
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- enum = [1, 2, 3].each
7
-
8
- spin do
9
- while (e = enum.next rescue nil)
10
- puts e
11
- sleep 0.1
12
- end
13
- end
14
-
15
- suspend
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- ::Exception.__disable_sanitized_backtrace__ = true
7
-
8
- def test
9
- # error is propagated to calling coprocess
10
- raised_error = nil
11
- spin do
12
- spin do
13
- raise 'foo'
14
- end
15
- puts "before snooze"
16
- snooze # allow nested coprocess to run before finishing
17
- puts "after snooze"
18
- end
19
- suspend
20
- rescue Exception => e
21
- raised_error = e
22
- ensure
23
- puts "raised_error: #{raised_error.inspect}"
24
- # puts "msg: #{raised_error.message.inspect}"
25
- end
26
-
27
- test
28
- begin
29
- puts "last suspend"
30
- #suspend
31
- Gyro.run
32
- rescue => e
33
- puts "!" * 60
34
- puts "Error after last suspend: #{e.inspect}"
35
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- f = Fiber.new do
4
- raise 'hi'
5
- end
6
-
7
- f.resume
8
-
9
- puts 'done'
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'fiber'
4
-
5
- # This is an experiment to see if we could provide better backtraces for
6
- # exceptions raised in fibers. Our approach is to monkey-patch Fiber.new so as
7
- # to keep track of the caller stack trace and calling fiber. We also
8
- # monkey-patch Exception#backtrace to calculate the full backtrace based on the
9
- # fiber in which the exception was raised. The benefit of this approach is that
10
- # there's no need to sanitize the backtrace (remove stack frames related to the
11
- # backtrace calculation).
12
- class Fiber
13
- attr_writer :__calling_fiber__, :__caller__
14
-
15
- class << self
16
- alias_method :orig_new, :new
17
- def new(&block)
18
- calling_fiber = Fiber.current
19
- fiber_caller = caller
20
- orig_new(&block).tap do |f|
21
- f.__calling_fiber__ = calling_fiber
22
- f.__caller__ = fiber_caller
23
- end
24
- end
25
- end
26
-
27
- def caller
28
- @__caller__ ||= []
29
- if @__calling_fiber__
30
- @__caller__ + @__calling_fiber__.caller
31
- else
32
- @__caller__
33
- end
34
- end
35
- end
36
-
37
- class Exception
38
- alias_method :orig_initialize, :initialize
39
-
40
- def initialize(*args)
41
- @__raising_fiber__ = Fiber.current
42
- orig_initialize(*args)
43
- end
44
-
45
- alias_method :orig_backtrace, :backtrace
46
- def backtrace
47
- unless @backtrace_called
48
- @backtrace_called = true
49
- return orig_backtrace
50
- end
51
-
52
- if @__raising_fiber__
53
- backtrace = orig_backtrace || []
54
- backtrace + @__raising_fiber__.caller
55
- else
56
- orig_backtrace
57
- end
58
- end
59
- end
60
-
61
- def foo
62
- Fiber.new do
63
- bar
64
- end.resume
65
- end
66
-
67
- def bar
68
- Fiber.new do
69
- raise 'baz'
70
- end.resume
71
- end
72
-
73
- foo
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
- Exception.__disable_sanitized_backtrace__ = true
6
-
7
- puts 'going to sleep...'
8
- move_on_after(1) do
9
- sleep 60
10
- end
11
- puts 'woke up'
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- puts 'going to sleep...'
7
- move_on_after(1) do
8
- sleep 60
9
- end
10
- puts 'woke up'
11
-
12
- puts 'going to sleep...'
13
- move_on_after(1) do
14
- sleep 60
15
- end
16
- puts 'woke up'
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- puts 'going to sleep...'
7
- move_on_after(0.5) do
8
- t0 = Time.now
9
- sleep(60)
10
- ensure
11
- puts "slept for #{Time.now - t0} seconds"
12
- end
13
- puts 'woke up'
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
- Exception.__disable_sanitized_backtrace__ = true
6
-
7
- def blocking_operation
8
- sleep 60
9
- :foo
10
- end
11
-
12
- puts 'going to sleep...'
13
- value = move_on_after(1, with_value: :bar) { blocking_operation }
14
- puts "got value #{value.inspect}"
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- spin do
7
- puts '1 >'
8
- sleep(1)
9
- puts '1 <'
10
- end
11
-
12
- spin do
13
- puts '2 >'
14
- sleep(1)
15
- puts '2 <'
16
- end
17
-
18
- suspend
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- def sleep_and_cancel
7
- puts "#{Time.now} going to sleep with cancel..."
8
- cancel_after(1) do
9
- puts "#{Time.now} outer cancel scope"
10
- cancel_after(10) do
11
- puts "#{Time.now} inner cancel scope"
12
- sleep 60
13
- rescue Exception => e
14
- puts "#{Time.now} inner scope got error: #{e}"
15
- raise e
16
- end
17
- rescue Exception => e
18
- puts "#{Time.now} outer scope got error: #{e}"
19
- end
20
- ensure
21
- puts "#{Time.now} woke up"
22
- end
23
-
24
- def sleep_and_move_on
25
- puts "#{Time.now} going to sleep with move_on..."
26
- move_on_after(1) do
27
- puts "#{Time.now} outer cancel scope"
28
- move_on_after(10) do
29
- puts "#{Time.now} inner cancel scope"
30
- sleep 60
31
- puts "#{Time.now} inner scope done"
32
- end
33
- puts "#{Time.now} outer scope done"
34
- end
35
- puts "#{Time.now} woke up"
36
- end
37
-
38
- sleep_and_cancel
39
- puts
40
- sleep_and_move_on
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- spin do
7
- spin do
8
- puts '1 >'
9
- sleep(1)
10
- puts '1 <'
11
- end
12
-
13
- spin do
14
- puts '2 >'
15
- sleep(1)
16
- puts '2 <'
17
- end
18
- end
19
-
20
- suspend
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- spin do
7
- puts 'going to sleep'
8
- result = spin do
9
- spin do
10
- spin do
11
- puts "Coprocess count: #{Polyphony::Coprocess.list.size}"
12
- sleep(1)
13
- end.await
14
- end.await
15
- end.await
16
- puts "result: #{result}"
17
- end
18
-
19
- suspend
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'fiber'
4
-
5
- ping = Fiber.new do |peer|
6
- loop do
7
- puts 'ping'
8
- sleep 0.3
9
- peer.transfer Fiber.current
10
- end
11
- end
12
-
13
- pong = Fiber.new do |peer|
14
- loop do
15
- puts 'pong'
16
- sleep 0.3
17
- peer.transfer Fiber.current
18
- end
19
- end
20
-
21
- ping.resume(pong)
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- resource_count = 0
7
- Pool = Polyphony::ResourcePool.new(limit: 3) do
8
- :"resource#{resource_count += 1}"
9
- end
10
-
11
- def user(number)
12
- loop do
13
- # puts "user #{number} >"
14
- Pool.acquire do |r|
15
- puts "user #{number} #{r.inspect} >"
16
- sleep(0.05 + rand * 0.2)
17
- puts "user #{number} #{r.inspect} <"
18
- # raise if rand > 0.9
19
- # STDOUT << '.'
20
- # puts "#{number}: #{r.inspect}"
21
- end
22
- end
23
- end
24
-
25
- 3.times do |x|
26
- spin { user(x) }
27
- end
28
-
29
- t0 = Time.now
30
- every(10) { puts "uptime: #{Time.now - t0}" }
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- spin do
7
- 10.times do |i|
8
- sleep 0.05
9
- p i
10
- end
11
- end
12
-
13
- spin do
14
- puts 'going to sleep...'
15
- sleep 0.4
16
- puts 'woke up'
17
- end.await
18
-
19
- puts 'done'
20
-
21
- suspend
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- COUNT = 10_000
7
-
8
- class ::Fiber
9
- attr_accessor :tag
10
- end
11
-
12
- COUNTS = Hash.new { |h, k| h[k] = 0 }
13
-
14
- def t(tag)
15
- Fiber.current.tag = tag.to_s
16
- COUNT.times do
17
- COUNTS[tag] += 1
18
- snooze
19
- end
20
- puts "#{tag} done"
21
- rescue StandardError => e
22
- puts e
23
- end
24
-
25
- GC.disable
26
- cp1 = spin { t(:a) }
27
- cp2 = spin { t(:b) }
28
-
29
- sleep 0.01 while cp1.alive? || cp2.alive?
30
-
31
- puts 'counts:'
32
- p COUNTS
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/setup'
4
- require 'polyphony'
5
-
6
- def error(t)
7
- raise "hello #{t}"
8
- end
9
-
10
- def spin_with_error
11
- spin { error(2) }
12
- end
13
-
14
- spin_with_error
15
-
16
- puts 'done coprocing'
17
- suspend