connectrpc-hyper 0.3.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 +7 -0
- data/Cargo.lock +927 -0
- data/Cargo.toml +8 -0
- data/LICENSE +21 -0
- data/README.md +475 -0
- data/THIRD_PARTY_NOTICES.md +3886 -0
- data/ext/connectrpc_hyper/Cargo.toml +31 -0
- data/ext/connectrpc_hyper/extconf.rb +9 -0
- data/ext/connectrpc_hyper/src/body.rs +142 -0
- data/ext/connectrpc_hyper/src/call.rs +659 -0
- data/ext/connectrpc_hyper/src/conn.rs +825 -0
- data/ext/connectrpc_hyper/src/failure.rs +247 -0
- data/ext/connectrpc_hyper/src/gvl.rs +414 -0
- data/ext/connectrpc_hyper/src/holds.rs +241 -0
- data/ext/connectrpc_hyper/src/lib.rs +652 -0
- data/ext/connectrpc_hyper/src/pool.rs +1206 -0
- data/ext/connectrpc_hyper/src/runtime.rs +352 -0
- data/ext/connectrpc_hyper/src/tls.rs +114 -0
- data/ext/connectrpc_hyper/src/unary.rs +120 -0
- data/ext/connectrpc_hyper/src/wire.rs +513 -0
- data/lib/connectrpc/hyper/errors.rb +79 -0
- data/lib/connectrpc/hyper/exchange.rb +89 -0
- data/lib/connectrpc/hyper/tls.rb +110 -0
- data/lib/connectrpc/hyper/transport.rb +369 -0
- data/lib/connectrpc/hyper/version.rb +7 -0
- data/lib/connectrpc/hyper.rb +27 -0
- data/sig/connectrpc-hyper.rbs +23 -0
- data/sig/manifest.yaml +3 -0
- metadata +107 -0
|
@@ -0,0 +1,652 @@
|
|
|
1
|
+
//! Native core of ConnectRPC::Hyper::Transport. Ruby owns protocol logic,
|
|
2
|
+
//! deadlines, cancellation and error phrases; this crate owns connections,
|
|
3
|
+
//! HTTP/2 streams and blocking waits. Every wait releases the GVL with an
|
|
4
|
+
//! unblocking function, and Rust never calls Ruby while the GVL is released.
|
|
5
|
+
mod body;
|
|
6
|
+
mod call;
|
|
7
|
+
mod conn;
|
|
8
|
+
mod failure;
|
|
9
|
+
mod gvl;
|
|
10
|
+
mod holds;
|
|
11
|
+
mod pool;
|
|
12
|
+
mod runtime;
|
|
13
|
+
mod tls;
|
|
14
|
+
mod unary;
|
|
15
|
+
mod wire;
|
|
16
|
+
|
|
17
|
+
use std::sync::Arc;
|
|
18
|
+
use std::time::{Duration, Instant};
|
|
19
|
+
|
|
20
|
+
use bytes::Bytes;
|
|
21
|
+
use magnus::value::{Lazy, ReprValue};
|
|
22
|
+
use magnus::{
|
|
23
|
+
Class, Error, ExceptionClass, RArray, RClass, RHash, RModule, RString, Ruby, Value, function,
|
|
24
|
+
method, prelude::*,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
use crate::call::CallState;
|
|
28
|
+
use crate::conn::{Origin, Settings, proxy};
|
|
29
|
+
use crate::failure::Failure;
|
|
30
|
+
use crate::gvl::{Signal, Waits, drive, wait_signal};
|
|
31
|
+
use crate::pool::{Config, DriverCore, RequestSpec, TimeoutHeader};
|
|
32
|
+
use crate::runtime::{ForkGuard, Tracker};
|
|
33
|
+
use crate::tls::TlsInputs;
|
|
34
|
+
|
|
35
|
+
static FAILURE: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
|
36
|
+
ruby.class_object()
|
|
37
|
+
.const_get::<_, RModule>("ConnectRPC")
|
|
38
|
+
.and_then(|m| m.const_get::<_, RModule>("Hyper"))
|
|
39
|
+
.and_then(|m| m.const_get::<_, RModule>("Native"))
|
|
40
|
+
.and_then(|m| m.const_get("Failure"))
|
|
41
|
+
.expect("ConnectRPC::Hyper::Native::Failure is defined")
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
fn failure_error(ruby: &Ruby, failure: Failure) -> Error {
|
|
45
|
+
let class = ruby.get_inner(&FAILURE);
|
|
46
|
+
let args = (
|
|
47
|
+
failure.detail,
|
|
48
|
+
ruby.to_symbol(failure.kind),
|
|
49
|
+
failure.reset,
|
|
50
|
+
failure.errno,
|
|
51
|
+
);
|
|
52
|
+
match class.new_instance(args) {
|
|
53
|
+
Ok(exception) => exception.into(),
|
|
54
|
+
Err(error) => error,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fn arg_error(ruby: &Ruby, message: &'static str) -> Error {
|
|
59
|
+
Error::new(ruby.exception_arg_error(), message)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fn settle<T>(ruby: &Ruby, result: Result<Result<T, Failure>, Error>) -> Result<T, Error> {
|
|
63
|
+
result?.map_err(|failure| failure_error(ruby, failure))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// The longest duration native code accepts from Ruby (about 115 days, the
|
|
67
|
+
/// largest `connect-timeout-ms` value). Ruby accepts any finite positive
|
|
68
|
+
/// number, and `Duration::from_secs_f64` or `Instant + Duration` panic for
|
|
69
|
+
/// values above about 1.8e19 s, so every Ruby-supplied duration saturates
|
|
70
|
+
/// here. The cap also keeps hyper's own `Instant + interval` keepalive
|
|
71
|
+
/// arithmetic far from overflow.
|
|
72
|
+
const MAX_SECONDS: f64 = 9_999_999.999;
|
|
73
|
+
|
|
74
|
+
/// A Ruby-supplied number of seconds as a duration, saturated to
|
|
75
|
+
/// `[0, MAX_SECONDS]`; never panics (NaN becomes zero).
|
|
76
|
+
fn seconds(value: f64) -> Duration {
|
|
77
|
+
Duration::try_from_secs_f64(value.clamp(0.0, MAX_SECONDS)).unwrap_or(Duration::ZERO)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn instant(seconds: Option<f64>) -> Option<Instant> {
|
|
81
|
+
seconds.map(|s| Instant::now() + self::seconds(s))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn bytes(value: RString) -> Bytes {
|
|
85
|
+
// SAFETY: the slice is copied before any Ruby code can run.
|
|
86
|
+
Bytes::copy_from_slice(unsafe { value.as_slice() })
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fn headers(
|
|
90
|
+
ruby: &Ruby,
|
|
91
|
+
pairs: RArray,
|
|
92
|
+
) -> Result<Vec<(http::HeaderName, http::HeaderValue)>, Error> {
|
|
93
|
+
let mut headers = Vec::with_capacity(pairs.len());
|
|
94
|
+
for pair in pairs.into_iter() {
|
|
95
|
+
let pair = RArray::try_convert(pair)?;
|
|
96
|
+
let name: RString = pair.entry(0)?;
|
|
97
|
+
let value: RString = pair.entry(1)?;
|
|
98
|
+
// SAFETY: both slices are copied before any Ruby code can run.
|
|
99
|
+
let name = http::HeaderName::from_bytes(unsafe { name.as_slice() })
|
|
100
|
+
.map_err(|_| arg_error(ruby, "Invalid request header name"))?;
|
|
101
|
+
let value = http::HeaderValue::from_bytes(unsafe { value.as_slice() })
|
|
102
|
+
.map_err(|_| arg_error(ruby, "Invalid request header value"))?;
|
|
103
|
+
headers.push((name, value));
|
|
104
|
+
}
|
|
105
|
+
Ok(headers)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
fn header_hash(ruby: &Ruby, map: &http::HeaderMap) -> Result<RHash, Error> {
|
|
109
|
+
let hash = ruby.hash_new_capa(map.keys_len());
|
|
110
|
+
for name in map.keys() {
|
|
111
|
+
let values = ruby.ary_new();
|
|
112
|
+
for value in map.get_all(name) {
|
|
113
|
+
values.push(ruby.str_from_slice(value.as_bytes()))?;
|
|
114
|
+
}
|
|
115
|
+
hash.aset(ruby.str_new(name.as_str()), values)?;
|
|
116
|
+
}
|
|
117
|
+
Ok(hash)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fn timeout_header(kind: Option<magnus::Symbol>) -> Result<Option<TimeoutHeader>, Error> {
|
|
121
|
+
let Some(kind) = kind else { return Ok(None) };
|
|
122
|
+
Ok(match kind.name()?.as_ref() {
|
|
123
|
+
"grpc" => Some(TimeoutHeader::Grpc),
|
|
124
|
+
_ => Some(TimeoutHeader::Connect),
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// A proxy endpoint Ruby resolved: [host, port, Proxy-Authorization or nil].
|
|
129
|
+
type ProxyArg = Option<(String, u16, Option<RString>)>;
|
|
130
|
+
|
|
131
|
+
fn proxy_route(
|
|
132
|
+
ruby: &Ruby,
|
|
133
|
+
proxy: ProxyArg,
|
|
134
|
+
https: bool,
|
|
135
|
+
http2: bool,
|
|
136
|
+
) -> Result<Option<proxy::Route>, Error> {
|
|
137
|
+
let Some((host, port, authorization)) = proxy else {
|
|
138
|
+
return Ok(None);
|
|
139
|
+
};
|
|
140
|
+
let authorization = authorization
|
|
141
|
+
.map(|value| {
|
|
142
|
+
// SAFETY: the slice is copied before any Ruby code can run.
|
|
143
|
+
http::HeaderValue::from_bytes(unsafe { value.as_slice() })
|
|
144
|
+
.map(|mut value| {
|
|
145
|
+
value.set_sensitive(true);
|
|
146
|
+
value
|
|
147
|
+
})
|
|
148
|
+
.map_err(|_| arg_error(ruby, "Invalid proxy configuration"))
|
|
149
|
+
})
|
|
150
|
+
.transpose()?;
|
|
151
|
+
Ok(Some(proxy::Route {
|
|
152
|
+
host: host
|
|
153
|
+
.trim_start_matches('[')
|
|
154
|
+
.trim_end_matches(']')
|
|
155
|
+
.to_ascii_lowercase(),
|
|
156
|
+
port,
|
|
157
|
+
authorization,
|
|
158
|
+
mode: proxy::mode(https, http2),
|
|
159
|
+
}))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
#[allow(clippy::too_many_arguments)]
|
|
163
|
+
fn request_spec(
|
|
164
|
+
ruby: &Ruby,
|
|
165
|
+
method: &str,
|
|
166
|
+
url: &str,
|
|
167
|
+
pairs: RArray,
|
|
168
|
+
kind: Option<magnus::Symbol>,
|
|
169
|
+
grpc: bool,
|
|
170
|
+
proxy: ProxyArg,
|
|
171
|
+
http2: bool,
|
|
172
|
+
) -> Result<RequestSpec, Error> {
|
|
173
|
+
let uri: http::Uri = url
|
|
174
|
+
.parse()
|
|
175
|
+
.map_err(|_| arg_error(ruby, "Invalid request URL"))?;
|
|
176
|
+
let mut origin = Origin::parse(&uri).map_err(|_| arg_error(ruby, "Invalid request URL"))?;
|
|
177
|
+
origin.proxy = proxy_route(ruby, proxy, origin.https, http2)?;
|
|
178
|
+
let method = http::Method::from_bytes(method.as_bytes())
|
|
179
|
+
.map_err(|_| arg_error(ruby, "Invalid request method"))?;
|
|
180
|
+
Ok(RequestSpec {
|
|
181
|
+
method,
|
|
182
|
+
uri,
|
|
183
|
+
origin,
|
|
184
|
+
headers: headers(ruby, pairs)?,
|
|
185
|
+
timeout_header: timeout_header(kind)?,
|
|
186
|
+
grpc,
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/// A cancellation handle registered with the call's token before the first
|
|
191
|
+
/// native wait. `fire` is safe from any Ruby thread. A token created before
|
|
192
|
+
/// fork and cancelled in the child does nothing: its Notify lock may have
|
|
193
|
+
/// been held by a parent runtime thread at fork time.
|
|
194
|
+
#[magnus::wrap(class = "ConnectRPC::Hyper::Native::Cancel", free_immediately, size)]
|
|
195
|
+
struct Cancel {
|
|
196
|
+
signal: ForkGuard<Arc<Signal>>,
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
impl Cancel {
|
|
200
|
+
fn new() -> Self {
|
|
201
|
+
Self {
|
|
202
|
+
signal: ForkGuard::new(Signal::new()),
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
fn fire(&self) {
|
|
207
|
+
if !self.signal.inherited() {
|
|
208
|
+
self.signal.fire();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/// For specs: whether this process fired the token.
|
|
213
|
+
fn is_fired(&self) -> bool {
|
|
214
|
+
!self.signal.inherited() && self.signal.fired()
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
#[magnus::wrap(class = "ConnectRPC::Hyper::Native::Driver", free_immediately, size)]
|
|
219
|
+
struct Driver {
|
|
220
|
+
core: ForkGuard<Arc<DriverCore>>,
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
impl Driver {
|
|
224
|
+
#[allow(clippy::too_many_arguments)]
|
|
225
|
+
fn new(
|
|
226
|
+
ruby: &Ruby,
|
|
227
|
+
pooled: bool,
|
|
228
|
+
http2: bool,
|
|
229
|
+
idle_timeout: Option<f64>,
|
|
230
|
+
connect_timeout: f64,
|
|
231
|
+
keepalive: Option<(f64, f64, bool)>,
|
|
232
|
+
roots: RArray,
|
|
233
|
+
chain: Option<RArray>,
|
|
234
|
+
key: Option<RString>,
|
|
235
|
+
tunnel: (f64, usize),
|
|
236
|
+
) -> Result<Self, Error> {
|
|
237
|
+
let ders = |list: RArray| -> Result<Vec<Vec<u8>>, Error> {
|
|
238
|
+
(0..list.len())
|
|
239
|
+
.map(|index| {
|
|
240
|
+
list.entry::<RString>(index as isize)
|
|
241
|
+
.map(|der| bytes(der).to_vec())
|
|
242
|
+
})
|
|
243
|
+
.collect()
|
|
244
|
+
};
|
|
245
|
+
let chain = chain.map(ders).transpose()?;
|
|
246
|
+
let tls = TlsInputs::new(ders(roots)?, chain, key.map(|der| bytes(der).to_vec()))
|
|
247
|
+
.map_err(|input| failure_error(ruby, Failure::new("tls_config", input)))?;
|
|
248
|
+
runtime::sockets::install();
|
|
249
|
+
let config = Config {
|
|
250
|
+
pooled,
|
|
251
|
+
idle_timeout: idle_timeout.map(seconds),
|
|
252
|
+
connect_timeout: seconds(connect_timeout),
|
|
253
|
+
settings: Settings {
|
|
254
|
+
http2,
|
|
255
|
+
keepalive: keepalive.map(|(time, timeout, while_idle)| {
|
|
256
|
+
(seconds(time), seconds(timeout), while_idle)
|
|
257
|
+
}),
|
|
258
|
+
tunnel: proxy::Limits {
|
|
259
|
+
timeout: seconds(tunnel.0),
|
|
260
|
+
max_head: tunnel.1,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
tls,
|
|
264
|
+
};
|
|
265
|
+
// The tracker binds the runtime lazily: building a driver starts no
|
|
266
|
+
// thread and opens no socket.
|
|
267
|
+
Ok(Self {
|
|
268
|
+
core: ForkGuard::new(DriverCore::new(config, Tracker::new())),
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
fn owned(&self, ruby: &Ruby) -> Result<&Arc<DriverCore>, Error> {
|
|
273
|
+
if self.core.inherited() {
|
|
274
|
+
return Err(Error::new(
|
|
275
|
+
ruby.exception_runtime_error(),
|
|
276
|
+
"driver was created before fork",
|
|
277
|
+
));
|
|
278
|
+
}
|
|
279
|
+
Ok(&self.core)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/// Connect unary in one native operation: [status, headers, body].
|
|
283
|
+
#[allow(clippy::too_many_arguments)]
|
|
284
|
+
fn unary(
|
|
285
|
+
ruby: &Ruby,
|
|
286
|
+
rb_self: &Self,
|
|
287
|
+
method: String,
|
|
288
|
+
url: String,
|
|
289
|
+
pairs: RArray,
|
|
290
|
+
payload: Option<RString>,
|
|
291
|
+
limit: usize,
|
|
292
|
+
kind: Option<magnus::Symbol>,
|
|
293
|
+
timeout: Option<f64>,
|
|
294
|
+
cancel: &Cancel,
|
|
295
|
+
proxy: ProxyArg,
|
|
296
|
+
) -> Result<RArray, Error> {
|
|
297
|
+
let core = rb_self.owned(ruby)?.clone();
|
|
298
|
+
let http2 = core.config.settings.http2;
|
|
299
|
+
let spec = request_spec(ruby, &method, &url, pairs, kind, false, proxy, http2)?;
|
|
300
|
+
let payload = payload.map(bytes);
|
|
301
|
+
let deadline = instant(timeout);
|
|
302
|
+
let inflight = core
|
|
303
|
+
.admit()
|
|
304
|
+
.map_err(|failure| failure_error(ruby, failure))?;
|
|
305
|
+
let waits = Waits {
|
|
306
|
+
cancel: Some(&cancel.signal),
|
|
307
|
+
abort: None,
|
|
308
|
+
shutdown: &core.shutdown,
|
|
309
|
+
deadline,
|
|
310
|
+
};
|
|
311
|
+
let outcome = drive(
|
|
312
|
+
ruby,
|
|
313
|
+
&waits,
|
|
314
|
+
unary::unary(core.clone(), spec, payload, limit, deadline),
|
|
315
|
+
);
|
|
316
|
+
drop(inflight);
|
|
317
|
+
let response = settle(ruby, outcome)?;
|
|
318
|
+
Ok(ruby.ary_new_from_values(&[
|
|
319
|
+
ruby.into_value(response.status),
|
|
320
|
+
header_hash(ruby, &response.headers)?.as_value(),
|
|
321
|
+
ruby.str_from_slice(&response.body).as_value(),
|
|
322
|
+
]))
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/// Opens a streaming exchange without waiting; its first write or
|
|
326
|
+
/// response head connects and dispatches.
|
|
327
|
+
#[allow(clippy::too_many_arguments)]
|
|
328
|
+
fn open(
|
|
329
|
+
ruby: &Ruby,
|
|
330
|
+
rb_self: &Self,
|
|
331
|
+
url: String,
|
|
332
|
+
pairs: RArray,
|
|
333
|
+
kind: Option<magnus::Symbol>,
|
|
334
|
+
timeout: Option<f64>,
|
|
335
|
+
grpc: bool,
|
|
336
|
+
require_h2: bool,
|
|
337
|
+
cancel: &Cancel,
|
|
338
|
+
proxy: ProxyArg,
|
|
339
|
+
) -> Result<Call, Error> {
|
|
340
|
+
let core = rb_self.owned(ruby)?.clone();
|
|
341
|
+
let http2 = core.config.settings.http2;
|
|
342
|
+
let spec = request_spec(ruby, "POST", &url, pairs, kind, grpc, proxy, http2)?;
|
|
343
|
+
let inflight = core
|
|
344
|
+
.admit()
|
|
345
|
+
.map_err(|failure| failure_error(ruby, failure))?;
|
|
346
|
+
let state = CallState::new(
|
|
347
|
+
core,
|
|
348
|
+
spec,
|
|
349
|
+
require_h2,
|
|
350
|
+
instant(timeout),
|
|
351
|
+
cancel.signal.clone(),
|
|
352
|
+
inflight,
|
|
353
|
+
);
|
|
354
|
+
Ok(Call {
|
|
355
|
+
state: ForkGuard::new(state),
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/// Stops admission, lets in-flight calls finish within `grace`, then
|
|
360
|
+
/// aborts the rest and waits for every owned task to end. True when
|
|
361
|
+
/// everything drained in time. The first close starts teardown on the
|
|
362
|
+
/// runtime; every closer waits for its published outcome on the calling
|
|
363
|
+
/// thread, never offloaded (from a non-blocking fiber under a fiber
|
|
364
|
+
/// scheduler, Ruby calls this from a helper thread). An interrupt raised
|
|
365
|
+
/// meanwhile is held and raised after the outcome is published, so
|
|
366
|
+
/// `close_outcome` reports it.
|
|
367
|
+
fn close(ruby: &Ruby, rb_self: &Self, grace: f64) -> Result<bool, Error> {
|
|
368
|
+
if rb_self.core.inherited() {
|
|
369
|
+
return Ok(true);
|
|
370
|
+
}
|
|
371
|
+
let torn_down = rb_self.core.start_teardown(seconds(grace));
|
|
372
|
+
let raised = wait_signal(ruby, &torn_down);
|
|
373
|
+
let quiet = rb_self
|
|
374
|
+
.core
|
|
375
|
+
.close_outcome()
|
|
376
|
+
.expect("teardown publishes its outcome before it signals");
|
|
377
|
+
match raised {
|
|
378
|
+
Some(error) => Err(error),
|
|
379
|
+
None => Ok(quiet),
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/// What `close` did, or nil before teardown finished.
|
|
384
|
+
fn close_outcome(&self) -> Option<bool> {
|
|
385
|
+
if self.core.inherited() {
|
|
386
|
+
return None;
|
|
387
|
+
}
|
|
388
|
+
self.core.close_outcome()
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/// For specs: arms hold point `name`; each arrival parks for at most
|
|
392
|
+
/// `bound` seconds unless released first.
|
|
393
|
+
fn hold(ruby: &Ruby, rb_self: &Self, name: String, bound: f64) -> Result<(), Error> {
|
|
394
|
+
rb_self.owned(ruby)?.holds.arm(name, seconds(bound));
|
|
395
|
+
Ok(())
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/// For specs: releases and disarms hold point `name`.
|
|
399
|
+
fn release(ruby: &Ruby, rb_self: &Self, name: String) -> Result<(), Error> {
|
|
400
|
+
rb_self.owned(ruby)?.holds.release(&name);
|
|
401
|
+
Ok(())
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/// For specs: arrivals at armed hold point `name`.
|
|
405
|
+
fn reached(ruby: &Ruby, rb_self: &Self, name: String) -> Result<usize, Error> {
|
|
406
|
+
Ok(rb_self.owned(ruby)?.holds.reached(&name))
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
fn opened(&self) -> usize {
|
|
410
|
+
if self.core.inherited() {
|
|
411
|
+
0
|
|
412
|
+
} else {
|
|
413
|
+
self.core.opened()
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/// [connections dialed, calls in flight, owned tasks running, idle
|
|
418
|
+
/// HTTP/1.1 connections], for specs.
|
|
419
|
+
fn stats(&self) -> (usize, usize, usize, usize) {
|
|
420
|
+
if self.core.inherited() {
|
|
421
|
+
return (0, 0, 0, 0);
|
|
422
|
+
}
|
|
423
|
+
(
|
|
424
|
+
self.core.opened(),
|
|
425
|
+
self.core.inflight(),
|
|
426
|
+
self.core.tracker.len(),
|
|
427
|
+
self.core.idle_h1(),
|
|
428
|
+
)
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
#[magnus::wrap(class = "ConnectRPC::Hyper::Native::Call", free_immediately, size)]
|
|
433
|
+
struct Call {
|
|
434
|
+
state: ForkGuard<Arc<CallState>>,
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
impl Drop for Call {
|
|
438
|
+
fn drop(&mut self) {
|
|
439
|
+
if !self.state.inherited() {
|
|
440
|
+
self.state.abort();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
impl Call {
|
|
446
|
+
fn state(&self, ruby: &Ruby) -> Result<&Arc<CallState>, Error> {
|
|
447
|
+
if self.state.inherited() {
|
|
448
|
+
return Err(Error::new(
|
|
449
|
+
ruby.exception_runtime_error(),
|
|
450
|
+
"call inherited across fork",
|
|
451
|
+
));
|
|
452
|
+
}
|
|
453
|
+
Ok(&self.state)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
fn overlap(ruby: &Ruby) -> Error {
|
|
457
|
+
Error::new(
|
|
458
|
+
ruby.exception_runtime_error(),
|
|
459
|
+
"overlapping or finished stream operation",
|
|
460
|
+
)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
fn waits<'a>(state: &'a Arc<CallState>, deadline: Option<Instant>) -> Waits<'a> {
|
|
464
|
+
Waits {
|
|
465
|
+
cancel: Some(&state.cancel),
|
|
466
|
+
abort: Some(&state.abort),
|
|
467
|
+
shutdown: state.shutdown(),
|
|
468
|
+
deadline,
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
fn take_recv(ruby: &Ruby, state: &Arc<CallState>) -> Result<call::RecvHalf, Error> {
|
|
473
|
+
state
|
|
474
|
+
.recv
|
|
475
|
+
.lock()
|
|
476
|
+
.unwrap_or_else(|e| e.into_inner())
|
|
477
|
+
.take()
|
|
478
|
+
.ok_or_else(|| Self::overlap(ruby))
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
fn head(ruby: &Ruby, rb_self: &Self, timeout: Option<f64>) -> Result<RArray, Error> {
|
|
482
|
+
let state = rb_self.state(ruby)?;
|
|
483
|
+
let mut recv = Self::take_recv(ruby, state)?;
|
|
484
|
+
let waits = Self::waits(state, instant(timeout));
|
|
485
|
+
let outcome = drive(ruby, &waits, state.head(&mut recv));
|
|
486
|
+
let head = recv.head.as_ref().map(|head| {
|
|
487
|
+
(
|
|
488
|
+
head.status,
|
|
489
|
+
header_hash(ruby, &head.headers),
|
|
490
|
+
head.end_stream,
|
|
491
|
+
)
|
|
492
|
+
});
|
|
493
|
+
state.restore(&state.recv, recv);
|
|
494
|
+
settle(ruby, outcome).inspect_err(|_| state.mark_failed())?;
|
|
495
|
+
let (status, headers, end_stream) = head.expect("head after success");
|
|
496
|
+
Ok(ruby.ary_new_from_values(&[
|
|
497
|
+
ruby.into_value(status),
|
|
498
|
+
headers?.as_value(),
|
|
499
|
+
ruby.into_value(end_stream),
|
|
500
|
+
]))
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
fn read(
|
|
504
|
+
ruby: &Ruby,
|
|
505
|
+
rb_self: &Self,
|
|
506
|
+
max: usize,
|
|
507
|
+
timeout: Option<f64>,
|
|
508
|
+
) -> Result<Option<RString>, Error> {
|
|
509
|
+
let state = rb_self.state(ruby)?;
|
|
510
|
+
let mut recv = Self::take_recv(ruby, state)?;
|
|
511
|
+
let waits = Self::waits(state, instant(timeout));
|
|
512
|
+
let outcome = drive(ruby, &waits, state.read(&mut recv, max));
|
|
513
|
+
state.restore(&state.recv, recv);
|
|
514
|
+
let chunk = settle(ruby, outcome).inspect_err(|_| state.mark_failed())?;
|
|
515
|
+
Ok(chunk.map(|bytes| ruby.str_from_slice(&bytes)))
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
fn write(
|
|
519
|
+
ruby: &Ruby,
|
|
520
|
+
rb_self: &Self,
|
|
521
|
+
data: RString,
|
|
522
|
+
timeout: Option<f64>,
|
|
523
|
+
) -> Result<Value, Error> {
|
|
524
|
+
let state = rb_self.state(ruby)?;
|
|
525
|
+
let mut send = state
|
|
526
|
+
.send
|
|
527
|
+
.lock()
|
|
528
|
+
.unwrap_or_else(|e| e.into_inner())
|
|
529
|
+
.take()
|
|
530
|
+
.ok_or_else(|| Self::overlap(ruby))?;
|
|
531
|
+
let waits = Self::waits(state, instant(timeout));
|
|
532
|
+
let outcome = drive(ruby, &waits, state.write(&mut send, bytes(data)));
|
|
533
|
+
state.restore(&state.send, send);
|
|
534
|
+
settle(ruby, outcome).inspect_err(|_| state.mark_failed())?;
|
|
535
|
+
Ok(ruby.qnil().as_value())
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
fn close_send(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
|
|
539
|
+
rb_self.state(ruby)?.close_send();
|
|
540
|
+
Ok(ruby.qnil().as_value())
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
fn trailers(ruby: &Ruby, rb_self: &Self) -> Result<Option<RHash>, Error> {
|
|
544
|
+
let state = rb_self.state(ruby)?;
|
|
545
|
+
let guard = state.recv.lock().unwrap_or_else(|e| e.into_inner());
|
|
546
|
+
match guard.as_ref().and_then(|recv| recv.trailers.as_ref()) {
|
|
547
|
+
Some(trailers) => Ok(Some(header_hash(ruby, trailers)?)),
|
|
548
|
+
None => Ok(None),
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/// Reads the rest of the response (discarding data), then releases the
|
|
553
|
+
/// call and its connection.
|
|
554
|
+
fn finish(ruby: &Ruby, rb_self: &Self, timeout: Option<f64>) -> Result<Value, Error> {
|
|
555
|
+
let state = rb_self.state(ruby)?;
|
|
556
|
+
if state.is_finished() {
|
|
557
|
+
return Ok(ruby.qnil().as_value());
|
|
558
|
+
}
|
|
559
|
+
let mut recv = Self::take_recv(ruby, state)?;
|
|
560
|
+
let waits = Self::waits(state, instant(timeout));
|
|
561
|
+
let outcome = drive(ruby, &waits, state.drain(&mut recv));
|
|
562
|
+
state.restore(&state.recv, recv);
|
|
563
|
+
settle(ruby, outcome).inspect_err(|_| state.mark_failed())?;
|
|
564
|
+
state.complete();
|
|
565
|
+
Ok(ruby.qnil().as_value())
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
fn abort(&self) {
|
|
569
|
+
if !self.state.inherited() {
|
|
570
|
+
self.state.abort();
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
fn local_port(&self) -> u16 {
|
|
575
|
+
if self.state.inherited() {
|
|
576
|
+
0
|
|
577
|
+
} else {
|
|
578
|
+
self.state.local_port()
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
fn is_h2(&self) -> bool {
|
|
583
|
+
!self.state.inherited() && self.state.is_h2()
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
fn is_finished(&self) -> bool {
|
|
587
|
+
self.state.inherited() || self.state.is_finished()
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
fn wait_count() -> u64 {
|
|
592
|
+
gvl::wait_count()
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
fn operation_count() -> u64 {
|
|
596
|
+
gvl::operation_count()
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
fn active_waits() -> usize {
|
|
600
|
+
gvl::active_waits()
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
fn resumed_waits() -> usize {
|
|
604
|
+
gvl::resumed_waits()
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
fn wait_bodies() -> (usize, usize) {
|
|
608
|
+
gvl::wait_bodies()
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
#[magnus::init]
|
|
612
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
613
|
+
let connectrpc = ruby.define_module("ConnectRPC")?;
|
|
614
|
+
let hyper = connectrpc.define_module("Hyper")?;
|
|
615
|
+
let native = hyper.define_module("Native")?;
|
|
616
|
+
gvl::init_flags(ruby)?;
|
|
617
|
+
native.define_singleton_method("wait_count", function!(wait_count, 0))?;
|
|
618
|
+
native.define_singleton_method("operation_count", function!(operation_count, 0))?;
|
|
619
|
+
native.define_singleton_method("active_waits", function!(active_waits, 0))?;
|
|
620
|
+
native.define_singleton_method("resumed_waits", function!(resumed_waits, 0))?;
|
|
621
|
+
native.define_singleton_method("wait_bodies", function!(wait_bodies, 0))?;
|
|
622
|
+
|
|
623
|
+
let cancel: RClass = native.define_class("Cancel", ruby.class_object())?;
|
|
624
|
+
cancel.define_singleton_method("new", function!(Cancel::new, 0))?;
|
|
625
|
+
cancel.define_method("fire", method!(Cancel::fire, 0))?;
|
|
626
|
+
cancel.define_method("fired?", method!(Cancel::is_fired, 0))?;
|
|
627
|
+
|
|
628
|
+
let driver: RClass = native.define_class("Driver", ruby.class_object())?;
|
|
629
|
+
driver.define_singleton_method("new", function!(Driver::new, 9))?;
|
|
630
|
+
driver.define_method("unary", method!(Driver::unary, 9))?;
|
|
631
|
+
driver.define_method("open", method!(Driver::open, 8))?;
|
|
632
|
+
driver.define_method("close", method!(Driver::close, 1))?;
|
|
633
|
+
driver.define_method("opened", method!(Driver::opened, 0))?;
|
|
634
|
+
driver.define_method("stats", method!(Driver::stats, 0))?;
|
|
635
|
+
driver.define_method("close_outcome", method!(Driver::close_outcome, 0))?;
|
|
636
|
+
driver.define_method("hold", method!(Driver::hold, 2))?;
|
|
637
|
+
driver.define_method("release", method!(Driver::release, 1))?;
|
|
638
|
+
driver.define_method("reached", method!(Driver::reached, 1))?;
|
|
639
|
+
|
|
640
|
+
let call: RClass = native.define_class("Call", ruby.class_object())?;
|
|
641
|
+
call.define_method("head", method!(Call::head, 1))?;
|
|
642
|
+
call.define_method("read", method!(Call::read, 2))?;
|
|
643
|
+
call.define_method("write", method!(Call::write, 2))?;
|
|
644
|
+
call.define_method("close_send", method!(Call::close_send, 0))?;
|
|
645
|
+
call.define_method("trailers", method!(Call::trailers, 0))?;
|
|
646
|
+
call.define_method("finish", method!(Call::finish, 1))?;
|
|
647
|
+
call.define_method("abort", method!(Call::abort, 0))?;
|
|
648
|
+
call.define_method("local_port", method!(Call::local_port, 0))?;
|
|
649
|
+
call.define_method("h2?", method!(Call::is_h2, 0))?;
|
|
650
|
+
call.define_method("finished?", method!(Call::is_finished, 0))?;
|
|
651
|
+
Ok(())
|
|
652
|
+
}
|