uringmachine 0.31.0 → 0.32.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +36 -37
- data/TODO.md +18 -55
- data/benchmark/bm_io_pipe.rb +2 -2
- data/benchmark/common.rb +16 -16
- data/benchmark/gets.rb +7 -7
- data/benchmark/gets_concurrent.rb +12 -12
- data/benchmark/http_parse.rb +11 -11
- data/benchmark/http_server_accept_queue.rb +7 -7
- data/benchmark/http_server_multi_accept.rb +7 -7
- data/benchmark/http_server_multi_ractor.rb +7 -7
- data/benchmark/http_server_single_thread.rb +8 -8
- data/benchmark/openssl.rb +4 -4
- data/examples/fiber_concurrency_io.rb +1 -1
- data/examples/fiber_concurrency_um.rb +16 -0
- data/examples/io_uring_simple.c +13 -4
- data/examples/pg.rb +2 -2
- data/examples/um_cancellation.rb +20 -0
- data/examples/um_fiber_scheduler.rb +10 -0
- data/examples/um_io.rb +19 -0
- data/examples/um_mo.c +32 -0
- data/examples/um_multishot.rb +15 -0
- data/examples/um_ssl.rb +11 -0
- data/ext/um/um.h +19 -19
- data/ext/um/um_ext.c +2 -4
- data/ext/um/{um_connection.c → um_io.c} +210 -210
- data/ext/um/{um_connection_class.c → um_io_class.c} +102 -102
- data/lib/uringmachine/version.rb +1 -1
- data/lib/uringmachine.rb +12 -12
- data/test/{test_connection.rb → test_io.rb} +29 -29
- data/test/test_um.rb +7 -7
- metadata +13 -6
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
#include "um.h"
|
|
2
2
|
|
|
3
|
-
VALUE
|
|
4
|
-
VALUE
|
|
3
|
+
VALUE cIO;
|
|
4
|
+
VALUE eIORESPError;
|
|
5
5
|
|
|
6
6
|
VALUE SYM_fd;
|
|
7
7
|
VALUE SYM_socket;
|
|
8
8
|
VALUE SYM_ssl;
|
|
9
9
|
|
|
10
|
-
inline int
|
|
10
|
+
inline int io_has_target_obj_p(struct um_io *conn) {
|
|
11
11
|
switch (conn->mode) {
|
|
12
|
-
case
|
|
13
|
-
case
|
|
14
|
-
case
|
|
12
|
+
case IO_SSL:
|
|
13
|
+
case IO_STRING:
|
|
14
|
+
case IO_IO_BUFFER:
|
|
15
15
|
return true;
|
|
16
16
|
default:
|
|
17
17
|
return false;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
inline void
|
|
21
|
+
inline void io_mark_segments(struct um_io *conn) {
|
|
22
22
|
struct um_segment *curr = conn->head;
|
|
23
23
|
while (curr) {
|
|
24
24
|
// rb_gc_mark_movable(curr->obj);
|
|
@@ -26,7 +26,7 @@ inline void connection_mark_segments(struct um_connection *conn) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
inline void
|
|
29
|
+
inline void io_compact_segments(struct um_io *conn) {
|
|
30
30
|
struct um_segment *curr = conn->head;
|
|
31
31
|
while (curr) {
|
|
32
32
|
// curr->obj = rb_gc_location(curr->obj);
|
|
@@ -34,63 +34,63 @@ inline void connection_compact_segments(struct um_connection *conn) {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
static void
|
|
38
|
-
struct
|
|
37
|
+
static void IO_mark(void *ptr) {
|
|
38
|
+
struct um_io *conn = ptr;
|
|
39
39
|
rb_gc_mark_movable(conn->self);
|
|
40
40
|
rb_gc_mark_movable(conn->machine->self);
|
|
41
41
|
|
|
42
|
-
if (
|
|
42
|
+
if (io_has_target_obj_p(conn)) {
|
|
43
43
|
rb_gc_mark_movable(conn->target);
|
|
44
|
-
|
|
44
|
+
io_mark_segments(conn);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
static void
|
|
49
|
-
struct
|
|
48
|
+
static void IO_compact(void *ptr) {
|
|
49
|
+
struct um_io *conn = ptr;
|
|
50
50
|
conn->self = rb_gc_location(conn->self);
|
|
51
51
|
|
|
52
|
-
if (
|
|
52
|
+
if (io_has_target_obj_p(conn)) {
|
|
53
53
|
conn->target = rb_gc_location(conn->target);
|
|
54
|
-
|
|
54
|
+
io_compact_segments(conn);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
static void
|
|
59
|
-
struct
|
|
60
|
-
|
|
58
|
+
static void IO_free(void *ptr) {
|
|
59
|
+
struct um_io *conn = ptr;
|
|
60
|
+
io_clear(conn);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
static const rb_data_type_t
|
|
64
|
-
.wrap_struct_name = "UringMachine::
|
|
63
|
+
static const rb_data_type_t IO_type = {
|
|
64
|
+
.wrap_struct_name = "UringMachine::IO",
|
|
65
65
|
.function = {
|
|
66
|
-
.dmark =
|
|
67
|
-
.dfree =
|
|
66
|
+
.dmark = IO_mark,
|
|
67
|
+
.dfree = IO_free,
|
|
68
68
|
.dsize = NULL,
|
|
69
|
-
.dcompact =
|
|
69
|
+
.dcompact = IO_compact
|
|
70
70
|
},
|
|
71
71
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
-
static VALUE
|
|
75
|
-
struct
|
|
76
|
-
VALUE self = TypedData_Make_Struct(klass, struct
|
|
74
|
+
static VALUE IO_allocate(VALUE klass) {
|
|
75
|
+
struct um_io *conn;
|
|
76
|
+
VALUE self = TypedData_Make_Struct(klass, struct um_io, &IO_type, conn);
|
|
77
77
|
return self;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
static inline struct
|
|
81
|
-
struct
|
|
82
|
-
TypedData_Get_Struct(self, struct
|
|
80
|
+
static inline struct um_io *um_get_io(VALUE self) {
|
|
81
|
+
struct um_io *conn;
|
|
82
|
+
TypedData_Get_Struct(self, struct um_io, &IO_type, conn);
|
|
83
83
|
return conn;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
static inline void
|
|
86
|
+
static inline void io_set_target(struct um_io *conn, VALUE target, enum um_io_mode mode) {
|
|
87
87
|
conn->mode = mode;
|
|
88
88
|
switch (mode) {
|
|
89
|
-
case
|
|
90
|
-
case
|
|
89
|
+
case IO_FD:
|
|
90
|
+
case IO_SOCKET:
|
|
91
91
|
conn->fd = NUM2INT(target);
|
|
92
92
|
return;
|
|
93
|
-
case
|
|
93
|
+
case IO_SSL:
|
|
94
94
|
conn->target = target;
|
|
95
95
|
um_ssl_set_bio(conn->machine, target);
|
|
96
96
|
return;
|
|
@@ -99,20 +99,20 @@ static inline void connection_set_target(struct um_connection *conn, VALUE targe
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
static inline void
|
|
102
|
+
static inline void io_setup(struct um_io *conn, VALUE target, VALUE mode) {
|
|
103
103
|
conn->working_buffer = NULL;
|
|
104
104
|
if (NIL_P(mode)) {
|
|
105
105
|
if (TYPE(target) == T_DATA)
|
|
106
|
-
|
|
106
|
+
io_set_target(conn, target, IO_SSL);
|
|
107
107
|
else
|
|
108
|
-
|
|
108
|
+
io_set_target(conn, target, IO_FD);
|
|
109
109
|
}
|
|
110
110
|
else if (mode == SYM_fd)
|
|
111
|
-
|
|
111
|
+
io_set_target(conn, target, IO_FD);
|
|
112
112
|
else if (mode == SYM_socket)
|
|
113
|
-
|
|
113
|
+
io_set_target(conn, target, IO_SOCKET);
|
|
114
114
|
else if (mode == SYM_ssl)
|
|
115
|
-
|
|
115
|
+
io_set_target(conn, target, IO_SSL);
|
|
116
116
|
else
|
|
117
117
|
rb_raise(eUMError, "Invalid connection mode");
|
|
118
118
|
}
|
|
@@ -131,18 +131,18 @@ static inline void connection_setup(struct um_connection *conn, VALUE target, VA
|
|
|
131
131
|
* @param mode [Symbol] optional connection mode: :fd, :socket, :ssl
|
|
132
132
|
* @return [void]
|
|
133
133
|
*/
|
|
134
|
-
VALUE
|
|
134
|
+
VALUE IO_initialize(int argc, VALUE *argv, VALUE self) {
|
|
135
135
|
VALUE machine;
|
|
136
136
|
VALUE target;
|
|
137
137
|
VALUE mode;
|
|
138
138
|
rb_scan_args(argc, argv, "21", &machine, &target, &mode);
|
|
139
139
|
|
|
140
|
-
struct
|
|
141
|
-
memset(conn, 0, sizeof(struct
|
|
140
|
+
struct um_io *conn = um_get_io(self);
|
|
141
|
+
memset(conn, 0, sizeof(struct um_io));
|
|
142
142
|
|
|
143
143
|
RB_OBJ_WRITE(self, &conn->self, self);
|
|
144
144
|
conn->machine = um_get_machine(machine);
|
|
145
|
-
|
|
145
|
+
io_setup(conn, target, mode);
|
|
146
146
|
|
|
147
147
|
return self;
|
|
148
148
|
}
|
|
@@ -154,12 +154,12 @@ VALUE Connection_initialize(int argc, VALUE *argv, VALUE self) {
|
|
|
154
154
|
*
|
|
155
155
|
* @return [Symbol] connection mode
|
|
156
156
|
*/
|
|
157
|
-
VALUE
|
|
158
|
-
struct
|
|
157
|
+
VALUE IO_mode(VALUE self) {
|
|
158
|
+
struct um_io *conn = um_get_io(self);
|
|
159
159
|
switch (conn->mode) {
|
|
160
|
-
case
|
|
161
|
-
case
|
|
162
|
-
case
|
|
160
|
+
case IO_FD: return SYM_fd;
|
|
161
|
+
case IO_SOCKET: return SYM_socket;
|
|
162
|
+
case IO_SSL: return SYM_ssl;
|
|
163
163
|
default: return Qnil;
|
|
164
164
|
}
|
|
165
165
|
return Qnil;
|
|
@@ -175,9 +175,9 @@ VALUE Connection_mode(VALUE self) {
|
|
|
175
175
|
* @param limit [integer] maximum line length (0 means no limit)
|
|
176
176
|
* @return [String, nil] read data or nil
|
|
177
177
|
*/
|
|
178
|
-
VALUE
|
|
179
|
-
struct
|
|
180
|
-
return
|
|
178
|
+
VALUE IO_read_line(VALUE self, VALUE limit) {
|
|
179
|
+
struct um_io *conn = um_get_io(self);
|
|
180
|
+
return io_read_line(conn, Qnil, NUM2ULONG(limit));
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
/* call-seq:
|
|
@@ -190,9 +190,9 @@ VALUE Connection_read_line(VALUE self, VALUE limit) {
|
|
|
190
190
|
* @param len [integer] number of bytes to read
|
|
191
191
|
* @return [String, nil] read data or nil
|
|
192
192
|
*/
|
|
193
|
-
VALUE
|
|
194
|
-
struct
|
|
195
|
-
return
|
|
193
|
+
VALUE IO_read(VALUE self, VALUE len) {
|
|
194
|
+
struct um_io *conn = um_get_io(self);
|
|
195
|
+
return io_read(conn, Qnil, NUM2LONG(len), 0, false);
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
/* call-seq:
|
|
@@ -210,9 +210,9 @@ VALUE Connection_read(VALUE self, VALUE len) {
|
|
|
210
210
|
* @param delim [String] delimiter (single byte) @param limit [integer] maximum
|
|
211
211
|
* line length (0 means no limit) @return [String, nil] read data or nil
|
|
212
212
|
*/
|
|
213
|
-
VALUE
|
|
214
|
-
struct
|
|
215
|
-
return
|
|
213
|
+
VALUE IO_read_to_delim(VALUE self, VALUE delim, VALUE limit) {
|
|
214
|
+
struct um_io *conn = um_get_io(self);
|
|
215
|
+
return io_read_to_delim(conn, Qnil, delim, NUM2LONG(limit));
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
/* call-seq:
|
|
@@ -223,9 +223,9 @@ VALUE Connection_read_to_delim(VALUE self, VALUE delim, VALUE limit) {
|
|
|
223
223
|
* @param len [integer] number of bytes to skip
|
|
224
224
|
* @return [Integer] len
|
|
225
225
|
*/
|
|
226
|
-
VALUE
|
|
227
|
-
struct
|
|
228
|
-
|
|
226
|
+
VALUE IO_skip(VALUE self, VALUE len) {
|
|
227
|
+
struct um_io *conn = um_get_io(self);
|
|
228
|
+
io_skip(conn, NUM2LONG(len), true);
|
|
229
229
|
return len;
|
|
230
230
|
}
|
|
231
231
|
|
|
@@ -234,11 +234,11 @@ VALUE Connection_skip(VALUE self, VALUE len) {
|
|
|
234
234
|
*
|
|
235
235
|
* Reads from the target, passing each chunk to the given block.
|
|
236
236
|
*
|
|
237
|
-
* @return [UringMachine::
|
|
237
|
+
* @return [UringMachine::IO] conn
|
|
238
238
|
*/
|
|
239
|
-
VALUE
|
|
240
|
-
struct
|
|
241
|
-
|
|
239
|
+
VALUE IO_read_each(VALUE self) {
|
|
240
|
+
struct um_io *conn = um_get_io(self);
|
|
241
|
+
io_read_each(conn);
|
|
242
242
|
return self;
|
|
243
243
|
}
|
|
244
244
|
|
|
@@ -251,9 +251,9 @@ VALUE Connection_read_each(VALUE self) {
|
|
|
251
251
|
* @param bufs [Array<String, IO::Buffer>] data to write
|
|
252
252
|
* @return [Integer] total bytes written
|
|
253
253
|
*/
|
|
254
|
-
VALUE
|
|
255
|
-
struct
|
|
256
|
-
return
|
|
254
|
+
VALUE IO_write(int argc, VALUE *argv, VALUE self) {
|
|
255
|
+
struct um_io *conn = um_get_io(self);
|
|
256
|
+
return io_writev(conn, argc, argv);
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
/* call-seq:
|
|
@@ -263,8 +263,8 @@ VALUE Connection_write(int argc, VALUE *argv, VALUE self) {
|
|
|
263
263
|
*
|
|
264
264
|
* @return [any] decoded object
|
|
265
265
|
*/
|
|
266
|
-
VALUE
|
|
267
|
-
struct
|
|
266
|
+
VALUE IO_resp_read(VALUE self) {
|
|
267
|
+
struct um_io *conn = um_get_io(self);
|
|
268
268
|
VALUE out_buffer = rb_utf8_str_new_literal("");
|
|
269
269
|
VALUE obj = resp_read(conn, out_buffer);
|
|
270
270
|
RB_GC_GUARD(out_buffer);
|
|
@@ -280,8 +280,8 @@ VALUE Connection_resp_read(VALUE self) {
|
|
|
280
280
|
* @param obj [any] object to write
|
|
281
281
|
* @return [Integer] total bytes written
|
|
282
282
|
*/
|
|
283
|
-
VALUE
|
|
284
|
-
struct
|
|
283
|
+
VALUE IO_resp_write(VALUE self, VALUE obj) {
|
|
284
|
+
struct um_io *conn = um_get_io(self);
|
|
285
285
|
|
|
286
286
|
VALUE str = rb_str_new(NULL, 0);
|
|
287
287
|
struct um_write_buffer buf;
|
|
@@ -290,7 +290,7 @@ VALUE Connection_resp_write(VALUE self, VALUE obj) {
|
|
|
290
290
|
resp_encode(&buf, obj);
|
|
291
291
|
write_buffer_update_len(&buf);
|
|
292
292
|
|
|
293
|
-
size_t len =
|
|
293
|
+
size_t len = io_write_raw(conn, buf.ptr, buf.len);
|
|
294
294
|
RB_GC_GUARD(str);
|
|
295
295
|
return ULONG2NUM(len);
|
|
296
296
|
}
|
|
@@ -304,7 +304,7 @@ VALUE Connection_resp_write(VALUE self, VALUE obj) {
|
|
|
304
304
|
* @param obj [any] object to be encoded
|
|
305
305
|
* @return [String] str
|
|
306
306
|
*/
|
|
307
|
-
VALUE
|
|
307
|
+
VALUE IO_resp_encode(VALUE self, VALUE str, VALUE obj) {
|
|
308
308
|
struct um_write_buffer buf;
|
|
309
309
|
write_buffer_init(&buf, str);
|
|
310
310
|
rb_str_modify(str);
|
|
@@ -320,8 +320,8 @@ VALUE Connection_resp_encode(VALUE self, VALUE str, VALUE obj) {
|
|
|
320
320
|
*
|
|
321
321
|
* @return [bool] EOF reached
|
|
322
322
|
*/
|
|
323
|
-
VALUE
|
|
324
|
-
struct
|
|
323
|
+
VALUE IO_eof_p(VALUE self) {
|
|
324
|
+
struct um_io *conn = um_get_io(self);
|
|
325
325
|
return conn->eof ? Qtrue : Qfalse;
|
|
326
326
|
}
|
|
327
327
|
|
|
@@ -332,8 +332,8 @@ VALUE Connection_eof_p(VALUE self) {
|
|
|
332
332
|
*
|
|
333
333
|
* @return [Integer] total bytes consumed
|
|
334
334
|
*/
|
|
335
|
-
VALUE
|
|
336
|
-
struct
|
|
335
|
+
VALUE IO_consumed(VALUE self) {
|
|
336
|
+
struct um_io *conn = um_get_io(self);
|
|
337
337
|
return LONG2NUM(conn->consumed_bytes);
|
|
338
338
|
}
|
|
339
339
|
|
|
@@ -344,8 +344,8 @@ VALUE Connection_consumed(VALUE self) {
|
|
|
344
344
|
*
|
|
345
345
|
* @return [Integer] bytes available
|
|
346
346
|
*/
|
|
347
|
-
VALUE
|
|
348
|
-
struct
|
|
347
|
+
VALUE IO_pending(VALUE self) {
|
|
348
|
+
struct um_io *conn = um_get_io(self);
|
|
349
349
|
return LONG2NUM(conn->pending_bytes);
|
|
350
350
|
}
|
|
351
351
|
|
|
@@ -356,37 +356,37 @@ VALUE Connection_pending(VALUE self) {
|
|
|
356
356
|
*
|
|
357
357
|
* @return [UM::Stream] self
|
|
358
358
|
*/
|
|
359
|
-
VALUE
|
|
360
|
-
struct
|
|
361
|
-
|
|
359
|
+
VALUE IO_clear(VALUE self) {
|
|
360
|
+
struct um_io *conn = um_get_io(self);
|
|
361
|
+
io_clear(conn);
|
|
362
362
|
return self;
|
|
363
363
|
}
|
|
364
364
|
|
|
365
|
-
void
|
|
366
|
-
|
|
367
|
-
rb_define_alloc_func(
|
|
365
|
+
void Init_IO(void) {
|
|
366
|
+
cIO = rb_define_class_under(cUM, "IO", rb_cObject);
|
|
367
|
+
rb_define_alloc_func(cIO, IO_allocate);
|
|
368
368
|
|
|
369
|
-
rb_define_method(
|
|
370
|
-
rb_define_method(
|
|
369
|
+
rb_define_method(cIO, "initialize", IO_initialize, -1);
|
|
370
|
+
rb_define_method(cIO, "mode", IO_mode, 0);
|
|
371
371
|
|
|
372
|
-
rb_define_method(
|
|
373
|
-
rb_define_method(
|
|
374
|
-
rb_define_method(
|
|
375
|
-
rb_define_method(
|
|
376
|
-
rb_define_method(
|
|
372
|
+
rb_define_method(cIO, "read_line", IO_read_line, 1);
|
|
373
|
+
rb_define_method(cIO, "read", IO_read, 1);
|
|
374
|
+
rb_define_method(cIO, "read_to_delim", IO_read_to_delim, 2);
|
|
375
|
+
rb_define_method(cIO, "skip", IO_skip, 1);
|
|
376
|
+
rb_define_method(cIO, "read_each", IO_read_each, 0);
|
|
377
377
|
|
|
378
|
-
rb_define_method(
|
|
378
|
+
rb_define_method(cIO, "write", IO_write, -1);
|
|
379
379
|
|
|
380
|
-
rb_define_method(
|
|
381
|
-
rb_define_method(
|
|
382
|
-
rb_define_singleton_method(
|
|
380
|
+
rb_define_method(cIO, "resp_read", IO_resp_read, 0);
|
|
381
|
+
rb_define_method(cIO, "resp_write", IO_resp_write, 1);
|
|
382
|
+
rb_define_singleton_method(cIO, "resp_encode", IO_resp_encode, 2);
|
|
383
383
|
|
|
384
|
-
rb_define_method(
|
|
385
|
-
rb_define_method(
|
|
386
|
-
rb_define_method(
|
|
387
|
-
rb_define_method(
|
|
384
|
+
rb_define_method(cIO, "eof?", IO_eof_p, 0);
|
|
385
|
+
rb_define_method(cIO, "consumed", IO_consumed, 0);
|
|
386
|
+
rb_define_method(cIO, "pending", IO_pending, 0);
|
|
387
|
+
rb_define_method(cIO, "clear", IO_clear, 0);
|
|
388
388
|
|
|
389
|
-
|
|
389
|
+
eIORESPError = rb_define_class_under(cIO, "RESPError", rb_eStandardError);
|
|
390
390
|
|
|
391
391
|
SYM_fd = ID2SYM(rb_intern("fd"));
|
|
392
392
|
SYM_socket = ID2SYM(rb_intern("socket"));
|
data/lib/uringmachine/version.rb
CHANGED
data/lib/uringmachine.rb
CHANGED
|
@@ -195,24 +195,24 @@ class UringMachine
|
|
|
195
195
|
end
|
|
196
196
|
|
|
197
197
|
# call-seq:
|
|
198
|
-
# machine.
|
|
199
|
-
# machine.
|
|
198
|
+
# machine.io(fd, mode = nil) -> conn
|
|
199
|
+
# machine.io(fd, mode = nil) { |conn| }
|
|
200
200
|
#
|
|
201
|
-
# Creates
|
|
202
|
-
#
|
|
201
|
+
# Creates an UM::IO for the given target (fd or SSLSocket). The mode indicates
|
|
202
|
+
# the type of target and how it is read from:
|
|
203
203
|
#
|
|
204
204
|
# - :fd - read from the given fd using the buffer pool (default mode)
|
|
205
205
|
# - :socket - receive from the given socket fd using the buffer pool
|
|
206
|
-
# - :ssl - read from the given SSL
|
|
206
|
+
# - :ssl - read from the given SSL socket
|
|
207
207
|
#
|
|
208
|
-
# If a block is given, the block will be called with the
|
|
209
|
-
# the method will return the block's return value.
|
|
208
|
+
# If a block is given, the block will be called with the IO instance as
|
|
209
|
+
# argument and the method will return the block's return value.
|
|
210
210
|
#
|
|
211
|
-
# @param target [Integer, OpenSSL::SSL::SSLSocket] fd or ssl
|
|
212
|
-
# @param mode [Symbol, nil]
|
|
213
|
-
# @return [UringMachine::
|
|
214
|
-
def
|
|
215
|
-
conn = UM::
|
|
211
|
+
# @param target [Integer, OpenSSL::SSL::SSLSocket] fd or ssl socket
|
|
212
|
+
# @param mode [Symbol, nil] IO mode
|
|
213
|
+
# @return [UringMachine::IO] IO object
|
|
214
|
+
def io(target, mode = nil)
|
|
215
|
+
conn = UM::IO.new(self, target, mode)
|
|
216
216
|
return conn if !block_given?
|
|
217
217
|
|
|
218
218
|
res = yield(conn)
|
|
@@ -5,13 +5,13 @@ require 'securerandom'
|
|
|
5
5
|
require 'openssl'
|
|
6
6
|
require 'localhost/authority'
|
|
7
7
|
|
|
8
|
-
class
|
|
8
|
+
class IOBaseTest < UMBaseTest
|
|
9
9
|
attr_reader :conn
|
|
10
10
|
|
|
11
11
|
def setup
|
|
12
12
|
super
|
|
13
13
|
@rfd, @wfd = UM.pipe
|
|
14
|
-
@conn = UM::
|
|
14
|
+
@conn = UM::IO.new(@machine, @rfd)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def teardown
|
|
@@ -22,7 +22,7 @@ class ConnectionBaseTest < UMBaseTest
|
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
class
|
|
25
|
+
class IOTest < IOBaseTest
|
|
26
26
|
def buffer_metrics
|
|
27
27
|
machine.metrics.fetch_values(
|
|
28
28
|
:buffers_allocated,
|
|
@@ -55,7 +55,7 @@ class ConnectionTest < ConnectionBaseTest
|
|
|
55
55
|
|
|
56
56
|
def test_connection_clear
|
|
57
57
|
rfd, wfd = UM.pipe
|
|
58
|
-
conn = UM::
|
|
58
|
+
conn = UM::IO.new(machine, rfd)
|
|
59
59
|
|
|
60
60
|
assert_equal [0, 0, 0, 0, 0], buffer_metrics
|
|
61
61
|
machine.write(wfd, "foobar")
|
|
@@ -79,7 +79,7 @@ class ConnectionTest < ConnectionBaseTest
|
|
|
79
79
|
|
|
80
80
|
def test_connection_big_read
|
|
81
81
|
s1, s2 = UM.socketpair(UM::AF_UNIX, UM::SOCK_STREAM, 0)
|
|
82
|
-
conn = UM::
|
|
82
|
+
conn = UM::IO.new(machine, s2)
|
|
83
83
|
|
|
84
84
|
msg = '1234567' * 20000
|
|
85
85
|
|
|
@@ -98,7 +98,7 @@ class ConnectionTest < ConnectionBaseTest
|
|
|
98
98
|
|
|
99
99
|
def test_connection_buffer_reuse
|
|
100
100
|
s1, s2 = UM.socketpair(UM::AF_UNIX, UM::SOCK_STREAM, 0)
|
|
101
|
-
conn = UM::
|
|
101
|
+
conn = UM::IO.new(machine, s2)
|
|
102
102
|
|
|
103
103
|
msg = '1234567' * 20000
|
|
104
104
|
|
|
@@ -307,13 +307,13 @@ class ConnectionTest < ConnectionBaseTest
|
|
|
307
307
|
end
|
|
308
308
|
end
|
|
309
309
|
|
|
310
|
-
class
|
|
310
|
+
class IOWriteTest < UMBaseTest
|
|
311
311
|
attr_reader :conn
|
|
312
312
|
|
|
313
313
|
def setup
|
|
314
314
|
super
|
|
315
315
|
@s1, @s2 = UM.socketpair(UM::AF_UNIX, UM::SOCK_STREAM, 0)
|
|
316
|
-
@conn = UM::
|
|
316
|
+
@conn = UM::IO.new(@machine, @s1)
|
|
317
317
|
end
|
|
318
318
|
|
|
319
319
|
def teardown
|
|
@@ -340,7 +340,7 @@ class ConnectionWriteTest < UMBaseTest
|
|
|
340
340
|
end
|
|
341
341
|
|
|
342
342
|
def test_connection_write_socket_mode
|
|
343
|
-
conn = machine.
|
|
343
|
+
conn = machine.io(@s2, :socket)
|
|
344
344
|
|
|
345
345
|
assert_equal 6, conn.write('foo', 'bar')
|
|
346
346
|
|
|
@@ -363,8 +363,8 @@ class ConnectionWriteTest < UMBaseTest
|
|
|
363
363
|
ssl2.connect
|
|
364
364
|
refute_equal 0, @machine.metrics[:total_ops]
|
|
365
365
|
|
|
366
|
-
conn1 = machine.
|
|
367
|
-
conn2 = machine.
|
|
366
|
+
conn1 = machine.io(ssl1)
|
|
367
|
+
conn2 = machine.io(ssl2)
|
|
368
368
|
|
|
369
369
|
assert_equal 10, conn1.write('foobar', "\n", 'baz')
|
|
370
370
|
|
|
@@ -379,7 +379,7 @@ class ConnectionWriteTest < UMBaseTest
|
|
|
379
379
|
end
|
|
380
380
|
end
|
|
381
381
|
|
|
382
|
-
class
|
|
382
|
+
class IORespTest < IOBaseTest
|
|
383
383
|
def test_connection_resp_read
|
|
384
384
|
machine.write(@wfd, "+foo bar\r\n")
|
|
385
385
|
assert_equal "foo bar", conn.resp_read
|
|
@@ -389,12 +389,12 @@ class ConnectionRespTest < ConnectionBaseTest
|
|
|
389
389
|
|
|
390
390
|
machine.write(@wfd, "-foobar\r\n")
|
|
391
391
|
o = conn.resp_read
|
|
392
|
-
assert_kind_of UM::
|
|
392
|
+
assert_kind_of UM::IO::RESPError, o
|
|
393
393
|
assert_equal "foobar", o.message
|
|
394
394
|
|
|
395
395
|
machine.write(@wfd, "!3\r\nbaz\r\n")
|
|
396
396
|
o = conn.resp_read
|
|
397
|
-
assert_kind_of UM::
|
|
397
|
+
assert_kind_of UM::IO::RESPError, o
|
|
398
398
|
assert_equal "baz", o.message
|
|
399
399
|
|
|
400
400
|
machine.write(@wfd, ":123\r\n")
|
|
@@ -459,7 +459,7 @@ class ConnectionRespTest < ConnectionBaseTest
|
|
|
459
459
|
end
|
|
460
460
|
|
|
461
461
|
def test_connection_resp_write
|
|
462
|
-
writer = machine.
|
|
462
|
+
writer = machine.io(@wfd)
|
|
463
463
|
|
|
464
464
|
writer.resp_write(nil);
|
|
465
465
|
assert_equal "_\r\n", conn.read(-100)
|
|
@@ -490,7 +490,7 @@ class ConnectionRespTest < ConnectionBaseTest
|
|
|
490
490
|
end
|
|
491
491
|
|
|
492
492
|
def test_connection_resp_encode
|
|
493
|
-
s = UM::
|
|
493
|
+
s = UM::IO
|
|
494
494
|
assert_equal "_\r\n", s.resp_encode(+'', nil)
|
|
495
495
|
assert_equal "#t\r\n", s.resp_encode(+'', true)
|
|
496
496
|
assert_equal "#f\r\n", s.resp_encode(+'', false)
|
|
@@ -507,7 +507,7 @@ class ConnectionRespTest < ConnectionBaseTest
|
|
|
507
507
|
end
|
|
508
508
|
end
|
|
509
509
|
|
|
510
|
-
class
|
|
510
|
+
class IOStressTest < UMBaseTest
|
|
511
511
|
def setup
|
|
512
512
|
super
|
|
513
513
|
|
|
@@ -525,7 +525,7 @@ class ConnectionStressTest < UMBaseTest
|
|
|
525
525
|
|
|
526
526
|
def start_connection_fiber(fd)
|
|
527
527
|
machine.spin do
|
|
528
|
-
conn = UM::
|
|
528
|
+
conn = UM::IO.new(machine, fd)
|
|
529
529
|
while (msg = conn.read_line(0))
|
|
530
530
|
@received << msg
|
|
531
531
|
end
|
|
@@ -620,10 +620,10 @@ class ConnectionStressTest < UMBaseTest
|
|
|
620
620
|
end
|
|
621
621
|
end
|
|
622
622
|
|
|
623
|
-
class
|
|
623
|
+
class IODevRandomTest < UMBaseTest
|
|
624
624
|
def test_connection_dev_random_read_line
|
|
625
625
|
fd = machine.open('/dev/random', UM::O_RDONLY)
|
|
626
|
-
conn = UM::
|
|
626
|
+
conn = UM::IO.new(machine, fd)
|
|
627
627
|
|
|
628
628
|
n = 100000
|
|
629
629
|
lines = []
|
|
@@ -639,7 +639,7 @@ class ConnectionDevRandomTest < UMBaseTest
|
|
|
639
639
|
|
|
640
640
|
def read_line_do(n, acc)
|
|
641
641
|
fd = @machine.open('/dev/random', UM::O_RDONLY)
|
|
642
|
-
conn = UM::
|
|
642
|
+
conn = UM::IO.new(@machine, fd)
|
|
643
643
|
n.times { acc << conn.read_line(0) }
|
|
644
644
|
end
|
|
645
645
|
|
|
@@ -656,7 +656,7 @@ class ConnectionDevRandomTest < UMBaseTest
|
|
|
656
656
|
|
|
657
657
|
def test_connection_dev_random_read
|
|
658
658
|
fd = machine.open('/dev/random', UM::O_RDONLY)
|
|
659
|
-
conn = UM::
|
|
659
|
+
conn = UM::IO.new(machine, fd)
|
|
660
660
|
|
|
661
661
|
n = 256
|
|
662
662
|
size = 65536 * 8
|
|
@@ -676,10 +676,10 @@ class ConnectionDevRandomTest < UMBaseTest
|
|
|
676
676
|
end
|
|
677
677
|
end
|
|
678
678
|
|
|
679
|
-
class
|
|
679
|
+
class IOModeTest < UMBaseTest
|
|
680
680
|
def test_connection_default_mode
|
|
681
681
|
r, w = UM.pipe
|
|
682
|
-
conn = UM::
|
|
682
|
+
conn = UM::IO.new(machine, r)
|
|
683
683
|
assert_equal :fd, conn.mode
|
|
684
684
|
ensure
|
|
685
685
|
machine.close(r) rescue nil
|
|
@@ -692,7 +692,7 @@ class ConnectionModeTest < UMBaseTest
|
|
|
692
692
|
sock1, sock2 = UNIXSocket.pair
|
|
693
693
|
|
|
694
694
|
s1 = OpenSSL::SSL::SSLSocket.new(sock1, @server_ctx)
|
|
695
|
-
conn = UM::
|
|
695
|
+
conn = UM::IO.new(machine, s1)
|
|
696
696
|
assert_equal :ssl, conn.mode
|
|
697
697
|
ensure
|
|
698
698
|
sock1&.close rescue nil
|
|
@@ -704,7 +704,7 @@ class ConnectionModeTest < UMBaseTest
|
|
|
704
704
|
machine.write(w, 'foobar')
|
|
705
705
|
machine.close(w)
|
|
706
706
|
|
|
707
|
-
conn = UM::
|
|
707
|
+
conn = UM::IO.new(machine, r, :socket)
|
|
708
708
|
assert_equal :socket, conn.mode
|
|
709
709
|
# assert :socket, conn.mode
|
|
710
710
|
assert_raises(Errno::ENOTSOCK) { conn.read(0) }
|
|
@@ -718,7 +718,7 @@ class ConnectionModeTest < UMBaseTest
|
|
|
718
718
|
machine.write(w, 'foobar')
|
|
719
719
|
machine.close(w)
|
|
720
720
|
|
|
721
|
-
conn = UM::
|
|
721
|
+
conn = UM::IO.new(machine, r, :socket)
|
|
722
722
|
assert_equal :socket, conn.mode
|
|
723
723
|
buf = conn.read(0)
|
|
724
724
|
assert_equal 'foobar', buf
|
|
@@ -751,7 +751,7 @@ class ConnectionModeTest < UMBaseTest
|
|
|
751
751
|
assert_equal 10, @machine.ssl_write(s1, buf, buf.bytesize)
|
|
752
752
|
buf = +''
|
|
753
753
|
|
|
754
|
-
conn = UM::
|
|
754
|
+
conn = UM::IO.new(machine, s2, :ssl)
|
|
755
755
|
assert_equal "foobar", conn.read_line(0)
|
|
756
756
|
|
|
757
757
|
buf = "buh"
|
|
@@ -774,7 +774,7 @@ class ConnectionModeTest < UMBaseTest
|
|
|
774
774
|
end
|
|
775
775
|
end
|
|
776
776
|
|
|
777
|
-
class
|
|
777
|
+
class IOByteCountsTest < IOBaseTest
|
|
778
778
|
def test_connection_byte_counts
|
|
779
779
|
machine.write(@wfd, "foobar")
|
|
780
780
|
|