polyphony 0.15 → 0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +11 -3
- data/TODO.md +25 -14
- data/docs/getting-started/getting-started.md +1 -1
- data/docs/getting-started/tutorial.md +2 -0
- data/examples/core/cancel.rb +2 -3
- data/examples/core/channel_echo.rb +2 -3
- data/examples/core/enumerator.rb +2 -3
- data/examples/core/fork.rb +2 -2
- data/examples/core/genserver.rb +2 -3
- data/examples/core/lock.rb +2 -3
- data/examples/core/move_on.rb +2 -3
- data/examples/core/move_on_twice.rb +2 -3
- data/examples/core/move_on_with_ensure.rb +2 -3
- data/examples/core/{multiple_async.rb → multiple_spawn.rb} +2 -3
- data/examples/core/nested_async.rb +2 -3
- data/examples/core/nested_cancel.rb +2 -3
- data/examples/core/{nested_multiple_async.rb → nested_multiple_spawn.rb} +2 -3
- data/examples/core/next_tick.rb +4 -5
- data/examples/core/pulse.rb +2 -3
- data/examples/core/resource.rb +2 -3
- data/examples/core/resource_cancel.rb +2 -3
- data/examples/core/resource_delegate.rb +2 -3
- data/examples/core/sleep.rb +2 -3
- data/examples/core/sleep_spawn.rb +19 -0
- data/examples/core/spawn.rb +2 -3
- data/examples/core/spawn_cancel.rb +2 -3
- data/examples/core/spawn_error.rb +2 -2
- data/examples/core/supervisor.rb +2 -3
- data/examples/core/supervisor_with_cancel_scope.rb +2 -3
- data/examples/core/supervisor_with_error.rb +2 -3
- data/examples/core/supervisor_with_manual_move_on.rb +2 -3
- data/examples/core/thread.rb +3 -6
- data/examples/core/thread_cancel.rb +2 -5
- data/examples/core/thread_pool.rb +3 -6
- data/examples/core/throttle.rb +2 -3
- data/examples/fs/read.rb +22 -19
- data/examples/http/happy_eyeballs.rb +2 -2
- data/examples/http/http_client.rb +5 -7
- data/examples/http/http_server.rb +2 -3
- data/examples/http/http_server_forked.rb +2 -3
- data/examples/http/http_server_throttled.rb +2 -3
- data/examples/http/http_ws_server.rb +4 -4
- data/examples/http/https_raw_client.rb +4 -5
- data/examples/http/https_server.rb +2 -3
- data/examples/http/https_wss_server.rb +2 -3
- data/examples/http/rack_server.rb +2 -4
- data/examples/http/rack_server_https.rb +2 -3
- data/examples/http/rack_server_https_forked.rb +2 -3
- data/examples/http/websocket_secure_server.rb +2 -3
- data/examples/http/websocket_server.rb +2 -3
- data/examples/interfaces/pg_client.rb +2 -4
- data/examples/interfaces/pg_pool.rb +4 -6
- data/examples/interfaces/{pg_query.rb → pg_transaction.rb} +2 -4
- data/examples/interfaces/redis_channels.rb +2 -4
- data/examples/interfaces/redis_client.rb +2 -4
- data/examples/interfaces/redis_pubsub.rb +2 -4
- data/examples/interfaces/redis_pubsub_perf.rb +2 -4
- data/examples/io/echo_client.rb +4 -5
- data/examples/io/echo_server.rb +2 -2
- data/examples/io/echo_server_with_timeout.rb +3 -5
- data/examples/io/echo_stdin.rb +3 -4
- data/examples/performance/perf_multi_snooze.rb +2 -2
- data/examples/performance/perf_snooze.rb +2 -2
- data/examples/performance/thread-vs-fiber/polyphony_server.rb +4 -5
- data/ext/ev/ev_module.c +3 -2
- data/lib/polyphony/{resource_pool.rb → core/resource_pool.rb} +0 -0
- data/lib/polyphony/core/supervisor.rb +8 -8
- data/lib/polyphony/extensions/{ssl.rb → openssl.rb} +0 -0
- data/lib/polyphony/http/agent.rb +1 -1
- data/lib/polyphony/http.rb +12 -4
- data/lib/polyphony/net.rb +8 -11
- data/lib/polyphony/{extensions/postgres.rb → postgres.rb} +1 -4
- data/lib/polyphony/{extensions/redis.rb → redis.rb} +3 -6
- data/lib/polyphony/version.rb +1 -1
- data/lib/polyphony/websocket.rb +1 -1
- data/lib/polyphony.rb +57 -21
- data/polyphony.gemspec +5 -4
- data/test/test_coprocess.rb +88 -15
- data/test/test_core.rb +142 -232
- data/test/test_ev.rb +88 -95
- data/test/test_io.rb +35 -41
- metadata +68 -16
- data/examples/core/sleep2.rb +0 -13
- data/examples/streams/lines.rb +0 -27
- data/examples/streams/stdio.rb +0 -18
- data/lib/polyphony/core.rb +0 -45
- data/lib/polyphony/server_task.rb +0 -18
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'modulation'
|
4
|
-
|
5
3
|
STDOUT.sync = true
|
6
4
|
|
7
|
-
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'polyphony/http'
|
7
|
+
require 'polyphony/websocket'
|
8
8
|
|
9
9
|
def ws_handler(conn)
|
10
10
|
timer = spawn {
|
@@ -13,7 +13,7 @@ def ws_handler(conn)
|
|
13
13
|
}
|
14
14
|
}
|
15
15
|
while msg = conn.recv
|
16
|
-
|
16
|
+
conn << "you said: #{msg}"
|
17
17
|
end
|
18
18
|
ensure
|
19
19
|
timer.stop
|
@@ -1,12 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
6
5
|
|
7
6
|
t0 = Time.now
|
8
|
-
io = Polyphony::Net.tcp_connect('
|
9
|
-
io.write("GET
|
7
|
+
io = Polyphony::Net.tcp_connect('google.com', 443, secure: true)
|
8
|
+
io.write("GET / HTTP/1.0\r\nHost: realiteq.net\r\n\r\n")
|
10
9
|
reply = io.read
|
11
10
|
puts "time: #{Time.now - t0}"
|
12
11
|
puts
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
4
5
|
require 'localhost/authority'
|
5
6
|
|
6
|
-
Polyphony = import('../../lib/polyphony')
|
7
|
-
|
8
7
|
authority = Localhost::Authority.fetch
|
9
8
|
opts = {
|
10
9
|
reuse_addr: true,
|
@@ -1,12 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
4
5
|
require 'localhost/authority'
|
5
6
|
|
6
7
|
STDOUT.sync = true
|
7
8
|
|
8
|
-
Polyphony = import('../../lib/polyphony')
|
9
|
-
|
10
9
|
def ws_handler(conn)
|
11
10
|
timer = spawn {
|
12
11
|
throttled_loop(1) {
|
@@ -1,9 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
|
6
|
-
Polyphony = import('../../lib/polyphony')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
7
5
|
|
8
6
|
app_path = ARGV.first || File.expand_path('./config.ru', __dir__)
|
9
7
|
app = Polyphony::HTTP::Rack.load(app_path)
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
4
5
|
require 'localhost/authority'
|
5
6
|
|
6
|
-
Polyphony = import('../../lib/polyphony')
|
7
|
-
|
8
7
|
app_path = ARGV.first || File.expand_path('./config.ru', __dir__)
|
9
8
|
app = Polyphony::HTTP::Rack.load(app_path)
|
10
9
|
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
4
5
|
require 'localhost/authority'
|
5
6
|
|
6
|
-
Polyphony = import('../../lib/polyphony')
|
7
|
-
|
8
7
|
app_path = ARGV.first || File.expand_path('./config.ru', __dir__)
|
9
8
|
app = Polyphony::HTTP::Rack.load(app_path)
|
10
9
|
|
@@ -1,12 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
4
5
|
require 'localhost/authority'
|
5
6
|
|
6
7
|
STDOUT.sync = true
|
7
8
|
|
8
|
-
Polyphony = import('../../lib/polyphony')
|
9
|
-
|
10
9
|
def ws_handler(conn)
|
11
10
|
while msg = conn.recv
|
12
11
|
conn << "you said: #{msg}"
|
@@ -1,11 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/http'
|
4
5
|
|
5
6
|
STDOUT.sync = true
|
6
7
|
|
7
|
-
Polyphony = import('../../lib/polyphony')
|
8
|
-
|
9
8
|
def ws_handler(conn)
|
10
9
|
while msg = conn.recv
|
11
10
|
conn << "you said: #{msg}"
|
@@ -1,9 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
6
|
-
Postgres = import('../../lib/polyphony/extensions/postgres')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/postgres'
|
7
5
|
|
8
6
|
def get_records
|
9
7
|
res = $db.query("select 1 as test")
|
@@ -1,9 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
6
|
-
Postgres = import('../../lib/polyphony/extensions/postgres')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/postgres'
|
7
5
|
|
8
6
|
PGOPTS = {
|
9
7
|
host: '/tmp',
|
@@ -16,7 +14,7 @@ PGOPTS = {
|
|
16
14
|
DBPOOL = Polyphony::ResourcePool.new(limit: 8) { PG.connect(PGOPTS) }
|
17
15
|
|
18
16
|
def get_records(db)
|
19
|
-
res = db.query("select pg_sleep(0.
|
17
|
+
res = db.query("select pg_sleep(0.001) as test")
|
20
18
|
# puts "got #{res.ntuples} records: #{res.to_a}"
|
21
19
|
rescue => e
|
22
20
|
puts "got error: #{e.inspect}"
|
@@ -32,6 +30,6 @@ count = 0
|
|
32
30
|
coprocs = CONCURRENCY.times.map {
|
33
31
|
spawn { loop { DBPOOL.acquire { |db| get_records(db); count += 1 } } }
|
34
32
|
}
|
35
|
-
sleep
|
33
|
+
sleep 5
|
36
34
|
puts "count: #{count} query rate: #{count / (Time.now - t0)} queries/s"
|
37
35
|
coprocs.each(&:interrupt)
|
@@ -1,9 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
6
|
-
import('../../lib/polyphony/extensions/redis')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/redis'
|
7
5
|
|
8
6
|
class RedisChannel < Polyphony::Channel
|
9
7
|
def self.publish_connection
|
@@ -1,11 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony/redis'
|
4
5
|
require 'json'
|
5
6
|
|
6
|
-
Polyphony = import('../../lib/polyphony')
|
7
|
-
import('../../lib/polyphony/extensions/redis')
|
8
|
-
|
9
7
|
X_SESSIONS = 1000
|
10
8
|
X_NODES = 10000
|
11
9
|
X_SUBSCRIPTIONS_PER_SESSION = 100
|
data/examples/io/echo_client.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony'
|
6
5
|
|
7
6
|
socket = Polyphony::Net.tcp_connect('127.0.0.1', 1234)
|
8
7
|
|
9
8
|
writer = spawn do
|
10
|
-
throttled_loop(1) { socket << "#{Time.now}\n" }
|
9
|
+
throttled_loop(1) { socket << "#{Time.now}\n" rescue nil }
|
11
10
|
end
|
12
11
|
|
13
12
|
reader = spawn do
|
14
13
|
puts "received from echo server:"
|
15
|
-
while data = socket.
|
14
|
+
while data = socket.readpartial(8192)
|
16
15
|
STDOUT << data
|
17
16
|
end
|
18
17
|
end
|
data/examples/io/echo_server.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony'
|
6
5
|
|
7
6
|
begin
|
8
7
|
server = Polyphony::Net.tcp_listen(nil, 1234, reuse_addr: true, dont_linger: true)
|
@@ -14,8 +13,7 @@ begin
|
|
14
13
|
cancel_scope = nil
|
15
14
|
move_on_after(5) do |s|
|
16
15
|
cancel_scope = s
|
17
|
-
|
18
|
-
data = client.read
|
16
|
+
while (data = client.readpartial(8192))
|
19
17
|
s.reset_timeout
|
20
18
|
client.write(data)
|
21
19
|
end
|
data/examples/io/echo_stdin.rb
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
Polyphony = import('../../lib/polyphony')
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony'
|
6
5
|
|
7
6
|
puts "Write something..."
|
8
7
|
move_on_after(5) do |scope|
|
9
8
|
loop do
|
10
|
-
data = STDIN.
|
9
|
+
data = STDIN.readpartial(8192)
|
11
10
|
scope.reset_timeout
|
12
11
|
puts "you wrote: #{data}"
|
13
12
|
end
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'polyphony'
|
4
5
|
require 'http/parser'
|
5
6
|
|
6
|
-
Polyphony = import('../../../lib/polyphony')
|
7
|
-
|
8
7
|
class Http::Parser
|
9
8
|
def setup_async
|
10
9
|
self.on_message_complete = proc { @request_complete = true }
|
@@ -25,8 +24,8 @@ async def handle_client(socket)
|
|
25
24
|
parser.on_message_complete = proc do |env|
|
26
25
|
req = parser
|
27
26
|
end
|
28
|
-
|
29
|
-
parser <<
|
27
|
+
while (data = socket.readpartial(8192)) do
|
28
|
+
parser << data
|
30
29
|
if req
|
31
30
|
handle_request(socket, req)
|
32
31
|
req = nil
|
data/ext/ev/ev_module.c
CHANGED
@@ -54,8 +54,9 @@ void Init_EV() {
|
|
54
54
|
rb_define_singleton_method(mEV, "post_fork", EV_post_fork, 0);
|
55
55
|
rb_define_singleton_method(mEV, "schedule_fiber", EV_schedule_fiber, 2);
|
56
56
|
|
57
|
-
|
58
|
-
|
57
|
+
rb_define_global_function("suspend", EV_suspend, 0);
|
58
|
+
rb_define_global_function("snooze", EV_snooze, 0);
|
59
|
+
rb_define_global_function("next_tick", EV_next_tick, 0);
|
59
60
|
|
60
61
|
ID_call = rb_intern("call");
|
61
62
|
ID_caller = rb_intern("caller");
|
File without changes
|
@@ -7,7 +7,7 @@ Exceptions = import('./exceptions')
|
|
7
7
|
|
8
8
|
class Supervisor
|
9
9
|
def initialize
|
10
|
-
@
|
10
|
+
@coprocesses = []
|
11
11
|
end
|
12
12
|
|
13
13
|
def await(&block)
|
@@ -34,14 +34,14 @@ class Supervisor
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def spawn_coprocess(proc)
|
37
|
-
@
|
37
|
+
@coprocesses << proc
|
38
38
|
proc.when_done { task_completed(proc) }
|
39
39
|
proc.run unless proc.running?
|
40
40
|
proc
|
41
41
|
end
|
42
42
|
|
43
43
|
def spawn_proc(proc)
|
44
|
-
@
|
44
|
+
@coprocesses << Object.spawn do |coprocess|
|
45
45
|
proc.call(coprocess)
|
46
46
|
task_completed(coprocess)
|
47
47
|
rescue Exception => e
|
@@ -50,7 +50,7 @@ class Supervisor
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def still_running?
|
53
|
-
!@
|
53
|
+
!@coprocesses.empty?
|
54
54
|
end
|
55
55
|
|
56
56
|
def stop!(result = nil)
|
@@ -62,15 +62,15 @@ class Supervisor
|
|
62
62
|
|
63
63
|
def stop_all_tasks
|
64
64
|
exception = Exceptions::Stop.new
|
65
|
-
@
|
65
|
+
@coprocesses.each do |c|
|
66
66
|
EV.next_tick { c.interrupt(exception) }
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
def task_completed(coprocess)
|
71
|
-
return unless @
|
71
|
+
return unless @coprocesses.include?(coprocess)
|
72
72
|
|
73
|
-
@
|
74
|
-
@supervisor_fiber&.transfer if @
|
73
|
+
@coprocesses.delete(coprocess)
|
74
|
+
@supervisor_fiber&.transfer if @coprocesses.empty?
|
75
75
|
end
|
76
76
|
end
|
File without changes
|
data/lib/polyphony/http/agent.rb
CHANGED
data/lib/polyphony/http.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative '../polyphony'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
module Polyphony
|
6
|
+
module HTTP
|
7
|
+
auto_import(
|
8
|
+
Agent: './http/agent',
|
9
|
+
Rack: './http/rack',
|
10
|
+
Server: './http/server',
|
11
|
+
)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
export_default Polyphony::HTTP
|
data/lib/polyphony/net.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
export :tcp_connect,
|
4
|
-
:tcp_listen
|
5
|
-
# :getaddrinfo
|
4
|
+
:tcp_listen
|
6
5
|
|
7
6
|
import('./extensions/socket')
|
8
|
-
import('./extensions/
|
7
|
+
import('./extensions/openssl')
|
9
8
|
|
10
9
|
def tcp_connect(host, port, opts = {})
|
11
|
-
puts "tcp_connect(#{host.inspect}, #{port.inspect}, #{opts.inspect})"
|
12
10
|
socket = ::Socket.new(:INET, :STREAM).tap { |s|
|
13
11
|
addr = ::Socket.sockaddr_in(port, host)
|
14
12
|
s.connect(addr)
|
@@ -37,17 +35,16 @@ def tcp_listen(host = nil, port = nil, opts = {})
|
|
37
35
|
end
|
38
36
|
end
|
39
37
|
|
40
|
-
DEFAULT_SSL_CONTEXT = OpenSSL::SSL::SSLContext.new
|
41
|
-
DEFAULT_SSL_CONTEXT.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER)
|
42
|
-
|
43
38
|
def secure_socket(socket, context, opts)
|
44
|
-
context
|
45
|
-
|
46
|
-
|
39
|
+
if context
|
40
|
+
setup_alpn(context, opts[:alpn_protocols]) if opts[:alpn_protocols]
|
41
|
+
OpenSSL::SSL::SSLSocket.new(socket, context).tap { |s| s.connect }
|
42
|
+
else
|
43
|
+
OpenSSL::SSL::SSLSocket.new(socket).tap { |s| s.connect }
|
44
|
+
end
|
47
45
|
end
|
48
46
|
|
49
47
|
def secure_server(socket, context, opts)
|
50
|
-
context ||= DEFAULT_SSL_CONTEXT
|
51
48
|
setup_alpn(context, opts[:alpn_protocols]) if opts[:alpn_protocols]
|
52
49
|
OpenSSL::SSL::SSLServer.new(socket, context)
|
53
50
|
end
|
@@ -1,12 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative '../polyphony'
|
4
4
|
|
5
5
|
require "redis"
|
6
6
|
require "hiredis/reader"
|
7
7
|
|
8
|
-
Net = import('../net')
|
9
|
-
|
10
8
|
class Driver
|
11
9
|
def self.connect(config)
|
12
10
|
if config[:scheme] == "unix"
|
@@ -22,7 +20,7 @@ class Driver
|
|
22
20
|
end
|
23
21
|
|
24
22
|
def initialize(host, port)
|
25
|
-
@connection = Net.tcp_connect(host, port)
|
23
|
+
@connection = Polyphony::Net.tcp_connect(host, port)
|
26
24
|
@reader = ::Hiredis::Reader.new
|
27
25
|
end
|
28
26
|
|
@@ -56,8 +54,7 @@ class Driver
|
|
56
54
|
reply = @reader.gets
|
57
55
|
return reply if reply
|
58
56
|
|
59
|
-
|
60
|
-
data = @connection.read
|
57
|
+
while (data = @connection.readpartial(8192))
|
61
58
|
@reader.feed(data)
|
62
59
|
reply = @reader.gets
|
63
60
|
return reply if reply
|
data/lib/polyphony/version.rb
CHANGED