polyphony 0.80 → 0.81
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/ext/polyphony/backend_common.h +1 -0
- data/ext/polyphony/backend_io_uring.c +66 -28
- data/ext/polyphony/polyphony.c +8 -0
- data/ext/polyphony/polyphony.h +11 -0
- data/lib/polyphony/adapters/process.rb +2 -2
- data/lib/polyphony/net.rb +15 -15
- data/lib/polyphony/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aef5f8ee7585e1ae6a6632099329ba35b7dc6959dba5041f4cdcbaf92caded72
|
4
|
+
data.tar.gz: b168b56f080029e4167e2528e66b83186a635ea4bda8dc74882dfe70639421fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa7bd86706d5879c884b4f5bdc7a10556cbe28e6737ff89e5107589e1630028098dd815424ef357e51879a7e689cd5127ea30105eb1c36a174da7385b8f6f433
|
7
|
+
data.tar.gz: 3c4b35eed6900d55c5295c366803b944a6e423775ca5278f4b2600a309cb547cda12a2167ae40a4ef8e0001a1b6a8b8ea936129246f24d87b0eec59c9d31a3b4
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -102,6 +102,24 @@ static VALUE Backend_initialize(VALUE self) {
|
|
102
102
|
return self;
|
103
103
|
}
|
104
104
|
|
105
|
+
static inline struct io_buffer get_io_buffer(VALUE in) {
|
106
|
+
if (FIXNUM_P(in)) {
|
107
|
+
struct raw_buffer *raw = (struct raw_buffer *)(FIX2LONG(in));
|
108
|
+
return (struct io_buffer){ raw->base, raw->size, 1 };
|
109
|
+
}
|
110
|
+
return (struct io_buffer){ RSTRING_PTR(in), RSTRING_LEN(in), 0 };
|
111
|
+
}
|
112
|
+
|
113
|
+
static inline VALUE coerce_io_string_or_buffer(VALUE buf) {
|
114
|
+
switch (TYPE(buf)) {
|
115
|
+
case T_STRING:
|
116
|
+
case T_FIXNUM:
|
117
|
+
return buf;
|
118
|
+
default:
|
119
|
+
return StringValue(buf);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
105
123
|
VALUE Backend_finalize(VALUE self) {
|
106
124
|
Backend_t *backend;
|
107
125
|
GetBackend(self, backend);
|
@@ -332,23 +350,37 @@ VALUE io_uring_backend_wait_fd(Backend_t *backend, int fd, int write) {
|
|
332
350
|
VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof, VALUE pos) {
|
333
351
|
Backend_t *backend;
|
334
352
|
rb_io_t *fptr;
|
335
|
-
|
336
|
-
long buffer_size = dynamic_len ? 4096 : NUM2INT(length);
|
353
|
+
struct io_buffer buffer = get_io_buffer(str);
|
337
354
|
long buf_pos = NUM2INT(pos);
|
338
|
-
int
|
339
|
-
|
355
|
+
int shrinkable_string = 0;
|
356
|
+
int expandable_buffer = 0;
|
340
357
|
long total = 0;
|
341
358
|
int read_to_eof = RTEST(to_eof);
|
342
359
|
VALUE underlying_io = rb_ivar_get(io, ID_ivar_io);
|
343
360
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
361
|
+
if (buffer.raw) {
|
362
|
+
if (buf_pos < 0 || buf_pos > buffer.size) buf_pos = buffer.size;
|
363
|
+
buffer.base += buf_pos;
|
364
|
+
buffer.size -= buf_pos;
|
365
|
+
}
|
366
|
+
else {
|
367
|
+
expandable_buffer = length == Qnil;
|
368
|
+
long expected_read_length = expandable_buffer ? 4096 : FIX2INT(length);
|
369
|
+
long string_cap = rb_str_capacity(str);
|
370
|
+
if (buf_pos < 0 || buf_pos > buffer.size) buf_pos = buffer.size;
|
371
|
+
|
372
|
+
if (string_cap < expected_read_length + buf_pos) {
|
373
|
+
shrinkable_string = io_setstrbuf(&str, expected_read_length + buf_pos);
|
374
|
+
buffer.base = RSTRING_PTR(str) + buf_pos;
|
375
|
+
buffer.size = expected_read_length;
|
376
|
+
}
|
377
|
+
else {
|
378
|
+
buffer.base += buf_pos;
|
379
|
+
buffer.size = string_cap - buf_pos;
|
380
|
+
if (buffer.size > expected_read_length)
|
381
|
+
buffer.size = expected_read_length;
|
382
|
+
}
|
348
383
|
}
|
349
|
-
else buf_pos = 0;
|
350
|
-
shrinkable = io_setstrbuf(&str, buf_pos + buffer_size);
|
351
|
-
buf = RSTRING_PTR(str) + buf_pos;
|
352
384
|
|
353
385
|
GetBackend(self, backend);
|
354
386
|
if (underlying_io != Qnil) io = underlying_io;
|
@@ -364,7 +396,7 @@ VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof,
|
|
364
396
|
int result;
|
365
397
|
int completed;
|
366
398
|
|
367
|
-
io_uring_prep_read(sqe, fptr->fd,
|
399
|
+
io_uring_prep_read(sqe, fptr->fd, buffer.base, buffer.size - total, -1);
|
368
400
|
|
369
401
|
result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
|
370
402
|
completed = context_store_release(&backend->store, ctx);
|
@@ -383,22 +415,28 @@ VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof,
|
|
383
415
|
total += result;
|
384
416
|
if (!read_to_eof) break;
|
385
417
|
|
386
|
-
if (total ==
|
387
|
-
if (!
|
418
|
+
if (total == buffer.size) {
|
419
|
+
if (!expandable_buffer) break;
|
388
420
|
|
389
421
|
// resize buffer
|
390
|
-
rb_str_resize(str,
|
391
|
-
rb_str_modify_expand(str,
|
392
|
-
|
393
|
-
|
394
|
-
|
422
|
+
rb_str_resize(str, total + buf_pos);
|
423
|
+
rb_str_modify_expand(str, buffer.size);
|
424
|
+
shrinkable_string = 0;
|
425
|
+
buffer.base = RSTRING_PTR(str) + total + buf_pos;
|
426
|
+
buffer.size = buffer.size;
|
427
|
+
}
|
428
|
+
else {
|
429
|
+
buffer.base += result;
|
430
|
+
buffer.size -= result;
|
431
|
+
if (!buffer.size) break;
|
395
432
|
}
|
396
|
-
else buf += result;
|
397
433
|
}
|
398
434
|
}
|
399
435
|
|
400
|
-
|
401
|
-
|
436
|
+
if (!buffer.raw) {
|
437
|
+
io_set_read_length(str, buf_pos + total, shrinkable_string);
|
438
|
+
io_enc_str(str, fptr);
|
439
|
+
}
|
402
440
|
|
403
441
|
if (!total) return Qnil;
|
404
442
|
|
@@ -514,9 +552,9 @@ VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
|
|
514
552
|
Backend_t *backend;
|
515
553
|
rb_io_t *fptr;
|
516
554
|
VALUE underlying_io;
|
517
|
-
|
518
|
-
|
519
|
-
long left =
|
555
|
+
|
556
|
+
struct io_buffer buffer = get_io_buffer(str);
|
557
|
+
long left = buffer.size;
|
520
558
|
|
521
559
|
underlying_io = rb_ivar_get(io, ID_ivar_io);
|
522
560
|
if (underlying_io != Qnil) io = underlying_io;
|
@@ -532,7 +570,7 @@ VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
|
|
532
570
|
int result;
|
533
571
|
int completed;
|
534
572
|
|
535
|
-
io_uring_prep_write(sqe, fptr->fd,
|
573
|
+
io_uring_prep_write(sqe, fptr->fd, buffer.base, left, 0);
|
536
574
|
|
537
575
|
result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
|
538
576
|
completed = context_store_release(&backend->store, ctx);
|
@@ -546,12 +584,12 @@ VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
|
|
546
584
|
if (result < 0)
|
547
585
|
rb_syserr_fail(-result, strerror(-result));
|
548
586
|
else {
|
549
|
-
|
587
|
+
buffer.base += result;
|
550
588
|
left -= result;
|
551
589
|
}
|
552
590
|
}
|
553
591
|
|
554
|
-
return INT2NUM(
|
592
|
+
return INT2NUM(buffer.size);
|
555
593
|
}
|
556
594
|
|
557
595
|
VALUE Backend_writev(VALUE self, VALUE io, int argc, VALUE *argv) {
|
data/ext/polyphony/polyphony.c
CHANGED
@@ -118,6 +118,12 @@ VALUE Polyphony_backend_write(int argc, VALUE *argv, VALUE self) {
|
|
118
118
|
return Backend_write_m(argc, argv, BACKEND());
|
119
119
|
}
|
120
120
|
|
121
|
+
VALUE Polyphony_backend_test(VALUE self, VALUE io, VALUE str) {
|
122
|
+
struct raw_buffer buffer = { RSTRING_PTR(str), RSTRING_LEN(str) };
|
123
|
+
VALUE args[2] = { io, LONG2FIX((long)&buffer) };
|
124
|
+
return Polyphony_backend_write(2, args, self);
|
125
|
+
}
|
126
|
+
|
121
127
|
// VALUE Polyphony_backend_close(VALUE self, VALUE io) {
|
122
128
|
// return Backend_close(BACKEND(), io);
|
123
129
|
// }
|
@@ -149,6 +155,8 @@ void Init_Polyphony() {
|
|
149
155
|
// rb_define_singleton_method(mPolyphony, "backend_close", Polyphony_backend_close, 1);
|
150
156
|
rb_define_singleton_method(mPolyphony, "backend_verify_blocking_mode", Backend_verify_blocking_mode, 2);
|
151
157
|
|
158
|
+
rb_define_singleton_method(mPolyphony, "backend_test", Polyphony_backend_test, 2);
|
159
|
+
|
152
160
|
rb_define_global_function("snooze", Polyphony_snooze, 0);
|
153
161
|
rb_define_global_function("suspend", Polyphony_suspend, 0);
|
154
162
|
|
data/ext/polyphony/polyphony.h
CHANGED
@@ -135,4 +135,15 @@ VALUE Thread_switch_fiber(VALUE thread);
|
|
135
135
|
|
136
136
|
VALUE Polyphony_snooze(VALUE self);
|
137
137
|
|
138
|
+
struct raw_buffer {
|
139
|
+
char *base;
|
140
|
+
int size;
|
141
|
+
};
|
142
|
+
|
143
|
+
struct io_buffer {
|
144
|
+
char *base;
|
145
|
+
int size;
|
146
|
+
int raw;
|
147
|
+
};
|
148
|
+
|
138
149
|
#endif /* POLYPHONY_H */
|
@@ -24,8 +24,6 @@ module Polyphony
|
|
24
24
|
kill_process(pid) unless terminated || pid.nil?
|
25
25
|
end
|
26
26
|
|
27
|
-
private
|
28
|
-
|
29
27
|
def kill_process(pid)
|
30
28
|
cancel_after(5) do
|
31
29
|
kill_and_await('TERM', pid)
|
@@ -34,6 +32,8 @@ module Polyphony
|
|
34
32
|
kill_and_await(-9, pid)
|
35
33
|
end
|
36
34
|
|
35
|
+
private
|
36
|
+
|
37
37
|
def kill_and_await(sig, pid)
|
38
38
|
::Process.kill(sig, pid)
|
39
39
|
Polyphony.backend_waitpid(pid)
|
data/lib/polyphony/net.rb
CHANGED
@@ -53,6 +53,21 @@ module Polyphony
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
# Sets up ALPN negotiation for the given context. The ALPN handler for the
|
57
|
+
# context will select the first protocol from the list given by the client
|
58
|
+
# that appears in the list of given protocols, according to the specified
|
59
|
+
# order.
|
60
|
+
#
|
61
|
+
# @param context [SSLContext] SSL context
|
62
|
+
# @param protocols [Array] array of supported protocols
|
63
|
+
# @return [void]
|
64
|
+
def setup_alpn(context, protocols)
|
65
|
+
context.alpn_protocols = protocols
|
66
|
+
context.alpn_select_cb = lambda do |peer_protocols|
|
67
|
+
(protocols & peer_protocols).first
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
56
71
|
private
|
57
72
|
|
58
73
|
# Creates a listening `Socket` instance.
|
@@ -114,21 +129,6 @@ module Polyphony
|
|
114
129
|
setup_alpn(context, opts[:alpn_protocols]) if opts[:alpn_protocols]
|
115
130
|
OpenSSL::SSL::SSLServer.new(socket, context)
|
116
131
|
end
|
117
|
-
|
118
|
-
# Sets up ALPN negotiation for the given context. The ALPN handler for the
|
119
|
-
# context will select the first protocol from the list given by the client
|
120
|
-
# that appears in the list of given protocols, according to the specified
|
121
|
-
# order.
|
122
|
-
#
|
123
|
-
# @param context [SSLContext] SSL context
|
124
|
-
# @param protocols [Array] array of supported protocols
|
125
|
-
# @return [void]
|
126
|
-
def setup_alpn(context, protocols)
|
127
|
-
context.alpn_protocols = protocols
|
128
|
-
context.alpn_select_cb = lambda do |peer_protocols|
|
129
|
-
(protocols & peer_protocols).first
|
130
|
-
end
|
131
|
-
end
|
132
132
|
end
|
133
133
|
end
|
134
134
|
end
|
data/lib/polyphony/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polyphony
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.81'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|