polyphony 0.80 → 0.81

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf3658689c8bb7614624ad75492b16fc31401c5c8ee37cd510bb4aab1c9d1449
4
- data.tar.gz: 5e2520d758db10e9dfe8022d6a85df61e1702f2e1e6560a95968e37b6f4f6e82
3
+ metadata.gz: aef5f8ee7585e1ae6a6632099329ba35b7dc6959dba5041f4cdcbaf92caded72
4
+ data.tar.gz: b168b56f080029e4167e2528e66b83186a635ea4bda8dc74882dfe70639421fc
5
5
  SHA512:
6
- metadata.gz: 7bd42d8fed28064941281c0e010f86a7ff7fff56e28c8e8190c8bc5c68eebd26a8d568e03ce20120db64684a036afb8d9b8586668c59f39fadebdaba2624ba75
7
- data.tar.gz: cd91d0394a0642fbb43d59cc3ba6a3100377be955f1cabeebc051f8f6159d6fb0e78fa7deedabc6bc4fabc20b12453c963b85ed9ce76002211d3423b05f51542
6
+ metadata.gz: aa7bd86706d5879c884b4f5bdc7a10556cbe28e6737ff89e5107589e1630028098dd815424ef357e51879a7e689cd5127ea30105eb1c36a174da7385b8f6f433
7
+ data.tar.gz: 3c4b35eed6900d55c5295c366803b944a6e423775ca5278f4b2600a309cb547cda12a2167ae40a4ef8e0001a1b6a8b8ea936129246f24d87b0eec59c9d31a3b4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.81 2022-03-03
2
+
3
+ - Restore public visibility for `Polyphony::Process.kill_process`
4
+ - Restore public visibility for `Polyphony::Net.setup_alpn`
5
+
1
6
  ## 0.80 2022-02-28
2
7
 
3
8
  - Prevent reentry into `trace_proc`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- polyphony (0.80)
4
+ polyphony (0.81)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,6 +9,7 @@
9
9
  #include "ruby.h"
10
10
  #include "ruby/io.h"
11
11
  #include "runqueue.h"
12
+ #include "polyphony.h"
12
13
 
13
14
  struct backend_stats {
14
15
  unsigned int runqueue_size;
@@ -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
- long dynamic_len = length == Qnil;
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 shrinkable;
339
- char *buf;
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
- if (str != Qnil) {
346
- int current_len = RSTRING_LEN(str);
347
- if (buf_pos < 0 || buf_pos > current_len) buf_pos = current_len;
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, buf, buffer_size - total, -1);
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 == buffer_size) {
387
- if (!dynamic_len) break;
418
+ if (total == buffer.size) {
419
+ if (!expandable_buffer) break;
388
420
 
389
421
  // resize buffer
390
- rb_str_resize(str, buf_pos + total);
391
- rb_str_modify_expand(str, buffer_size);
392
- buf = RSTRING_PTR(str) + buf_pos + total;
393
- shrinkable = 0;
394
- buffer_size += buffer_size;
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
- io_set_read_length(str, buf_pos + total, shrinkable);
401
- io_enc_str(str, fptr);
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
- char *buf = StringValuePtr(str);
518
- long len = RSTRING_LEN(str);
519
- long left = len;
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, buf, left, 0);
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
- buf += result;
587
+ buffer.base += result;
550
588
  left -= result;
551
589
  }
552
590
  }
553
591
 
554
- return INT2NUM(len);
592
+ return INT2NUM(buffer.size);
555
593
  }
556
594
 
557
595
  VALUE Backend_writev(VALUE self, VALUE io, int argc, VALUE *argv) {
@@ -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
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Polyphony
4
- VERSION = '0.80'
4
+ VERSION = '0.81'
5
5
  end
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.80'
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-02-28 00:00:00.000000000 Z
11
+ date: 2022-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler