robin 0.0.4 → 0.1.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.
Files changed (225) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +372 -0
  3. data/COPYING +674 -0
  4. data/LICENSE +165 -0
  5. data/README.md +1490 -0
  6. data/assets/favicon.svg +31 -0
  7. data/assets/robin-logo.svg +37 -0
  8. data/assets/robin-mono.svg +12 -0
  9. data/assets/robin-wordmark.svg +4 -0
  10. data/assets/robin.svg +34 -0
  11. data/exe/robin +5 -0
  12. data/ext/robin_resp/extconf.rb +29 -0
  13. data/ext/robin_resp/robin_resp.c +645 -0
  14. data/lib/active_job/queue_adapters/robin_adapter.rb +3 -0
  15. data/lib/generators/robin/install/install_generator.rb +39 -0
  16. data/lib/generators/robin/install/templates/initializer.rb.tt +31 -0
  17. data/lib/generators/robin/install/templates/robin.yml.tt +25 -0
  18. data/lib/generators/robin/job/job_generator.rb +78 -0
  19. data/lib/generators/robin/job/templates/job.rb.tt +13 -0
  20. data/lib/generators/robin/job/templates/job_spec.rb.tt +21 -0
  21. data/lib/generators/robin/job/templates/job_test.rb.tt +21 -0
  22. data/lib/puma/plugin/robin.rb +131 -0
  23. data/lib/robin/active_job.rb +144 -0
  24. data/lib/robin/api.rb +777 -0
  25. data/lib/robin/batch.rb +700 -0
  26. data/lib/robin/cancellation.rb +62 -0
  27. data/lib/robin/cli.rb +325 -0
  28. data/lib/robin/client.rb +355 -21
  29. data/lib/robin/config.rb +454 -4
  30. data/lib/robin/cron.rb +325 -0
  31. data/lib/robin/debounce.rb +108 -0
  32. data/lib/robin/deploy.rb +65 -0
  33. data/lib/robin/embedded.rb +53 -0
  34. data/lib/robin/encryption.rb +147 -0
  35. data/lib/robin/events.rb +60 -0
  36. data/lib/robin/expiry.rb +41 -0
  37. data/lib/robin/fetcher.rb +441 -0
  38. data/lib/robin/gvl.rb +62 -0
  39. data/lib/robin/health.rb +76 -0
  40. data/lib/robin/iterable_job.rb +294 -0
  41. data/lib/robin/job.rb +258 -0
  42. data/lib/robin/job_class.rb +157 -0
  43. data/lib/robin/job_logger.rb +121 -0
  44. data/lib/robin/launcher.rb +658 -0
  45. data/lib/robin/leader.rb +86 -0
  46. data/lib/robin/limiter.rb +639 -0
  47. data/lib/robin/manager.rb +125 -0
  48. data/lib/robin/metrics.rb +303 -0
  49. data/lib/robin/middleware.rb +165 -0
  50. data/lib/robin/opentelemetry.rb +88 -0
  51. data/lib/robin/periodic.rb +327 -0
  52. data/lib/robin/processor.rb +317 -0
  53. data/lib/robin/profiler.rb +140 -0
  54. data/lib/robin/progress.rb +46 -0
  55. data/lib/robin/rails/app_loader.rb +23 -0
  56. data/lib/robin/rails/current_attributes.rb +88 -0
  57. data/lib/robin/rails/error_reporter.rb +31 -0
  58. data/lib/robin/rails/notifications.rb +22 -0
  59. data/lib/robin/rails/railtie.rb +87 -0
  60. data/lib/robin/rails/transactional_push.rb +49 -0
  61. data/lib/robin/rails.rb +23 -0
  62. data/lib/robin/recovery.rb +135 -0
  63. data/lib/robin/redis/connection.rb +639 -0
  64. data/lib/robin/redis/pool.rb +190 -0
  65. data/lib/robin/redis/resp.rb +77 -0
  66. data/lib/robin/reliable_push.rb +153 -0
  67. data/lib/robin/retrier.rb +284 -0
  68. data/lib/robin/scheduler.rb +102 -0
  69. data/lib/robin/scripts.rb +331 -0
  70. data/lib/robin/statsd.rb +135 -0
  71. data/lib/robin/swarm.rb +319 -0
  72. data/lib/robin/systemd.rb +47 -0
  73. data/lib/robin/testing.rb +158 -0
  74. data/lib/robin/top.rb +178 -0
  75. data/lib/robin/unique.rb +198 -0
  76. data/lib/robin/version.rb +5 -0
  77. data/lib/robin/warmup.rb +94 -0
  78. data/lib/robin/web/api.rb +580 -0
  79. data/lib/robin/web/batch_status.rb +44 -0
  80. data/lib/robin/web/dist/assets/ar-y7v9b75f.js +1 -0
  81. data/lib/robin/web/dist/assets/ar-y7v9b75f.js.gz +0 -0
  82. data/lib/robin/web/dist/assets/bricolage-0qmzcpnj.woff2 +0 -0
  83. data/lib/robin/web/dist/assets/cs-df88mq5e.js +1 -0
  84. data/lib/robin/web/dist/assets/cs-df88mq5e.js.gz +0 -0
  85. data/lib/robin/web/dist/assets/da-kemms74g.js +1 -0
  86. data/lib/robin/web/dist/assets/da-kemms74g.js.gz +0 -0
  87. data/lib/robin/web/dist/assets/de-z8kkqg65.js +1 -0
  88. data/lib/robin/web/dist/assets/de-z8kkqg65.js.gz +0 -0
  89. data/lib/robin/web/dist/assets/es-9ahrrn1s.js +1 -0
  90. data/lib/robin/web/dist/assets/es-9ahrrn1s.js.gz +0 -0
  91. data/lib/robin/web/dist/assets/fa-7r0x28nc.js +1 -0
  92. data/lib/robin/web/dist/assets/fa-7r0x28nc.js.gz +0 -0
  93. data/lib/robin/web/dist/assets/favicon-1ebfcc0182.svg +31 -0
  94. data/lib/robin/web/dist/assets/favicon-1ebfcc0182.svg.gz +0 -0
  95. data/lib/robin/web/dist/assets/fr-r7mqf3d9.js +1 -0
  96. data/lib/robin/web/dist/assets/fr-r7mqf3d9.js.gz +0 -0
  97. data/lib/robin/web/dist/assets/he-7t46b6rt.js +1 -0
  98. data/lib/robin/web/dist/assets/he-7t46b6rt.js.gz +0 -0
  99. data/lib/robin/web/dist/assets/hi-zj434tx4.js +1 -0
  100. data/lib/robin/web/dist/assets/hi-zj434tx4.js.gz +0 -0
  101. data/lib/robin/web/dist/assets/it-pqc2m7gm.js +1 -0
  102. data/lib/robin/web/dist/assets/it-pqc2m7gm.js.gz +0 -0
  103. data/lib/robin/web/dist/assets/ja-k9rgm1pj.js +1 -0
  104. data/lib/robin/web/dist/assets/ja-k9rgm1pj.js.gz +0 -0
  105. data/lib/robin/web/dist/assets/ko-bxersv91.js +1 -0
  106. data/lib/robin/web/dist/assets/ko-bxersv91.js.gz +0 -0
  107. data/lib/robin/web/dist/assets/main-anw3f6ba.css +1 -0
  108. data/lib/robin/web/dist/assets/main-anw3f6ba.css.gz +0 -0
  109. data/lib/robin/web/dist/assets/main-pdtvjejb.js +7 -0
  110. data/lib/robin/web/dist/assets/main-pdtvjejb.js.gz +0 -0
  111. data/lib/robin/web/dist/assets/main-zz8zzhwn.js +2 -0
  112. data/lib/robin/web/dist/assets/main-zz8zzhwn.js.gz +0 -0
  113. data/lib/robin/web/dist/assets/nb-fjnc5064.js +1 -0
  114. data/lib/robin/web/dist/assets/nb-fjnc5064.js.gz +0 -0
  115. data/lib/robin/web/dist/assets/nl-9p8b95b6.js +1 -0
  116. data/lib/robin/web/dist/assets/nl-9p8b95b6.js.gz +0 -0
  117. data/lib/robin/web/dist/assets/pl-8xsrdn8n.js +1 -0
  118. data/lib/robin/web/dist/assets/pl-8xsrdn8n.js.gz +0 -0
  119. data/lib/robin/web/dist/assets/pt-BR-650j7cvg.js +1 -0
  120. data/lib/robin/web/dist/assets/pt-BR-650j7cvg.js.gz +0 -0
  121. data/lib/robin/web/dist/assets/robin-ae4a4aedd9.svg +34 -0
  122. data/lib/robin/web/dist/assets/robin-ae4a4aedd9.svg.gz +0 -0
  123. data/lib/robin/web/dist/assets/robin-wordmark-5dde5d8e4b.svg +4 -0
  124. data/lib/robin/web/dist/assets/robin-wordmark-5dde5d8e4b.svg.gz +0 -0
  125. data/lib/robin/web/dist/assets/ru-a9ftyw3d.js +1 -0
  126. data/lib/robin/web/dist/assets/ru-a9ftyw3d.js.gz +0 -0
  127. data/lib/robin/web/dist/assets/sv-j9gqamr1.js +1 -0
  128. data/lib/robin/web/dist/assets/sv-j9gqamr1.js.gz +0 -0
  129. data/lib/robin/web/dist/assets/tr-y9z0q51g.js +1 -0
  130. data/lib/robin/web/dist/assets/tr-y9z0q51g.js.gz +0 -0
  131. data/lib/robin/web/dist/assets/uk-2dr9ew3p.js +1 -0
  132. data/lib/robin/web/dist/assets/uk-2dr9ew3p.js.gz +0 -0
  133. data/lib/robin/web/dist/assets/ur-h25nzq1s.js +1 -0
  134. data/lib/robin/web/dist/assets/ur-h25nzq1s.js.gz +0 -0
  135. data/lib/robin/web/dist/assets/vi-9y2ff1bd.js +1 -0
  136. data/lib/robin/web/dist/assets/vi-9y2ff1bd.js.gz +0 -0
  137. data/lib/robin/web/dist/assets/zh-CN-tbj9mf98.js +1 -0
  138. data/lib/robin/web/dist/assets/zh-CN-tbj9mf98.js.gz +0 -0
  139. data/lib/robin/web/dist/assets/zh-TW-59q8j9vw.js +1 -0
  140. data/lib/robin/web/dist/assets/zh-TW-59q8j9vw.js.gz +0 -0
  141. data/lib/robin/web/dist/index.html +19 -0
  142. data/lib/robin/web/extension.rb +193 -0
  143. data/lib/robin/web/permissions.rb +40 -0
  144. data/lib/robin/web/prometheus.rb +79 -0
  145. data/lib/robin/web/router.rb +150 -0
  146. data/lib/robin/web/static.rb +110 -0
  147. data/lib/robin/web.rb +196 -0
  148. data/lib/robin.rb +258 -8
  149. data/sig/manifest.yaml +9 -0
  150. data/sig/robin/api.rbs +442 -0
  151. data/sig/robin/batch.rbs +292 -0
  152. data/sig/robin/cancellation.rbs +25 -0
  153. data/sig/robin/cli.rbs +102 -0
  154. data/sig/robin/client.rbs +118 -0
  155. data/sig/robin/config.rbs +341 -0
  156. data/sig/robin/cron.rbs +149 -0
  157. data/sig/robin/debounce.rbs +36 -0
  158. data/sig/robin/deploy.rbs +24 -0
  159. data/sig/robin/embedded.rbs +25 -0
  160. data/sig/robin/encryption.rbs +49 -0
  161. data/sig/robin/events.rbs +10 -0
  162. data/sig/robin/expiry.rbs +17 -0
  163. data/sig/robin/fetcher.rbs +134 -0
  164. data/sig/robin/gvl.rbs +39 -0
  165. data/sig/robin/health.rbs +30 -0
  166. data/sig/robin/iterable_job.rbs +99 -0
  167. data/sig/robin/job.rbs +173 -0
  168. data/sig/robin/job_class.rbs +55 -0
  169. data/sig/robin/job_logger.rbs +56 -0
  170. data/sig/robin/launcher.rbs +191 -0
  171. data/sig/robin/leader.rbs +43 -0
  172. data/sig/robin/limiter.rbs +317 -0
  173. data/sig/robin/manager.rbs +54 -0
  174. data/sig/robin/metrics.rbs +122 -0
  175. data/sig/robin/middleware.rbs +106 -0
  176. data/sig/robin/opentelemetry.rbs +51 -0
  177. data/sig/robin/periodic.rbs +81 -0
  178. data/sig/robin/processor.rbs +107 -0
  179. data/sig/robin/profiler.rbs +60 -0
  180. data/sig/robin/progress.rbs +22 -0
  181. data/sig/robin/rails/app_loader.rbs +10 -0
  182. data/sig/robin/rails/current_attributes.rbs +44 -0
  183. data/sig/robin/rails/error_reporter.rbs +21 -0
  184. data/sig/robin/rails/notifications.rbs +9 -0
  185. data/sig/robin/rails/transactional_push.rbs +16 -0
  186. data/sig/robin/rails.rbs +8 -0
  187. data/sig/robin/recovery.rbs +29 -0
  188. data/sig/robin/redis/connection.rbs +224 -0
  189. data/sig/robin/redis/pool.rbs +89 -0
  190. data/sig/robin/redis/resp.rbs +29 -0
  191. data/sig/robin/reliable_push.rbs +77 -0
  192. data/sig/robin/retrier.rbs +72 -0
  193. data/sig/robin/scheduler.rbs +40 -0
  194. data/sig/robin/scripts.rbs +36 -0
  195. data/sig/robin/statsd.rbs +55 -0
  196. data/sig/robin/swarm.rbs +133 -0
  197. data/sig/robin/systemd.rbs +13 -0
  198. data/sig/robin/testing.rbs +80 -0
  199. data/sig/robin/top.rbs +79 -0
  200. data/sig/robin/unique.rbs +72 -0
  201. data/sig/robin/version.rbs +3 -0
  202. data/sig/robin/warmup.rbs +44 -0
  203. data/sig/robin/web/api.rbs +196 -0
  204. data/sig/robin/web/batch_status.rbs +25 -0
  205. data/sig/robin/web/extension.rbs +106 -0
  206. data/sig/robin/web/permissions.rbs +14 -0
  207. data/sig/robin/web/prometheus.rbs +33 -0
  208. data/sig/robin/web/router.rbs +53 -0
  209. data/sig/robin/web/static.rbs +42 -0
  210. data/sig/robin/web.rbs +140 -0
  211. data/sig/robin.rbs +116 -0
  212. data/sig/robin_resp.rbs +55 -0
  213. metadata +274 -44
  214. data/bin/robin +0 -5
  215. data/lib/robin/actions/authenticate.rb +0 -38
  216. data/lib/robin/actions/empty_tweet.rb +0 -7
  217. data/lib/robin/actions/followers.rb +0 -22
  218. data/lib/robin/actions/help.rb +0 -16
  219. data/lib/robin/actions/home_timeline.rb +0 -9
  220. data/lib/robin/actions/new_tweet.rb +0 -11
  221. data/lib/robin/actions/unknown_flag.rb +0 -11
  222. data/lib/robin/actions/user_timeline.rb +0 -19
  223. data/lib/robin/args_parser.rb +0 -70
  224. data/lib/robin/pin_authenticator.rb +0 -31
  225. data/lib/robin/user.rb +0 -36
@@ -0,0 +1,645 @@
1
+ /*
2
+ * robin_resp.c - native RESP (Redis protocol) reply parser and socket
3
+ * reader for Robin.
4
+ *
5
+ * Copyright (C) Robin contributors. Licensed under the LGPL-3.0.
6
+ *
7
+ * Defines Robin::Redis::NativeRESP.parse(buf, pos), a drop-in replacement
8
+ * for the pure-Ruby Robin::Redis::RESP.parse (RESP2) with identical results
9
+ * for well-formed replies:
10
+ *
11
+ * - returns [value, next_pos] when buf holds a complete reply at pos;
12
+ * - returns nil when more bytes are needed;
13
+ * - error replies become Robin::Redis::CommandError instances (returned,
14
+ * not raised);
15
+ * - strings are tagged UTF-8;
16
+ * - an unknown type byte or a malformed integer line raises
17
+ * Robin::Redis::ProtocolError.
18
+ *
19
+ * Also defines Robin::Redis::NativeRESP.encode(args, out = "".b), the
20
+ * counterpart of Robin::Redis::RESP.encode, and, where poll(2) exists,
21
+ * Robin::Redis::NativeIO (below). Robin::NativeGVL (at the end) measures
22
+ * how long threads wait for the GVL, on Rubies with thread event hooks.
23
+ */
24
+
25
+ #include <ruby.h>
26
+ #include <ruby/encoding.h>
27
+ #include <ruby/thread.h>
28
+ #include <string.h>
29
+ #ifdef HAVE_POLL_H
30
+ #include <ruby/io.h>
31
+ #include <errno.h>
32
+ #include <limits.h>
33
+ #include <poll.h>
34
+ #include <sys/socket.h>
35
+ #include <unistd.h>
36
+ #endif
37
+ #include <stdint.h>
38
+ #include <time.h>
39
+
40
+ #define ROBIN_MAX_DEPTH 512
41
+
42
+ /* Decimal digits that always fit in a long long. */
43
+ #define ROBIN_FAST_DIGITS 18
44
+
45
+ /* Sentinel meaning "incomplete": never a value the parser produces. */
46
+ #define INCOMPLETE Qundef
47
+
48
+ static VALUE cCommandError = Qnil;
49
+ static VALUE cProtocolError = Qnil;
50
+ static ID id_new;
51
+ static ID id_to_s;
52
+
53
+ /* Ruby wraps its string constructors in macros that special-case
54
+ * compile-time constants; the parentheses call the functions directly and
55
+ * avoid branches that can never run here. */
56
+ static VALUE utf8_string(const char *ptr, long len) {
57
+ return (rb_utf8_str_new)(ptr, len); // NOLINT(readability-redundant-parentheses)
58
+ }
59
+
60
+ static VALUE binary_string(const char *ptr, long len) {
61
+ return (rb_str_new)(ptr, len); // NOLINT(readability-redundant-parentheses)
62
+ }
63
+
64
+ typedef struct {
65
+ const char *ptr;
66
+ long len;
67
+ int depth;
68
+ } parser_t;
69
+
70
+ static VALUE lookup_class(VALUE *cache, const char *path) {
71
+ if (NIL_P(*cache)) {
72
+ *cache = rb_path2class(path);
73
+ rb_gc_register_mark_object(*cache);
74
+ }
75
+ return *cache;
76
+ }
77
+
78
+ NORETURN(static void protocol_error(const char *message));
79
+ static void protocol_error(const char *message) {
80
+ rb_raise(lookup_class(&cProtocolError, "Robin::Redis::ProtocolError"), "%s", message);
81
+ }
82
+
83
+ /* Index of the first "\r\n" at or after pos, or -1. */
84
+ static long find_crlf(const parser_t *p, long pos) {
85
+ const char *start = p->ptr + pos;
86
+ const char *end = p->ptr + p->len;
87
+
88
+ while (start < end) {
89
+ const char *cr = memchr(start, '\r', (size_t)(end - start));
90
+ if (cr == NULL || cr + 1 == end) {
91
+ return -1;
92
+ }
93
+ if (cr[1] == '\n') {
94
+ return (long)(cr - p->ptr);
95
+ }
96
+ start = cr + 1;
97
+ }
98
+ return -1;
99
+ }
100
+
101
+ /* The Integer for a line of the form -?[0-9]+; anything else is a protocol
102
+ * error. Short numbers are converted directly, long ones by Ruby. */
103
+ static VALUE line_to_integer(const char *s, long n) {
104
+ long first = (n > 0 && s[0] == '-') ? 1 : 0;
105
+ long long value = 0;
106
+
107
+ if (first == n) {
108
+ protocol_error("invalid integer in RESP reply");
109
+ }
110
+ for (long i = first; i < n; i++) {
111
+ if (s[i] < '0' || s[i] > '9') {
112
+ protocol_error("invalid integer in RESP reply");
113
+ }
114
+ }
115
+ if (n - first > ROBIN_FAST_DIGITS) {
116
+ return rb_str_to_inum(binary_string(s, n), 10, Qtrue);
117
+ }
118
+ for (long i = first; i < n; i++) {
119
+ value = (value * 10) + (s[i] - '0');
120
+ }
121
+ return LL2NUM(first ? -value : value);
122
+ }
123
+
124
+ /* A length or count. Too large to fit a long means the reply can't be
125
+ * complete yet (it would need more memory than exists): returns 0. Lengths
126
+ * below zero all mean "null", so huge negatives become -1. */
127
+ static int line_to_length(const char *s, long n, long *out) {
128
+ VALUE num = line_to_integer(s, n);
129
+
130
+ if (FIXNUM_P(num)) {
131
+ *out = FIX2LONG(num);
132
+ return 1;
133
+ }
134
+ if (rb_big_sign(num)) {
135
+ return 0;
136
+ }
137
+ *out = -1;
138
+ return 1;
139
+ }
140
+
141
+ static VALUE parse_at(parser_t *p, long pos, long *next);
142
+
143
+ /* Recursion is bounded by ROBIN_MAX_DEPTH. */
144
+ static VALUE parse_array(parser_t *p, long pos, long count, long *next) { // NOLINT(misc-no-recursion)
145
+ VALUE items = Qnil;
146
+
147
+ /* Each element needs at least 3 bytes (":0\r\n" is 4); a count larger
148
+ * than what's left can't be complete yet. Avoids huge allocations. */
149
+ if (count > (p->len - pos) / 3) {
150
+ return INCOMPLETE;
151
+ }
152
+ if (p->depth == ROBIN_MAX_DEPTH) {
153
+ protocol_error("RESP reply nested too deeply");
154
+ }
155
+ p->depth++;
156
+ items = rb_ary_new_capa(count);
157
+ for (long i = 0; i < count; i++) {
158
+ VALUE item = parse_at(p, pos, &pos);
159
+ if (item == INCOMPLETE) {
160
+ p->depth--;
161
+ return INCOMPLETE;
162
+ }
163
+ rb_ary_push(items, item);
164
+ }
165
+ p->depth--;
166
+ *next = pos;
167
+ return items;
168
+ }
169
+
170
+ static VALUE parse_at(parser_t *p, long pos, long *next) { // NOLINT(misc-no-recursion)
171
+ long eol = find_crlf(p, pos);
172
+ long size = 0;
173
+
174
+ if (eol < 0) {
175
+ return INCOMPLETE;
176
+ }
177
+
178
+ const char *line = p->ptr + pos + 1;
179
+ long line_len = eol - pos - 1;
180
+ long body = eol + 2;
181
+
182
+ switch ((unsigned char)p->ptr[pos]) {
183
+ case '+':
184
+ *next = body;
185
+ return utf8_string(line, line_len);
186
+
187
+ case '-':
188
+ *next = body;
189
+ return rb_funcall(lookup_class(&cCommandError, "Robin::Redis::CommandError"), id_new, 1,
190
+ utf8_string(line, line_len));
191
+
192
+ case ':':
193
+ *next = body;
194
+ return line_to_integer(line, line_len);
195
+
196
+ case '$':
197
+ if (!line_to_length(line, line_len, &size)) {
198
+ return INCOMPLETE;
199
+ }
200
+ if (size < 0) {
201
+ *next = body;
202
+ return Qnil;
203
+ }
204
+ if (size > p->len - body - 2) {
205
+ return INCOMPLETE;
206
+ }
207
+ *next = body + size + 2;
208
+ return utf8_string(p->ptr + body, size);
209
+
210
+ case '*':
211
+ if (!line_to_length(line, line_len, &size)) {
212
+ return INCOMPLETE;
213
+ }
214
+ if (size < 0) {
215
+ *next = body;
216
+ return Qnil;
217
+ }
218
+ return parse_array(p, body, size, next);
219
+
220
+ default:
221
+ protocol_error("unknown RESP type byte");
222
+ }
223
+ }
224
+
225
+ /*
226
+ * call-seq:
227
+ * Robin::Redis::NativeRESP.parse(buf, pos) -> [value, next_pos] or nil
228
+ */
229
+ static VALUE native_parse(VALUE self, VALUE buf, VALUE vpos) {
230
+ long pos = NUM2LONG(vpos);
231
+ long next = 0;
232
+ parser_t p;
233
+ VALUE value = Qnil;
234
+
235
+ (void)self;
236
+ StringValue(buf);
237
+ if (pos < 0) {
238
+ rb_raise(rb_eArgError, "negative offset");
239
+ }
240
+ p.ptr = RSTRING_PTR(buf);
241
+ p.len = RSTRING_LEN(buf);
242
+ p.depth = 0;
243
+
244
+ value = parse_at(&p, pos, &next);
245
+ RB_GC_GUARD(buf); // NOLINT(portability-no-assembler) -- Ruby's GC guard idiom
246
+ if (value == INCOMPLETE) {
247
+ return Qnil;
248
+ }
249
+ return rb_assoc_new(value, LONG2NUM(next));
250
+ }
251
+
252
+ /* Appends a type byte, a length or count, and CRLF. */
253
+ static void append_header(VALUE out, char type, long number) {
254
+ char header[32];
255
+ int len = snprintf(header, sizeof(header), "%c%ld\r\n", type, number);
256
+
257
+ (rb_str_cat)(out, header, len); // NOLINT(readability-redundant-parentheses)
258
+ }
259
+
260
+ /* Appends one bulk string: arg's bytes when it's a String, its digits when
261
+ * it's a small Integer, else the bytes of its to_s. */
262
+ static void append_argument(VALUE out, VALUE arg) {
263
+ char digits[24];
264
+
265
+ if (FIXNUM_P(arg)) {
266
+ int len = snprintf(digits, sizeof(digits), "%ld", FIX2LONG(arg));
267
+ append_header(out, '$', len);
268
+ (rb_str_cat)(out, digits, len); // NOLINT(readability-redundant-parentheses)
269
+ } else {
270
+ if (RB_SPECIAL_CONST_P(arg) || RB_BUILTIN_TYPE(arg) != T_STRING) {
271
+ arg = rb_funcall(arg, id_to_s, 0);
272
+ StringValue(arg);
273
+ }
274
+ append_header(out, '$', RSTRING_LEN(arg));
275
+ (rb_str_cat)(out, RSTRING_PTR(arg), RSTRING_LEN(arg)); // NOLINT(readability-redundant-parentheses)
276
+ RB_GC_GUARD(arg); // NOLINT(portability-no-assembler) -- Ruby's GC guard idiom
277
+ }
278
+ (rb_str_cat)(out, "\r\n", 2); // NOLINT(readability-redundant-parentheses)
279
+ }
280
+
281
+ /*
282
+ * call-seq:
283
+ * Robin::Redis::NativeRESP.encode(args, out = "".b) -> out
284
+ *
285
+ * Appends the command +args+ to +out+, byte for byte what
286
+ * Robin::Redis::RESP.encode appends.
287
+ */
288
+ static VALUE native_encode(int argc, VALUE *argv, VALUE self) {
289
+ VALUE args = Qnil;
290
+ VALUE out = Qnil;
291
+
292
+ (void)self;
293
+ rb_scan_args(argc, argv, "11", &args, &out);
294
+ Check_Type(args, T_ARRAY);
295
+ if (NIL_P(out)) {
296
+ out = binary_string("", 0);
297
+ }
298
+ StringValue(out);
299
+ append_header(out, '*', RARRAY_LEN(args));
300
+ for (long i = 0; i < RARRAY_LEN(args); i++) {
301
+ append_argument(out, RARRAY_AREF(args, i));
302
+ }
303
+ return out;
304
+ }
305
+
306
+ #ifdef HAVE_POLL_H
307
+ /*
308
+ * Robin::Redis::NativeIO: reads replies off a socket. Waiting for a reply
309
+ * and reading it are one release of the GVL (Ruby's IO releases and
310
+ * retakes it around each syscall), which matters with many worker threads.
311
+ */
312
+
313
+ /* Bytes per read: plenty for a typical reply; longer ones take more reads. */
314
+ #define ROBIN_READ_CHUNK 16384
315
+
316
+ /* read_t.status when nothing arrived in time (errno values are positive). */
317
+ #define ROBIN_TIMED_OUT (-1)
318
+
319
+ typedef struct {
320
+ int fd;
321
+ int timeout_ms;
322
+ ssize_t count;
323
+ int status;
324
+ char data[ROBIN_READ_CHUNK];
325
+ } read_t;
326
+
327
+ /* Runs without the GVL: waits until the socket is readable, then reads. */
328
+ static void *read_without_gvl(void *arg) {
329
+ read_t *r = arg;
330
+ struct pollfd pfd = {.fd = r->fd, .events = POLLIN, .revents = 0};
331
+ int ready = poll(&pfd, 1, r->timeout_ms);
332
+
333
+ if (ready <= 0) {
334
+ r->status = ready == 0 ? ROBIN_TIMED_OUT : errno;
335
+ return NULL;
336
+ }
337
+ r->count = read(r->fd, r->data, sizeof(r->data));
338
+ r->status = r->count < 0 ? errno : 0;
339
+ return NULL;
340
+ }
341
+
342
+ /* Seconds to whole milliseconds, rounded up (so a short wait isn't none). */
343
+ static int to_timeout_ms(VALUE timeout) {
344
+ double ms = NUM2DBL(timeout) * 1000.0;
345
+
346
+ if (ms <= 0) {
347
+ return 0;
348
+ }
349
+ if (ms >= (double)INT_MAX) {
350
+ return INT_MAX;
351
+ }
352
+ int whole = (int)ms;
353
+ return whole < ms ? whole + 1 : whole;
354
+ }
355
+
356
+ /*
357
+ * call-seq:
358
+ * Robin::Redis::NativeIO.fill(io, buffer, timeout) -> Integer, nil or false
359
+ *
360
+ * Appends what arrives on +io+ within +timeout+ seconds to +buffer+ and
361
+ * returns how many bytes that was; nil when the server closed the
362
+ * connection, false when nothing arrived in time. A failed read raises
363
+ * SystemCallError. Thread#raise and Thread#kill interrupt the wait; other
364
+ * interruptions restart it (with the full timeout).
365
+ */
366
+ static VALUE native_fill(VALUE self, VALUE io, VALUE buffer, VALUE timeout) {
367
+ read_t r;
368
+
369
+ (void)self;
370
+ StringValue(buffer);
371
+ r.fd = rb_io_descriptor(io);
372
+ r.timeout_ms = to_timeout_ms(timeout);
373
+ do {
374
+ r.count = 0;
375
+ r.status = 0;
376
+ /* Raises here if Thread#raise or #kill interrupted the wait. */
377
+ rb_thread_call_without_gvl(read_without_gvl, &r, RUBY_UBF_IO, NULL);
378
+ } while (r.status == EINTR); /* interrupted for nothing (Thread#wakeup, a signal): wait again */
379
+ if (r.status == ROBIN_TIMED_OUT) {
380
+ return Qfalse;
381
+ }
382
+ if (r.status != 0) {
383
+ rb_syserr_fail(r.status, "reading from Redis");
384
+ }
385
+ if (r.count == 0) {
386
+ return Qnil;
387
+ }
388
+ (rb_str_cat)(buffer, r.data, r.count); // NOLINT(readability-redundant-parentheses)
389
+ return LONG2NUM(r.count);
390
+ }
391
+
392
+ /* roundtrip_t.status values besides 0 and errno values (positive). */
393
+ #define ROBIN_STALE (-2)
394
+ #define ROBIN_WRITE_TIMED_OUT (-3)
395
+
396
+ /* What a round trip has done so far: interrupted (EINTR), it resumes. */
397
+ typedef struct {
398
+ int fd;
399
+ const char *data;
400
+ long length;
401
+ long written;
402
+ int write_timeout_ms;
403
+ read_t read;
404
+ int status;
405
+ } roundtrip_t;
406
+
407
+ /* Whether the connection can't be used: closed, failed, or holding bytes
408
+ * nothing asked for. Peeks without reading or waiting. */
409
+ static int is_stale(int fd) {
410
+ char byte = 0;
411
+ ssize_t peeked = recv(fd, &byte, 1, MSG_PEEK | MSG_DONTWAIT);
412
+
413
+ /* EAGAIN (the same value as EWOULDBLOCK here): nothing to read, as it
414
+ * should be. */
415
+ return !(peeked < 0 && errno == EAGAIN);
416
+ }
417
+
418
+ /* Runs without the GVL: checks the connection (before anything is
419
+ * written), writes the commands, then waits for the replies and reads
420
+ * what has arrived. */
421
+ static void *roundtrip_without_gvl(void *arg) {
422
+ roundtrip_t *rt = arg;
423
+
424
+ if (rt->written == 0 && is_stale(rt->fd)) {
425
+ rt->status = ROBIN_STALE;
426
+ return NULL;
427
+ }
428
+ while (rt->written < rt->length) {
429
+ ssize_t n = write(rt->fd, rt->data + rt->written, (size_t)(rt->length - rt->written));
430
+ if (n >= 0) {
431
+ rt->written += n;
432
+ continue;
433
+ }
434
+ if (errno != EAGAIN) {
435
+ rt->status = errno;
436
+ return NULL;
437
+ }
438
+ struct pollfd pfd = {.fd = rt->fd, .events = POLLOUT, .revents = 0};
439
+ int ready = poll(&pfd, 1, rt->write_timeout_ms);
440
+ if (ready <= 0) {
441
+ rt->status = ready == 0 ? ROBIN_WRITE_TIMED_OUT : errno;
442
+ return NULL;
443
+ }
444
+ }
445
+ read_without_gvl(&rt->read);
446
+ rt->status = rt->read.status;
447
+ return NULL;
448
+ }
449
+
450
+ /* Runs the round trip, resuming it after interruptions for nothing
451
+ * (Thread#wakeup, a signal); raises if Thread#raise or #kill interrupted it. */
452
+ static VALUE run_roundtrip(VALUE arg) {
453
+ roundtrip_t *rt =
454
+ (roundtrip_t *)arg; // NOLINT(performance-no-int-to-ptr) -- rb_ensure passes it as a VALUE
455
+
456
+ do {
457
+ rt->read.count = 0;
458
+ rt->status = 0;
459
+ rb_thread_call_without_gvl(roundtrip_without_gvl, rt, RUBY_UBF_IO, NULL);
460
+ } while (rt->status == EINTR);
461
+ return Qnil;
462
+ }
463
+
464
+ /* Unlocks what native_roundtrip locked (nil: a frozen string, never locked). */
465
+ static VALUE unlock_string(VALUE str) {
466
+ return NIL_P(str) ? Qnil : rb_str_unlocktmp(str);
467
+ }
468
+
469
+ /*
470
+ * call-seq:
471
+ * Robin::Redis::NativeIO.roundtrip(io, data, buffer, write_timeout, read_timeout)
472
+ * -> Integer, nil, false, :stale or [written, errno]
473
+ *
474
+ * Sends +data+ (encoded commands) on +io+ and appends the first of the
475
+ * replies to +buffer+, all in one release of the GVL. Returns what
476
+ * NativeIO.fill would for the read; :stale, having sent nothing, when the
477
+ * connection can't be used (see NativeIO.stale?); or, when the write
478
+ * failed, [bytes written, errno], with a nil errno for a write that timed
479
+ * out. A failed read raises SystemCallError. Thread#raise and Thread#kill
480
+ * interrupt it, leaving the connection unusable; other interruptions
481
+ * resume it.
482
+ */
483
+ static VALUE native_roundtrip(VALUE self, VALUE io, VALUE data, VALUE buffer, VALUE write_timeout,
484
+ VALUE read_timeout) {
485
+ roundtrip_t rt;
486
+
487
+ (void)self;
488
+ StringValue(data);
489
+ StringValue(buffer);
490
+ rt.fd = rb_io_descriptor(io);
491
+ rt.length = RSTRING_LEN(data);
492
+ rt.written = 0;
493
+ rt.write_timeout_ms = to_timeout_ms(write_timeout);
494
+ rt.read.fd = rt.fd;
495
+ rt.read.timeout_ms = to_timeout_ms(read_timeout);
496
+ /*
497
+ * Nothing may change data while it's being written. A frozen string
498
+ * can't change (and Ruby 4.0 refuses to lock one); others are locked.
499
+ */
500
+ VALUE locked = OBJ_FROZEN(data) ? Qnil : rb_str_locktmp(data);
501
+ rt.data = RSTRING_PTR(data);
502
+ rb_ensure(run_roundtrip, (VALUE)&rt, unlock_string, locked);
503
+ RB_GC_GUARD(data);
504
+
505
+ if (rt.status == ROBIN_STALE) {
506
+ return ID2SYM(rb_intern("stale"));
507
+ }
508
+ if (rt.written < rt.length) {
509
+ return rb_assoc_new(LONG2NUM(rt.written),
510
+ rt.status == ROBIN_WRITE_TIMED_OUT ? Qnil : INT2NUM(rt.status));
511
+ }
512
+ if (rt.status == ROBIN_TIMED_OUT) {
513
+ return Qfalse;
514
+ }
515
+ if (rt.status != 0) {
516
+ rb_syserr_fail(rt.status, "reading from Redis");
517
+ }
518
+ if (rt.read.count == 0) {
519
+ return Qnil;
520
+ }
521
+ (rb_str_cat)(buffer, rt.read.data, rt.read.count); // NOLINT(readability-redundant-parentheses)
522
+ return LONG2NUM(rt.read.count);
523
+ }
524
+
525
+ /*
526
+ * call-seq:
527
+ * Robin::Redis::NativeIO.stale?(io) -> true or false
528
+ *
529
+ * Whether a connection between commands can't be used: the server closed
530
+ * it, it failed, or it holds bytes nothing asked for. Peeks without
531
+ * reading or waiting, so it's cheap enough to run before every command.
532
+ */
533
+ static VALUE native_stale_p(VALUE self, VALUE io) {
534
+ (void)self;
535
+ return is_stale(rb_io_descriptor(io)) ? Qtrue : Qfalse;
536
+ }
537
+ #endif
538
+
539
+ /*
540
+ * Robin::NativeGVL: the total time threads have spent waiting for the GVL.
541
+ * Ruby calls gvl_event when a thread starts waiting for the GVL (READY,
542
+ * without the GVL) and when it gets it (RESUMED, with it), possibly from
543
+ * several native threads at once, and not necessarily on the native
544
+ * thread that runs the Ruby thread: a thread woken by another (a new one,
545
+ * or one joining a thread that ends) is READY on the waker's. So the
546
+ * start of a wait is kept with the Ruby thread, in a slot of the
547
+ * thread-specific storage Ruby keeps for tools like this (in a native
548
+ * thread's variable, a woken thread's wait was counted from a READY long
549
+ * before), and each wait is added to one total atomically. The slot holds
550
+ * a pointer, so the start is kept in one only where a pointer has 64 bits
551
+ * (elsewhere a nanosecond timestamp wouldn't fit, and there's no
552
+ * Robin::NativeGVL: Robin::GVL measures nothing, as on JRuby). Nor is
553
+ * there one on Windows, where Ruby adds no thread event hooks.
554
+ */
555
+ #if UINTPTR_MAX >= UINT64_MAX && !defined(_WIN32)
556
+ #define ROBIN_NATIVE_GVL 1
557
+ static rb_internal_thread_specific_key_t gvl_ready_key;
558
+ static rb_internal_thread_event_hook_t *gvl_hook;
559
+ static uint64_t gvl_wait_ns;
560
+ static uint64_t gvl_started_at;
561
+
562
+ static uint64_t monotonic_ns(void) {
563
+ struct timespec now;
564
+ clock_gettime(CLOCK_MONOTONIC, &now);
565
+ return ((uint64_t)now.tv_sec * 1000000000U) + (uint64_t)now.tv_nsec;
566
+ }
567
+
568
+ static void gvl_event(rb_event_flag_t event, const rb_internal_thread_event_data_t *data, void *user_data) {
569
+ (void)user_data;
570
+ if (event == RUBY_INTERNAL_THREAD_EVENT_READY) {
571
+ void *ready_at = (void *)(uintptr_t)monotonic_ns(); // NOLINT(performance-no-int-to-ptr): a timestamp
572
+ rb_internal_thread_specific_set(data->thread, gvl_ready_key, ready_at);
573
+ return;
574
+ }
575
+ uint64_t ready_at = (uint64_t)(uintptr_t)rb_internal_thread_specific_get(data->thread, gvl_ready_key);
576
+ /*
577
+ * A thread already waiting when measuring started has no start since:
578
+ * its stamp is older (or none, or from before a stop, its wait's end
579
+ * unseen), and its wait isn't counted.
580
+ */
581
+ if (ready_at >= __atomic_load_n(&gvl_started_at, __ATOMIC_RELAXED)) {
582
+ __atomic_fetch_add(&gvl_wait_ns, monotonic_ns() - ready_at, __ATOMIC_RELAXED);
583
+ }
584
+ rb_internal_thread_specific_set(data->thread, gvl_ready_key, NULL);
585
+ }
586
+
587
+ /* Starts measuring (again); a no-op while measuring. */
588
+ static VALUE native_gvl_start(VALUE self) {
589
+ (void)self;
590
+ if (gvl_hook == NULL) {
591
+ __atomic_store_n(&gvl_started_at, monotonic_ns(), __ATOMIC_RELAXED);
592
+ gvl_hook = rb_internal_thread_add_event_hook(
593
+ gvl_event, RUBY_INTERNAL_THREAD_EVENT_READY | RUBY_INTERNAL_THREAD_EVENT_RESUMED, NULL);
594
+ }
595
+ return Qnil;
596
+ }
597
+
598
+ /* Stops measuring; the total stays. */
599
+ static VALUE native_gvl_stop(VALUE self) {
600
+ (void)self;
601
+ if (gvl_hook != NULL) {
602
+ rb_internal_thread_remove_event_hook(gvl_hook);
603
+ gvl_hook = NULL;
604
+ }
605
+ return Qnil;
606
+ }
607
+
608
+ static VALUE native_gvl_running_p(VALUE self) {
609
+ (void)self;
610
+ return gvl_hook != NULL ? Qtrue : Qfalse;
611
+ }
612
+
613
+ /* Nanoseconds all threads have waited for the GVL while measuring. */
614
+ static VALUE native_gvl_wait_ns(VALUE self) {
615
+ (void)self;
616
+ return ULL2NUM(__atomic_load_n(&gvl_wait_ns, __ATOMIC_RELAXED));
617
+ }
618
+ #endif
619
+
620
+ /* Called by Ruby's `require`, so it must have external linkage. */
621
+ void Init_robin_resp(void) { // NOLINT(misc-use-internal-linkage)
622
+ VALUE robin = rb_define_module("Robin");
623
+ VALUE redis = rb_define_module_under(robin, "Redis");
624
+ VALUE native = rb_define_module_under(redis, "NativeRESP");
625
+
626
+ rb_ext_ractor_safe(true);
627
+ id_new = rb_intern("new");
628
+ id_to_s = rb_intern("to_s");
629
+ rb_define_module_function(native, "parse", native_parse, 2);
630
+ rb_define_module_function(native, "encode", native_encode, -1);
631
+ #ifdef HAVE_POLL_H
632
+ VALUE native_io = rb_define_module_under(redis, "NativeIO");
633
+ rb_define_module_function(native_io, "fill", native_fill, 3);
634
+ rb_define_module_function(native_io, "stale?", native_stale_p, 1);
635
+ rb_define_module_function(native_io, "roundtrip", native_roundtrip, 5);
636
+ #endif
637
+ #ifdef ROBIN_NATIVE_GVL
638
+ VALUE native_gvl = rb_define_module_under(robin, "NativeGVL");
639
+ gvl_ready_key = rb_internal_thread_specific_key_create();
640
+ rb_define_module_function(native_gvl, "start", native_gvl_start, 0);
641
+ rb_define_module_function(native_gvl, "stop", native_gvl_stop, 0);
642
+ rb_define_module_function(native_gvl, "running?", native_gvl_running_p, 0);
643
+ rb_define_module_function(native_gvl, "wait_ns", native_gvl_wait_ns, 0);
644
+ #endif
645
+ }
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "robin/active_job"
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+
5
+ module Robin
6
+ module Generators
7
+ # rails generate robin:install
8
+ #
9
+ # Creates config/robin.yml and config/initializers/robin.rb, and mounts
10
+ # the dashboard in config/routes.rb. Running it again changes nothing.
11
+ class InstallGenerator < ::Rails::Generators::Base
12
+ ROUTE = <<~RUBY
13
+ # Robin's dashboard can see and change every job: protect it, e.g. with
14
+ # Robin::Web.authorize (config/initializers/robin.rb) or by mounting it
15
+ # inside an authenticated route constraint.
16
+ mount Robin::Web => "/robin"
17
+ RUBY
18
+
19
+ source_root File.expand_path("templates", __dir__)
20
+ desc "Creates Robin's config file and initializer, and mounts its dashboard."
21
+
22
+ def create_config_file
23
+ template "robin.yml", "config/robin.yml"
24
+ end
25
+
26
+ def create_initializer
27
+ template "initializer.rb", "config/initializers/robin.rb"
28
+ end
29
+
30
+ def mount_dashboard
31
+ routes = File.join(destination_root, "config", "routes.rb")
32
+ return unless File.exist?(routes)
33
+ return if File.read(routes).include?("Robin::Web")
34
+
35
+ route ROUTE
36
+ end
37
+ end
38
+ end
39
+ end