polyphony 0.19 → 0.20

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.rubocop.yml +87 -1
  4. data/CHANGELOG.md +35 -0
  5. data/Gemfile.lock +17 -6
  6. data/README.md +200 -139
  7. data/Rakefile +4 -4
  8. data/TODO.md +35 -7
  9. data/bin/poly +11 -0
  10. data/docs/getting-started/getting-started.md +1 -1
  11. data/docs/summary.md +3 -0
  12. data/docs/technical-overview/exception-handling.md +94 -0
  13. data/docs/technical-overview/fiber-scheduling.md +99 -0
  14. data/examples/core/cancel.rb +8 -4
  15. data/examples/core/channel_echo.rb +18 -17
  16. data/examples/core/defer.rb +12 -0
  17. data/examples/core/enumerator.rb +4 -4
  18. data/examples/core/fiber_error.rb +9 -0
  19. data/examples/core/fiber_error_with_backtrace.rb +73 -0
  20. data/examples/core/fork.rb +6 -6
  21. data/examples/core/genserver.rb +16 -8
  22. data/examples/core/lock.rb +3 -3
  23. data/examples/core/move_on.rb +4 -3
  24. data/examples/core/move_on_twice.rb +5 -5
  25. data/examples/core/move_on_with_ensure.rb +8 -11
  26. data/examples/core/move_on_with_value.rb +14 -0
  27. data/examples/core/{multiple_spawn.rb → multiple_spin.rb} +5 -5
  28. data/examples/core/nested_cancel.rb +5 -5
  29. data/examples/core/{nested_multiple_spawn.rb → nested_multiple_spin.rb} +6 -6
  30. data/examples/core/nested_spin.rb +17 -0
  31. data/examples/core/pingpong.rb +21 -0
  32. data/examples/core/pulse.rb +4 -5
  33. data/examples/core/resource.rb +6 -4
  34. data/examples/core/resource_cancel.rb +6 -9
  35. data/examples/core/resource_delegate.rb +3 -3
  36. data/examples/core/sleep.rb +3 -3
  37. data/examples/core/sleep_spin.rb +19 -0
  38. data/examples/core/snooze.rb +32 -0
  39. data/examples/core/spin.rb +14 -0
  40. data/examples/core/{spawn_cancel.rb → spin_cancel.rb} +6 -7
  41. data/examples/core/spin_error.rb +17 -0
  42. data/examples/core/spin_error_backtrace.rb +30 -0
  43. data/examples/core/spin_uncaught_error.rb +15 -0
  44. data/examples/core/supervisor.rb +8 -8
  45. data/examples/core/supervisor_with_cancel_scope.rb +7 -7
  46. data/examples/core/supervisor_with_error.rb +8 -8
  47. data/examples/core/supervisor_with_manual_move_on.rb +6 -7
  48. data/examples/core/suspend.rb +13 -0
  49. data/examples/core/thread.rb +1 -1
  50. data/examples/core/thread_cancel.rb +9 -11
  51. data/examples/core/thread_pool.rb +18 -14
  52. data/examples/core/throttle.rb +7 -7
  53. data/examples/core/timeout.rb +3 -3
  54. data/examples/fs/read.rb +7 -9
  55. data/examples/http/config.ru +7 -3
  56. data/examples/http/cuba.ru +22 -0
  57. data/examples/http/happy_eyeballs.rb +6 -4
  58. data/examples/http/http_client.rb +1 -1
  59. data/examples/http/http_get.rb +1 -1
  60. data/examples/http/http_parse_experiment.rb +21 -16
  61. data/examples/http/http_proxy.rb +28 -26
  62. data/examples/http/http_server.rb +10 -10
  63. data/examples/http/http_server_forked.rb +6 -5
  64. data/examples/http/http_server_throttled.rb +3 -3
  65. data/examples/http/http_ws_server.rb +11 -11
  66. data/examples/http/https_raw_client.rb +1 -1
  67. data/examples/http/https_server.rb +8 -8
  68. data/examples/http/https_wss_server.rb +13 -11
  69. data/examples/http/rack_server.rb +2 -2
  70. data/examples/http/rack_server_https.rb +4 -4
  71. data/examples/http/rack_server_https_forked.rb +5 -5
  72. data/examples/http/websocket_secure_server.rb +6 -6
  73. data/examples/http/websocket_server.rb +5 -5
  74. data/examples/interfaces/pg_client.rb +4 -4
  75. data/examples/interfaces/pg_pool.rb +13 -6
  76. data/examples/interfaces/pg_transaction.rb +5 -4
  77. data/examples/interfaces/redis_channels.rb +15 -11
  78. data/examples/interfaces/redis_client.rb +2 -2
  79. data/examples/interfaces/redis_pubsub.rb +2 -1
  80. data/examples/interfaces/redis_pubsub_perf.rb +13 -9
  81. data/examples/io/backticks.rb +11 -0
  82. data/examples/io/cat.rb +4 -5
  83. data/examples/io/echo_client.rb +9 -4
  84. data/examples/io/echo_client_from_stdin.rb +20 -0
  85. data/examples/io/echo_pipe.rb +7 -8
  86. data/examples/io/echo_server.rb +8 -6
  87. data/examples/io/echo_server_with_timeout.rb +13 -10
  88. data/examples/io/echo_stdin.rb +3 -3
  89. data/examples/io/httparty.rb +2 -2
  90. data/examples/io/httparty_multi.rb +8 -4
  91. data/examples/io/httparty_threaded.rb +6 -2
  92. data/examples/io/io_read.rb +2 -2
  93. data/examples/io/irb.rb +16 -4
  94. data/examples/io/net-http.rb +3 -3
  95. data/examples/io/open.rb +17 -0
  96. data/examples/io/system.rb +3 -3
  97. data/examples/io/tcpserver.rb +15 -0
  98. data/examples/io/tcpsocket.rb +6 -5
  99. data/examples/performance/multi_snooze.rb +29 -0
  100. data/examples/performance/{perf_snooze.rb → snooze.rb} +7 -5
  101. data/examples/performance/snooze_raw.rb +39 -0
  102. data/ext/gyro/async.c +165 -0
  103. data/ext/gyro/child.c +167 -0
  104. data/ext/{ev → gyro}/extconf.rb +4 -3
  105. data/ext/gyro/gyro.c +316 -0
  106. data/ext/{ev/ev.h → gyro/gyro.h} +12 -7
  107. data/ext/gyro/gyro_ext.c +23 -0
  108. data/ext/{ev → gyro}/io.c +65 -57
  109. data/ext/{ev → gyro}/libev.h +0 -0
  110. data/ext/gyro/signal.c +117 -0
  111. data/ext/{ev → gyro}/socket.c +61 -6
  112. data/ext/gyro/timer.c +199 -0
  113. data/ext/libev/Changes +35 -0
  114. data/ext/libev/README +2 -1
  115. data/ext/libev/ev.c +213 -151
  116. data/ext/libev/ev.h +95 -88
  117. data/ext/libev/ev_epoll.c +26 -15
  118. data/ext/libev/ev_kqueue.c +11 -5
  119. data/ext/libev/ev_linuxaio.c +642 -0
  120. data/ext/libev/ev_poll.c +13 -8
  121. data/ext/libev/ev_port.c +5 -2
  122. data/ext/libev/ev_vars.h +14 -3
  123. data/ext/libev/ev_wrap.h +16 -0
  124. data/lib/ev_ext.bundle +0 -0
  125. data/lib/polyphony.rb +46 -50
  126. data/lib/polyphony/auto_run.rb +12 -0
  127. data/lib/polyphony/core/cancel_scope.rb +11 -7
  128. data/lib/polyphony/core/channel.rb +16 -9
  129. data/lib/polyphony/core/coprocess.rb +101 -51
  130. data/lib/polyphony/core/exceptions.rb +14 -12
  131. data/lib/polyphony/core/resource_pool.rb +21 -8
  132. data/lib/polyphony/core/supervisor.rb +10 -5
  133. data/lib/polyphony/core/sync.rb +7 -6
  134. data/lib/polyphony/core/thread.rb +4 -4
  135. data/lib/polyphony/core/thread_pool.rb +4 -4
  136. data/lib/polyphony/core/throttler.rb +6 -4
  137. data/lib/polyphony/extensions/core.rb +253 -0
  138. data/lib/polyphony/extensions/io.rb +28 -16
  139. data/lib/polyphony/extensions/openssl.rb +2 -1
  140. data/lib/polyphony/extensions/socket.rb +47 -52
  141. data/lib/polyphony/http.rb +4 -3
  142. data/lib/polyphony/http/agent.rb +68 -57
  143. data/lib/polyphony/http/server.rb +5 -5
  144. data/lib/polyphony/http/server/http1.rb +268 -0
  145. data/lib/polyphony/http/server/http2.rb +62 -0
  146. data/lib/polyphony/http/server/http2_stream.rb +104 -0
  147. data/lib/polyphony/http/server/rack.rb +64 -0
  148. data/lib/polyphony/http/server/request.rb +119 -0
  149. data/lib/polyphony/net.rb +26 -15
  150. data/lib/polyphony/postgres.rb +17 -13
  151. data/lib/polyphony/redis.rb +16 -15
  152. data/lib/polyphony/version.rb +1 -1
  153. data/lib/polyphony/websocket.rb +11 -4
  154. data/polyphony.gemspec +13 -9
  155. data/test/eg.rb +27 -0
  156. data/test/helper.rb +25 -0
  157. data/test/run.rb +5 -0
  158. data/test/test_async.rb +33 -0
  159. data/test/test_coprocess.rb +239 -77
  160. data/test/test_core.rb +95 -61
  161. data/test/test_gyro.rb +148 -0
  162. data/test/test_http_server.rb +313 -0
  163. data/test/test_io.rb +79 -27
  164. data/test/test_kernel.rb +22 -12
  165. data/test/test_signal.rb +36 -0
  166. data/test/test_timer.rb +24 -0
  167. metadata +89 -33
  168. data/examples/core/nested_async.rb +0 -17
  169. data/examples/core/next_tick.rb +0 -12
  170. data/examples/core/sleep_spawn.rb +0 -19
  171. data/examples/core/spawn.rb +0 -14
  172. data/examples/core/spawn_error.rb +0 -28
  173. data/examples/performance/perf_multi_snooze.rb +0 -21
  174. data/ext/ev/async.c +0 -168
  175. data/ext/ev/child.c +0 -169
  176. data/ext/ev/ev_ext.c +0 -23
  177. data/ext/ev/ev_module.c +0 -242
  178. data/ext/ev/signal.c +0 -119
  179. data/ext/ev/timer.c +0 -197
  180. data/lib/polyphony/core/fiber_pool.rb +0 -98
  181. data/lib/polyphony/extensions/kernel.rb +0 -169
  182. data/lib/polyphony/http/http1_adapter.rb +0 -254
  183. data/lib/polyphony/http/http2_adapter.rb +0 -157
  184. data/lib/polyphony/http/rack.rb +0 -25
  185. data/lib/polyphony/http/request.rb +0 -66
  186. data/test/test_ev.rb +0 -110
@@ -1,3 +1,7 @@
1
- run Proc.new { |env|
2
- ['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
3
- }
1
+ # frozen_string_literal: true
2
+
3
+ run(proc do |env|
4
+ ['200', { 'Content-Type' => 'text/html' }, [
5
+ env.select { |k, _v| k =~ /^[A-Z]/ }.inspect
6
+ ]]
7
+ end)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cuba'
4
+ require 'cuba/safe'
5
+
6
+ Cuba.use Rack::Session::Cookie, secret: '__a_very_long_string__'
7
+
8
+ Cuba.plugin Cuba::Safe
9
+
10
+ Cuba.define do
11
+ on get do
12
+ on 'hello' do
13
+ res.write 'Hello world!'
14
+ end
15
+
16
+ on root do
17
+ res.redirect '/hello'
18
+ end
19
+ end
20
+ end
21
+
22
+ run Cuba
@@ -6,11 +6,13 @@
6
6
  require 'bundler/setup'
7
7
  require 'polyphony/http'
8
8
 
9
- async def try_connect(target, supervisor)
9
+ def try_connect(target, supervisor)
10
10
  puts "trying #{target[2]}"
11
11
  socket = Polyphony::Net.tcp_connect(target[2], 80)
12
+ # connection successful
12
13
  supervisor.stop!([target[2], socket])
13
14
  rescue IOError, SystemCallError
15
+ # ignore error
14
16
  end
15
17
 
16
18
  def happy_eyeballs(hostname, port, max_wait_time: 0.025)
@@ -20,11 +22,11 @@ def happy_eyeballs(hostname, port, max_wait_time: 0.025)
20
22
  success = supervise do |supervisor|
21
23
  targets.each_with_index do |t, idx|
22
24
  sleep(max_wait_time) if idx > 0
23
- supervisor.coproc try_connect(t, supervisor)
25
+ supervisor.spin { try_connect(t, supervisor) }
24
26
  end
25
27
  end
26
28
  if success
27
- puts "success: %s (%.3fs)" % [success[0], Time.now - t0]
29
+ puts format('success: %s (%.3fs)', success[0], Time.now - t0)
28
30
  else
29
31
  puts "timed out (#{Time.now - t0}s)"
30
32
  end
@@ -32,4 +34,4 @@ def happy_eyeballs(hostname, port, max_wait_time: 0.025)
32
34
  end
33
35
 
34
36
  # Let's try it out:
35
- happy_eyeballs("debian.org", "https")
37
+ happy_eyeballs('debian.org', 'https')
@@ -14,4 +14,4 @@ supervise do |s|
14
14
  X.times { s.spin { get_server_time } }
15
15
  end
16
16
  elapsed = Time.now - t0
17
- puts "count: #{X} elapsed: #{elapsed} rate: #{X / elapsed} reqs/s"
17
+ puts "count: #{X} elapsed: #{elapsed} rate: #{X / elapsed} reqs/s"
@@ -4,4 +4,4 @@ require 'bundler/setup'
4
4
  require 'polyphony/http'
5
5
 
6
6
  resp = Polyphony::HTTP::Agent.get('https://google.com/')
7
- p resp.body
7
+ p resp.body
@@ -17,7 +17,7 @@ class ParseLoop
17
17
  @parser << data
18
18
  snooze
19
19
  end
20
- rescue => e
20
+ rescue StandardError => e
21
21
  conn.close
22
22
  e
23
23
  ensure
@@ -56,7 +56,7 @@ class ParseLoop
56
56
 
57
57
  def consume_request
58
58
  return unless @message_in_train
59
-
59
+
60
60
  @calling_fiber = Fiber.current
61
61
  @read_body = false
62
62
  @parse_fiber.safe_transfer while @message_in_train
@@ -74,24 +74,25 @@ end
74
74
  def handle(parser)
75
75
  headers = parser.parse_headers
76
76
  return unless headers
77
+
77
78
  puts "headers: #{headers.inspect}"
78
- content_length = headers['Content-Length']
79
+ headers['Content-Length']
79
80
  # if content_length && (content_length.to_i < 1000)
80
- while chunk = parser.parse_body_chunk
81
- puts "chunk: #{chunk.inspect}"
82
- end
81
+ while (chunk = parser.parse_body_chunk)
82
+ puts "chunk: #{chunk.inspect}"
83
+ end
83
84
  # else
84
85
  # parser.consume_request
85
86
  # end
86
- puts "end of request"
87
- rescue => e
87
+ puts 'end of request'
88
+ rescue StandardError => e
88
89
  puts "error: #{e.inspect}"
89
90
  raise e
90
91
  end
91
92
 
92
- writer = spin {
93
+ writer = spin do
93
94
  o << "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 6\r\n\r\n"
94
- o << "Hello!"
95
+ o << 'Hello!'
95
96
 
96
97
  o << "POST / HTTP/1.1\r\nHost: example.com\r\n\r\n"
97
98
 
@@ -99,20 +100,24 @@ writer = spin {
99
100
  # o << ("Bye!" * 2048)
100
101
 
101
102
  o << "POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 4\r\n\r\n"
102
- o << "Bye!"
103
+ o << 'Bye!'
103
104
 
104
- (o << ("BLAH" * 100000)) rescue nil
105
+ begin
106
+ (o << ('BLAH' * 100_000))
107
+ rescue StandardError
108
+ nil
109
+ end
105
110
  o.close
106
- }
111
+ end
107
112
 
108
113
  begin
109
114
  parse_loop = ParseLoop.new(i)
110
115
  while parse_loop.alive?
111
- puts "*" * 40
116
+ puts '*' * 40
112
117
  handle(parse_loop)
113
118
  end
114
- rescue => e
119
+ rescue StandardError => e
115
120
  writer.stop
116
121
  puts "#{e.class}: #{e.message}"
117
122
  puts e.backtrace
118
- end
123
+ end
@@ -8,32 +8,30 @@ require 'localhost/authority'
8
8
 
9
9
  BASE_URL = 'http://realiteq.net'
10
10
 
11
- CACHE = {}
11
+ CACHE = {}.freeze
12
12
 
13
13
  def proxy(uri, opts)
14
14
  now = Time.now
15
15
  uri = BASE_URL + uri
16
16
  entry = CACHE[uri]
17
- if entry && entry[:expires] >= now
18
- return entry[:response]
19
- else
20
- puts "proxy => #{uri} (#{opts.inspect})"
21
- response = Polyphony::HTTP::Agent.get(uri, opts)
22
- # CACHE[uri] = {
23
- # expires: now + 60,
24
- # response: response
25
- # }
26
- response
27
- end
17
+ return entry[:response] if entry && entry[:expires] >= now
18
+
19
+ puts "proxy => #{uri} (#{opts.inspect})"
20
+ response = Polyphony::HTTP::Agent.get(uri, opts)
21
+ # CACHE[uri] = {
22
+ # expires: now + 60,
23
+ # response: response
24
+ # }
25
+ response
28
26
  end
29
27
 
30
- HEADERS_BLACK_LIST = %w{
28
+ HEADERS_BLACK_LIST = %w[
31
29
  Transfer-Encoding Date Server Connection Content-Length Cache-Control
32
30
  :method :authority :scheme :path
33
- }
31
+ ].freeze
34
32
 
35
33
  def sanitize_headers(headers)
36
- headers.reject { |k, v| HEADERS_BLACK_LIST.include?(k) }
34
+ headers.reject { |k, _v| HEADERS_BLACK_LIST.include?(k) }
37
35
  end
38
36
 
39
37
  def sanitize_html(html)
@@ -42,20 +40,24 @@ end
42
40
 
43
41
  # authority = Localhost::Authority.fetch
44
42
 
45
- rsa_cert = OpenSSL::X509::Certificate.new(IO.read('../reality/aws/config/ssl/full_chain.pem'))
46
- rsa_pkey = OpenSSL::PKey.read(IO.read('../reality/aws/config/ssl/private_key.pem'))
43
+ rsa_cert = OpenSSL::X509::Certificate.new(
44
+ IO.read('../reality/aws/config/ssl/full_chain.pem')
45
+ )
46
+ rsa_pkey = OpenSSL::PKey.read(
47
+ IO.read('../reality/aws/config/ssl/private_key.pem')
48
+ )
47
49
  ctx = OpenSSL::SSL::SSLContext.new
48
50
  ctx.add_certificate(rsa_cert, rsa_pkey)
49
51
 
50
52
  opts = {
51
- reuse_addr: true,
52
- dont_linger: true,
53
- secure_context: ctx#authority.server_context
53
+ reuse_addr: true,
54
+ dont_linger: true,
55
+ secure_context: ctx # authority.server_context
54
56
  }
55
57
 
56
- spin {
58
+ spin do
57
59
  Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
58
- puts "#{req.method} #{req.uri.to_s}"
60
+ puts "#{req.method} #{req.uri}"
59
61
  puts "headers <: #{req.headers.inspect}"
60
62
  # h = {
61
63
  # uri: req.uri.to_s,
@@ -70,12 +72,12 @@ spin {
70
72
  puts "headers >: #{response[:headers].inspect}"
71
73
  # body = sanitize_html(body) if headers['Content-Type'] =~ /text\/html/
72
74
  req.respond(body, headers)
73
- rescue => e
74
- puts "error"
75
+ rescue StandardError => e
76
+ puts 'error'
75
77
  p e
76
78
  puts e.backtrace.join("\n")
77
79
  end
78
- }
80
+ end
79
81
 
80
82
  puts "pid: #{Process.pid}"
81
- puts "Listening on port 1234..."
83
+ puts 'Listening on port 1234...'
@@ -1,25 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/setup'
4
+ require 'polyphony/auto_run'
4
5
  require 'polyphony/http'
5
6
 
6
7
  opts = {
7
- reuse_addr: true,
8
+ reuse_addr: true,
8
9
  dont_linger: true
9
10
  }
10
11
 
11
12
  spin do
12
13
  Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
13
- req.respond('Hello world!')
14
- # req.send_headers
15
- # req.send_body_chunk("Method: #{req.method}\n")
16
- # req.send_body_chunk("Path: #{req.path}\n")
17
- # req.send_body_chunk("Query: #{req.query.inspect}\n", done: true)
14
+ req.respond("Hello world!\n")
15
+ end
16
+ end
17
+
18
+ spin do
19
+ throttled_loop(1) do
20
+ puts "Coprocess count: #{Polyphony::Coprocess.list.size}"
18
21
  end
19
- rescue Exception => e
20
- puts "*" * 40
21
- p e
22
22
  end
23
23
 
24
24
  puts "pid: #{Process.pid}"
25
- puts "Listening on port 1234..."
25
+ puts 'Listening on port 1234...'
@@ -4,23 +4,24 @@ require 'bundler/setup'
4
4
  require 'polyphony/http'
5
5
 
6
6
  opts = {
7
- reuse_addr: true,
8
- dont_linger: true,
7
+ reuse_addr: true,
8
+ dont_linger: true
9
9
  }
10
10
 
11
11
  server = Polyphony::HTTP::Server.listen('0.0.0.0', 1234, opts)
12
12
 
13
- puts "Listening on port 1234"
13
+ puts 'Listening on port 1234'
14
14
 
15
15
  child_pids = []
16
16
  4.times do
17
- child_pids << Polyphony.fork do
17
+ pid = Polyphony.fork do
18
18
  puts "forked pid: #{Process.pid}"
19
19
  server.each do |req|
20
20
  req.respond("Hello world! from pid: #{Process.pid}\n")
21
21
  end
22
22
  rescue Interrupt
23
23
  end
24
+ child_pids << pid
24
25
  end
25
26
 
26
- child_pids.each { |pid| EV::Child.new(pid).await }
27
+ child_pids.each { |pid| EV::Child.new(pid).await }
@@ -5,11 +5,11 @@ require 'polyphony/http'
5
5
 
6
6
  $throttler = throttle(1000)
7
7
  opts = { reuse_addr: true, dont_linger: true }
8
- spin {
8
+ spin do
9
9
  Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
10
10
  $throttler.call { req.respond("Hello world!\n") }
11
11
  end
12
- }
12
+ end
13
13
 
14
14
  puts "pid: #{Process.pid}"
15
- puts "Listening on port 1234..."
15
+ puts 'Listening on port 1234...'
@@ -7,12 +7,12 @@ require 'polyphony/http'
7
7
  require 'polyphony/websocket'
8
8
 
9
9
  def ws_handler(conn)
10
- timer = spin {
11
- throttled_loop(1) {
10
+ timer = spin do
11
+ throttled_loop(1) do
12
12
  conn << Time.now.to_s
13
- }
14
- }
15
- while msg = conn.recv
13
+ end
14
+ end
15
+ while (msg = conn.recv)
16
16
  conn << "you said: #{msg}"
17
17
  end
18
18
  ensure
@@ -20,20 +20,20 @@ ensure
20
20
  end
21
21
 
22
22
  opts = {
23
- reuse_addr: true,
23
+ reuse_addr: true,
24
24
  dont_linger: true,
25
- upgrade: {
25
+ upgrade: {
26
26
  websocket: Polyphony::Websocket.handler(&method(:ws_handler))
27
27
  }
28
28
  }
29
29
 
30
30
  HTML = IO.read(File.join(__dir__, 'ws_page.html'))
31
31
 
32
- spin {
33
- server = Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
32
+ spin do
33
+ Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
34
34
  req.respond(HTML, 'Content-Type' => 'text/html')
35
35
  end
36
- }
36
+ end
37
37
 
38
38
  puts "pid: #{Process.pid}"
39
- puts "Listening on port 1234..."
39
+ puts 'Listening on port 1234...'
@@ -9,4 +9,4 @@ io.write("GET / HTTP/1.0\r\nHost: realiteq.net\r\n\r\n")
9
9
  reply = io.read
10
10
  puts "time: #{Time.now - t0}"
11
11
  puts
12
- puts reply
12
+ puts reply
@@ -6,17 +6,17 @@ require 'localhost/authority'
6
6
 
7
7
  authority = Localhost::Authority.fetch
8
8
  opts = {
9
- reuse_addr: true,
10
- dont_linger: true,
9
+ reuse_addr: true,
10
+ dont_linger: true,
11
11
  secure_context: authority.server_context
12
12
  }
13
13
 
14
14
  puts "pid: #{Process.pid}"
15
- puts "Listening on port 1234..."
15
+ puts 'Listening on port 1234...'
16
16
  Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
17
- req.respond('Hello world!')
17
+ req.respond("Hello world!\n")
18
18
  # req.send_headers
19
- # req.send_body_chunk("Method: #{req.method}\n")
20
- # req.send_body_chunk("Path: #{req.path}\n")
21
- # req.send_body_chunk("Query: #{req.query.inspect}\n", done: true)
22
- end
19
+ # req.send_chunk("Method: #{req.method}\n")
20
+ # req.send_chunk("Path: #{req.path}\n")
21
+ # req.send_chunk("Query: #{req.query.inspect}\n", done: true)
22
+ end
@@ -7,12 +7,14 @@ require 'localhost/authority'
7
7
  STDOUT.sync = true
8
8
 
9
9
  def ws_handler(conn)
10
- timer = spin {
11
- throttled_loop(1) {
12
- conn << Time.now.to_s rescue nil
13
- }
14
- }
15
- while msg = conn.recv
10
+ timer = spin do
11
+ throttled_loop(1) do
12
+ conn << Time.now.to_s
13
+ rescue StandardError
14
+ nil
15
+ end
16
+ end
17
+ while (msg = conn.recv)
16
18
  puts "msg: #{msg}"
17
19
  # conn << "you said: #{msg}"
18
20
  end
@@ -22,10 +24,10 @@ end
22
24
 
23
25
  authority = Localhost::Authority.fetch
24
26
  opts = {
25
- reuse_addr: true,
26
- dont_linger: true,
27
+ reuse_addr: true,
28
+ dont_linger: true,
27
29
  secure_context: authority.server_context,
28
- upgrade: {
30
+ upgrade: {
29
31
  websocket: Polyphony::Websocket.handler(&method(:ws_handler))
30
32
  }
31
33
  }
@@ -33,7 +35,7 @@ opts = {
33
35
  HTML = IO.read(File.join(__dir__, 'wss_page.html'))
34
36
 
35
37
  puts "pid: #{Process.pid}"
36
- puts "Listening on port 1234..."
38
+ puts 'Listening on port 1234...'
37
39
  Polyphony::HTTP::Server.serve('0.0.0.0', 1234, opts) do |req|
38
40
  req.respond(HTML, 'Content-Type' => 'text/html')
39
- end
41
+ end