polyphony 0.53.0 → 0.56.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0682736e3fdca0ada986f4210f1bb25d682c75a7b9d297451c186b90c8025e13'
4
- data.tar.gz: 5ff46620651c056983a6f73c879ca1c49acea22c0c7abcefb1a119cbe248cb77
3
+ metadata.gz: c6c9247de0e5049128fffb31879fd0a79ba6fe304e3802a5b7fa69baa4445ecf
4
+ data.tar.gz: f2e432fb0f9d6362b7d38834a0e37c7907328c6a27d6544e1dad5363ba4f715c
5
5
  SHA512:
6
- metadata.gz: cfbb8141b21cc9e56cdcd9a34fd55cd3b48573dcc3011f127ece5049b13ca2c7fa8a17c8525ba5996ab97afcbfc8e46064a3d68c79d71d44d40c274ff9051d2d
7
- data.tar.gz: f31d711c88fe354b8ffd63e37f30887b1bfb5c3709d3760412ee4ec4ea06d14c0a17e668ddd750958295dc12307b96cf64874b1be4ed46b8564d04236114ee67
6
+ metadata.gz: 3d69bdb3f12cb65cf580cc243ea0a4f479b5306cf087bcfc5e9d8def1fee49b998528f881844649bc01b501ea22371632fd1f4f08d1f8118692bdf2331f698e6
7
+ data.tar.gz: d49152bd9c9be94835a020f829c7ddc7f3766fcac9fca8e652bd8df43daab7a6eda90c94b40e57b3cef064d1ff2a957f42030aedefae1b2d1ad6a6587cf6fa86
@@ -26,6 +26,6 @@ jobs:
26
26
  - name: Show Linux kernel version
27
27
  run: uname -r
28
28
  - name: Compile C-extension
29
- run: bundle exec rake compile
29
+ run: POLYPHONY_USE_LIBEV=1 bundle exec rake compile
30
30
  - name: Run tests
31
31
  run: bundle exec rake test
data/.gitignore CHANGED
@@ -56,4 +56,6 @@ lib/*.bundle
56
56
  lib/*.so
57
57
 
58
58
  _site
59
- .sass-cache
59
+ .sass-cache
60
+
61
+ log
data/CHANGELOG.md CHANGED
@@ -1,8 +1,32 @@
1
- ## 0.53.0
1
+ ## 0.56.0 2021-06-22
2
+
3
+ - Implement fake `Backend#splice`, `Backend#splice_to_eof` methods for non-Linux
4
+ OS
5
+
6
+ ## 0.55.0 2021-06-17
7
+
8
+ - Finish io_uring implementation of Backend#chain
9
+ - Reimplement io_uring op_context acquire/release algorithm (using ref count)
10
+ - Fix #gets on sockets
11
+ - Redesign event anti-starvation mechanism
12
+
13
+ ## 0.54.0 2021-06-14
14
+
15
+ - Implement Mutex#owned?, #locked? (#50)
16
+ - Fix arity for SSLSocket#peeraddr (#55)
17
+ - Add missing SSLServer#accept_loop method (#53)
18
+ - Fix SSLSocket buffering behaviour
19
+ - Add recv_loop alias for SSLSocket (#54)
20
+
21
+ ## 0.53.2 2021-05-10
22
+
23
+ - Remove `splice` methods on libev backend on non-Linux OS (#43)
24
+
25
+ ## 0.53.0 2021-04-23
2
26
 
3
27
  - Implement `Backend#splice`, `Backend#splice_to_eof`, along with `IO#splice`, `IO#splice_to_eof`
4
28
 
5
- ## 0.52.0
29
+ ## 0.52.0 2021-02-28
6
30
 
7
31
  - Polyphony is now compatible with Ruby 3.0
8
32
  - Add `Backend#sendv` method for sending multiple strings
@@ -12,19 +36,19 @@
12
36
  - libev backend: Use` pidfd_open` for Linux 5.3+, otherwise use a libev child watcher
13
37
  - Use `:call` as default method in `#feed_loop`
14
38
 
15
- ## 0.51.0
39
+ ## 0.51.0 2021-02-02
16
40
 
17
41
  - Implement `IO#feed_loop`, `Socket#feed_loop`
18
42
  - Fix error handling in `Process.kill_and_await`
19
43
 
20
- ## 0.50.1
44
+ ## 0.50.1 2021-01-31
21
45
 
22
46
  - Set `IOSQE_ASYNC` flag in io_uring backend
23
47
  - Fix error handling in `Backend#waitpid`
24
48
  - Reimplement libev backend's `#waitpid` by using pidfd_open (in similar manner
25
49
  to the io_uring backend)
26
50
 
27
- ## 0.50.0
51
+ ## 0.50.0 2021-01-28
28
52
 
29
53
  - Use `Process::CLOCK_MONOTONIC` in Timer
30
54
  - Add `Timer#sleep`, `Timer#after`, `Timer#every`
@@ -32,50 +56,50 @@
32
56
  - Add `Thread#fiber_index_of` method
33
57
  - Use `Backend#wait_event` in `Fiber#await`
34
58
 
35
- ## 0.49.2
59
+ ## 0.49.2 2021-01-19
36
60
 
37
61
  - Fix hang with 100s or more child fibers when terminating
38
62
  - Fix double pending_count increment in io_uring backend
39
63
 
40
- ## 0.49.1
64
+ ## 0.49.1 2021-01-13
41
65
 
42
66
  - Use `TCPSocket` instead of `Socket` in `Net.tcp_connect`
43
67
  - Catch `Errno::ERSCH` in `Process.kill_and_await`
44
68
  - Set io_uring queue size to 2048
45
69
 
46
- ## 0.49.0
70
+ ## 0.49.0 2021-01-11
47
71
 
48
72
  - Implement `Polyphony::Timer` for performant timeouts
49
73
 
50
- ## 0.48.0
74
+ ## 0.48.0 2021-01-05
51
75
 
52
76
  - Implement graceful shutdown
53
77
  - Add support for `break` / `StopIteration` in `spin_loop`
54
78
  - Fix `IO#gets`, `IO#readpartial`
55
79
 
56
- ## 0.47.5.1
80
+ ## 0.47.5.1 2020-11-20
57
81
 
58
82
  - Add missing `Socket#accept_loop` method
59
83
 
60
- ## 0.47.5
84
+ ## 0.47.5 2020-11-20
61
85
 
62
86
  - Add `socket_class` argument to `Backend#accept`, `Backend#accept_loop`
63
87
  - Fix `#supervise` to stop when all children fibers are done
64
88
 
65
- ## 0.47.4
89
+ ## 0.47.4 2020-11-14
66
90
 
67
91
  - Add support for Unix sockets
68
92
 
69
- ## 0.47.3
93
+ ## 0.47.3 2020-11-12
70
94
 
71
95
  - Enable I/O in signal handlers (#45)
72
96
  - Accept `:interval` argument in `#spin_loop`
73
97
 
74
- ## 0.47.2
98
+ ## 0.47.2 2020-11-10
75
99
 
76
100
  - Fix API compatibility between TCPSocket and IO
77
101
 
78
- ## 0.47.0
102
+ ## 0.47.0 2020-11-10
79
103
 
80
104
  - Implement `#spin_scope` used for creating blocking fiber scopes
81
105
  - Reimplement `move_on_after`, `cancel_after`, `Timeout.timeout` using
@@ -83,18 +107,18 @@
83
107
  - Implement `Backend#timeout` API
84
108
  - Implemented capped queues
85
109
 
86
- ## 0.46.1
110
+ ## 0.46.1 2020-11-04
87
111
 
88
112
  - Add `TCPServer#accept_loop`, `OpenSSL::SSL::SSLSocket#accept_loop` method
89
113
  - Fix compilation error on MacOS (#43)
90
114
  - Fix backtrace for `Timeout.timeout`
91
115
  - Add `Backend#timer_loop`
92
116
 
93
- ## 0.46.0
117
+ ## 0.46.0 2020-10-08
94
118
 
95
119
  - Implement [io_uring backend](https://github.com/digital-fabric/polyphony/pull/44)
96
120
 
97
- ## 0.45.5
121
+ ## 0.45.5 2020-10-04
98
122
 
99
123
  - Fix compilation error (#43)
100
124
  - Add support for resetting move_on_after, cancel_after timeouts
@@ -103,22 +127,22 @@
103
127
  - Schedule parent with priority on uncaught exception
104
128
  - Fix race condition in `Mutex#synchronize` (#41)
105
129
 
106
- ## 0.45.4
130
+ ## 0.45.4 2020-09-06
107
131
 
108
132
  - Improve signal trapping mechanism
109
133
 
110
- ## 0.45.3
134
+ ## 0.45.3 2020-09-02
111
135
 
112
136
  - Don't swallow error in `Process#kill_and_await`
113
137
  - Add `Fiber#mailbox` attribute reader
114
138
  - Fix bug in `Fiber.await`
115
139
  - Implement `IO#getc`, `IO#getbyte`
116
140
 
117
- ## 0.45.2
141
+ ## 0.45.2 2020-08-03
118
142
 
119
143
  - Rewrite `Fiber#<<`, `Fiber#await`, `Fiber#receive` in C
120
144
 
121
- ## 0.45.1
145
+ ## 0.45.1 2020-08-01
122
146
 
123
147
  - Fix Net::HTTP compatibility
124
148
  - Fix fs adapter
@@ -128,7 +152,7 @@
128
152
  - Cleanup code
129
153
  - Improve support for Ruby 3 keyword args
130
154
 
131
- ## 0.45.0
155
+ ## 0.45.0 2020-07-29
132
156
 
133
157
  - Cleanup code
134
158
  - Rename `Agent` to `Backend`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- polyphony (0.53.0)
4
+ polyphony (0.56.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -17,6 +17,7 @@ GEM
17
17
  mime-types (~> 3.0)
18
18
  multi_xml (>= 0.5.2)
19
19
  json (2.3.0)
20
+ localhost (1.1.8)
20
21
  method_source (1.0.0)
21
22
  mime-types (3.3.1)
22
23
  mime-types-data (~> 3.2015)
@@ -72,6 +73,7 @@ DEPENDENCIES
72
73
  hiredis (= 0.6.3)
73
74
  http_parser.rb (~> 0.6.0)
74
75
  httparty (= 0.17.1)
76
+ localhost (~> 1.1.4)
75
77
  minitest (= 5.14.4)
76
78
  minitest-reporters (= 1.4.2)
77
79
  msgpack (= 1.4.2)
data/TODO.md CHANGED
@@ -10,9 +10,6 @@
10
10
 
11
11
  - Add support for `break` and `StopIteration` in all loops (with tests)
12
12
 
13
- - Change `IO#gets` to use `String#split` to cut into lines, much faster (see
14
- examples/performance/line_splitting.rb)
15
-
16
13
  - More tight loops
17
14
  - `IO#gets_loop`, `Socket#gets_loop`, `OpenSSL::Socket#gets_loop` (medium effort)
18
15
  - `Fiber#receive_loop` (very little effort, should be implemented in C)
@@ -35,8 +32,8 @@
35
32
 
36
33
  ```ruby
37
34
  Thread.current.backend.submit(
38
- [sock, :<<, chunk_header(len)],
39
- [sock, :splice, file, len]
35
+ [:send, sock, chunk_header(len)],
36
+ [:splice, file, sock, len]
40
37
  )
41
38
  ```
42
39
 
@@ -51,8 +48,8 @@
51
48
  break if len == 0
52
49
 
53
50
  backend.submit(
54
- [sock, :<<, chunk_header(len)],
55
- [sock, :splice, file, len]
51
+ [:write, sock, chunk_header(len)],
52
+ [:splice, i, sock, len]
56
53
  )
57
54
  end
58
55
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+
5
+ require 'polyphony'
6
+
7
+ GC.disable
8
+
9
+ p count: GC.count
10
+ snooze
11
+ p count_after_snooze: GC.count
12
+ sleep 0.1
13
+ p count_after_sleep: GC.count
14
+
15
+ Thread.current.backend.idle_gc_period = 60
16
+
17
+ p count: GC.count
18
+ snooze
19
+ p count_after_snooze: GC.count
20
+ sleep 0.1
21
+ p count_after_sleep: GC.count
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'polyphony'
5
+
6
+ queue = Queue.new
7
+
8
+ 4.times { |i|
9
+ spin_loop {
10
+ job = queue.pop
11
+ puts("worker %d job %s" % [i, job.inspect])
12
+ }
13
+ }
14
+
15
+ (1..10).each do |i|
16
+ queue << "job#{i}"
17
+ end
18
+
19
+ sleep 0.1 until queue.empty?
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'polyphony'
5
+ require 'localhost/authority'
6
+
7
+ authority = Localhost::Authority.fetch
8
+ opts = {
9
+ reuse_addr: true,
10
+ dont_linger: true,
11
+ secure_context: authority.server_context
12
+ }
13
+
14
+ server = Polyphony::Net.tcp_listen('localhost', 1234, opts)
15
+
16
+ puts 'Serving HTTPS on port 1234'
17
+
18
+ spin_loop(interval: 1) { STDOUT << '.' }
19
+
20
+ # server.accept_loop do |socket|
21
+ while (socket = server.accept)
22
+ spin do
23
+ while (data = socket.gets("\n", 8192))
24
+ if data.chomp.empty?
25
+ socket << "HTTP/1.1 200 OK\nConnection: close\nContent-Length: 4\n\nfoo\n"
26
+ break
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'polyphony'
5
+
6
+ i, o = IO.pipe
7
+ f = spin { p i.read }
8
+
9
+ o << 'hello'
10
+ o.close
11
+ f.await
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'polyphony'
5
+
6
+ puts 'Please enter your name:'
7
+ name = gets.chomp
8
+ puts "Hello, #{name}!"
@@ -0,0 +1,186 @@
1
+ #include <time.h>
2
+ #include <fcntl.h>
3
+ #include "ruby.h"
4
+ #include "ruby/io.h"
5
+ #include "polyphony.h"
6
+ #include "backend_common.h"
7
+
8
+ #ifdef POLYPHONY_USE_PIDFD_OPEN
9
+ #ifndef __NR_pidfd_open
10
+ #define __NR_pidfd_open 434 /* System call # on most architectures */
11
+ #endif
12
+
13
+ inline int pidfd_open(pid_t pid, unsigned int flags) {
14
+ return syscall(__NR_pidfd_open, pid, flags);
15
+ }
16
+ #endif
17
+
18
+ //////////////////////////////////////////////////////////////////////
19
+ //////////////////////////////////////////////////////////////////////
20
+ // the following is copied verbatim from the Ruby source code (io.c)
21
+
22
+ inline int io_setstrbuf(VALUE *str, long len) {
23
+ #ifdef _WIN32
24
+ len = (len + 1) & ~1L; /* round up for wide char */
25
+ #endif
26
+ if (*str == Qnil) {
27
+ *str = rb_str_new(0, len);
28
+ return 1;
29
+ }
30
+ else {
31
+ VALUE s = StringValue(*str);
32
+ long clen = RSTRING_LEN(s);
33
+ if (clen >= len) {
34
+ rb_str_modify(s);
35
+ return 0;
36
+ }
37
+ len -= clen;
38
+ }
39
+ rb_str_modify_expand(*str, len);
40
+ return 0;
41
+ }
42
+
43
+ #define MAX_REALLOC_GAP 4096
44
+
45
+ inline void io_shrink_read_string(VALUE str, long n) {
46
+ if (rb_str_capacity(str) - n > MAX_REALLOC_GAP) {
47
+ rb_str_resize(str, n);
48
+ }
49
+ }
50
+
51
+ inline void io_set_read_length(VALUE str, long n, int shrinkable) {
52
+ if (RSTRING_LEN(str) != n) {
53
+ rb_str_modify(str);
54
+ rb_str_set_len(str, n);
55
+ if (shrinkable) io_shrink_read_string(str, n);
56
+ }
57
+ }
58
+
59
+ inline rb_encoding* io_read_encoding(rb_io_t *fptr) {
60
+ if (fptr->encs.enc) {
61
+ return fptr->encs.enc;
62
+ }
63
+ return rb_default_external_encoding();
64
+ }
65
+
66
+ inline VALUE io_enc_str(VALUE str, rb_io_t *fptr) {
67
+ OBJ_TAINT(str);
68
+ rb_enc_associate(str, io_read_encoding(fptr));
69
+ return str;
70
+ }
71
+
72
+ //////////////////////////////////////////////////////////////////////
73
+ //////////////////////////////////////////////////////////////////////
74
+
75
+ VALUE backend_await(struct Backend_base *backend) {
76
+ VALUE ret;
77
+ backend->pending_count++;
78
+ ret = Thread_switch_fiber(rb_thread_current());
79
+ backend->pending_count--;
80
+ RB_GC_GUARD(ret);
81
+ return ret;
82
+ }
83
+
84
+ VALUE backend_snooze() {
85
+ Fiber_make_runnable(rb_fiber_current(), Qnil);
86
+ VALUE ret = Thread_switch_fiber(rb_thread_current());
87
+ return ret;
88
+ }
89
+
90
+ inline void rectify_io_file_pos(rb_io_t *fptr) {
91
+ // Apparently after reopening a closed file, the file position is not reset,
92
+ // which causes the read to fail. Fortunately we can use fptr->rbuf.len to
93
+ // find out if that's the case.
94
+ // See: https://github.com/digital-fabric/polyphony/issues/30
95
+ if (fptr->rbuf.len > 0) {
96
+ lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
97
+ fptr->rbuf.len = 0;
98
+ }
99
+ }
100
+
101
+ inline double current_time() {
102
+ struct timespec ts;
103
+ clock_gettime(CLOCK_MONOTONIC, &ts);
104
+ long long ns = ts.tv_sec;
105
+ ns = ns * 1e9 + ts.tv_nsec;
106
+ double t = ns;
107
+ return t / 1e9;
108
+ }
109
+
110
+ inline VALUE backend_timeout_exception(VALUE exception) {
111
+ if (rb_obj_is_kind_of(exception, rb_cArray) == Qtrue)
112
+ return rb_funcall(rb_ary_entry(exception, 0), ID_new, 1, rb_ary_entry(exception, 1));
113
+ else if (rb_obj_is_kind_of(exception, rb_cClass) == Qtrue)
114
+ return rb_funcall(exception, ID_new, 0);
115
+ else
116
+ return rb_funcall(rb_eRuntimeError, ID_new, 1, exception);
117
+ }
118
+
119
+ VALUE Backend_timeout_safe(VALUE arg) {
120
+ return rb_yield(arg);
121
+ }
122
+
123
+ VALUE Backend_timeout_rescue(VALUE arg, VALUE exception) {
124
+ return exception;
125
+ }
126
+
127
+ VALUE Backend_timeout_ensure_safe(VALUE arg) {
128
+ return rb_rescue2(Backend_timeout_safe, Qnil, Backend_timeout_rescue, Qnil, rb_eException, (VALUE)0);
129
+ }
130
+
131
+ static VALUE empty_string = Qnil;
132
+
133
+ VALUE Backend_sendv(VALUE self, VALUE io, VALUE ary, VALUE flags) {
134
+ switch (RARRAY_LEN(ary)) {
135
+ case 0:
136
+ return Qnil;
137
+ case 1:
138
+ return Backend_send(self, io, RARRAY_AREF(ary, 0), flags);
139
+ default:
140
+ if (empty_string == Qnil) {
141
+ empty_string = rb_str_new_literal("");
142
+ rb_global_variable(&empty_string);
143
+ }
144
+ VALUE joined = rb_ary_join(ary, empty_string);
145
+ VALUE result = Backend_send(self, io, joined, flags);
146
+ RB_GC_GUARD(joined);
147
+ return result;
148
+ }
149
+ }
150
+
151
+ inline void io_verify_blocking_mode(rb_io_t *fptr, VALUE io, VALUE blocking) {
152
+ VALUE blocking_mode = rb_ivar_get(io, ID_ivar_blocking_mode);
153
+ if (blocking == blocking_mode) return;
154
+
155
+ rb_ivar_set(io, ID_ivar_blocking_mode, blocking);
156
+
157
+ #ifdef _WIN32
158
+ if (blocking != Qtrue)
159
+ rb_w32_set_nonblock(fptr->fd);
160
+ #elif defined(F_GETFL)
161
+ int flags = fcntl(fptr->fd, F_GETFL);
162
+ if (flags == -1) return;
163
+ int is_nonblocking = flags & O_NONBLOCK;
164
+
165
+ if (blocking == Qtrue) {
166
+ if (!is_nonblocking) return;
167
+ flags &= ~O_NONBLOCK;
168
+ } else {
169
+ if (is_nonblocking) return;
170
+ flags |= O_NONBLOCK;
171
+ }
172
+ fcntl(fptr->fd, F_SETFL, flags);
173
+ #endif
174
+ }
175
+
176
+ inline void backend_run_idle_tasks(struct Backend_base *base) {
177
+ if (base->idle_gc_period == 0) return;
178
+
179
+ double now = current_time();
180
+ if (now - base->idle_gc_last_time < base->idle_gc_period) return;
181
+
182
+ base->idle_gc_last_time = now;
183
+ rb_gc_enable();
184
+ rb_gc_start();
185
+ rb_gc_disable();
186
+ }