Skip to content

Commit 860f0f3

Browse files
committed
fix(ffi): hide some runtime code from ffi
code that was previously not compiled for the C API due to the `runtime` flag was causing compilation errors
1 parent 2b5721a commit 860f0f3

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed

src/proto/h2/client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ fn new_ping_config(config: &Config) -> ping::Config {
8989
} else {
9090
None
9191
},
92+
#[cfg(not(feature = "ffi"))]
9293
keep_alive_interval: config.keep_alive_interval,
94+
#[cfg(not(feature = "ffi"))]
9395
keep_alive_timeout: config.keep_alive_timeout,
96+
#[cfg(not(feature = "ffi"))]
9497
keep_alive_while_idle: config.keep_alive_while_idle,
9598
}
9699
}
@@ -137,6 +140,7 @@ where
137140
conn.set_target_window_size(wnd);
138141
conn.set_initial_window_size(wnd)?;
139142
}
143+
#[cfg(not(feature = "ffi"))]
140144
Poll::Ready(ping::Ponged::KeepAliveTimedOut) => {
141145
debug!("connection keep-alive timed out");
142146
return Poll::Ready(Ok(()));

src/proto/h2/ping.rs

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
/// 3b. Merge RTT with a running average.
1919
/// 3c. Calculate bdp as bytes/rtt.
2020
/// 3d. If bdp is over 2/3 max, set new max to bdp and update windows.
21+
#[cfg(not(feature = "ffi"))]
2122
use std::fmt;
23+
#[cfg(not(feature = "ffi"))]
2224
use std::future::Future;
25+
#[cfg(not(feature = "ffi"))]
2326
use std::pin::Pin;
2427
use std::sync::{Arc, Mutex};
2528
use std::task::{self, Poll};
@@ -28,7 +31,9 @@ use std::time::{Duration, Instant};
2831
use h2::{Ping, PingPong};
2932
use tracing::{debug, trace};
3033

34+
#[cfg_attr(feature = "ffi", allow(unused))]
3135
use crate::common::time::Time;
36+
#[cfg_attr(feature = "ffi", allow(unused))]
3237
use crate::rt::Sleep;
3338

3439
type WindowSize = u32;
@@ -57,6 +62,7 @@ pub(super) fn channel(ping_pong: PingPong, config: Config, __timer: Time) -> (Re
5762
(None, None)
5863
};
5964

65+
#[cfg(not(feature = "ffi"))]
6066
let keep_alive = config.keep_alive_interval.map(|interval| KeepAlive {
6167
interval,
6268
timeout: config.keep_alive_timeout,
@@ -66,11 +72,14 @@ pub(super) fn channel(ping_pong: PingPong, config: Config, __timer: Time) -> (Re
6672
timer: __timer,
6773
});
6874

75+
#[cfg(not(feature = "ffi"))]
6976
let last_read_at = keep_alive.as_ref().map(|_| Instant::now());
7077

7178
let shared = Arc::new(Mutex::new(Shared {
7279
bytes,
80+
#[cfg(not(feature = "ffi"))]
7381
last_read_at,
82+
#[cfg(not(feature = "ffi"))]
7483
is_keep_alive_timed_out: false,
7584
ping_pong,
7685
ping_sent_at: None,
@@ -83,6 +92,7 @@ pub(super) fn channel(ping_pong: PingPong, config: Config, __timer: Time) -> (Re
8392
},
8493
Ponger {
8594
bdp,
95+
#[cfg(not(feature = "ffi"))]
8696
keep_alive,
8797
shared,
8898
},
@@ -93,11 +103,14 @@ pub(super) fn channel(ping_pong: PingPong, config: Config, __timer: Time) -> (Re
93103
pub(super) struct Config {
94104
pub(super) bdp_initial_window: Option<WindowSize>,
95105
/// If no frames are received in this amount of time, a PING frame is sent.
106+
#[cfg(not(feature = "ffi"))]
96107
pub(super) keep_alive_interval: Option<Duration>,
97108
/// After sending a keepalive PING, the connection will be closed if
98109
/// a pong is not received in this amount of time.
110+
#[cfg(not(feature = "ffi"))]
99111
pub(super) keep_alive_timeout: Duration,
100112
/// If true, sends pings even when there are no active streams.
113+
#[cfg(not(feature = "ffi"))]
101114
pub(super) keep_alive_while_idle: bool,
102115
}
103116

@@ -108,6 +121,7 @@ pub(crate) struct Recorder {
108121

109122
pub(super) struct Ponger {
110123
bdp: Option<Bdp>,
124+
#[cfg(not(feature = "ffi"))]
111125
keep_alive: Option<KeepAlive>,
112126
shared: Arc<Mutex<Shared>>,
113127
}
@@ -127,8 +141,10 @@ struct Shared {
127141
// keep-alive
128142
/// If `Some`, keep-alive is enabled, and the Instant is how long ago
129143
/// the connection read the last frame.
144+
#[cfg(not(feature = "ffi"))]
130145
last_read_at: Option<Instant>,
131146

147+
#[cfg(not(feature = "ffi"))]
132148
is_keep_alive_timed_out: bool,
133149
}
134150

@@ -147,6 +163,7 @@ struct Bdp {
147163
stable_count: u32,
148164
}
149165

166+
#[cfg(not(feature = "ffi"))]
150167
struct KeepAlive {
151168
/// If no frames are received in this amount of time, a PING frame is sent.
152169
interval: Duration,
@@ -155,11 +172,13 @@ struct KeepAlive {
155172
timeout: Duration,
156173
/// If true, sends pings even when there are no active streams.
157174
while_idle: bool,
175+
158176
state: KeepAliveState,
159177
sleep: Pin<Box<dyn Sleep>>,
160178
timer: Time,
161179
}
162180

181+
#[cfg(not(feature = "ffi"))]
163182
enum KeepAliveState {
164183
Init,
165184
Scheduled(Instant),
@@ -168,17 +187,27 @@ enum KeepAliveState {
168187

169188
pub(super) enum Ponged {
170189
SizeUpdate(WindowSize),
190+
#[cfg(not(feature = "ffi"))]
171191
KeepAliveTimedOut,
172192
}
173193

194+
#[cfg(not(feature = "ffi"))]
174195
#[derive(Debug)]
175196
pub(super) struct KeepAliveTimedOut;
176197

177198
// ===== impl Config =====
178199

179200
impl Config {
180201
pub(super) fn is_enabled(&self) -> bool {
181-
self.bdp_initial_window.is_some() || self.keep_alive_interval.is_some()
202+
#[cfg(not(feature = "ffi"))]
203+
{
204+
self.bdp_initial_window.is_some() || self.keep_alive_interval.is_some()
205+
}
206+
207+
#[cfg(feature = "ffi")]
208+
{
209+
self.bdp_initial_window.is_some()
210+
}
182211
}
183212
}
184213

@@ -194,6 +223,7 @@ impl Recorder {
194223

195224
let mut locked = shared.lock().unwrap();
196225

226+
#[cfg(not(feature = "ffi"))]
197227
locked.update_last_read_at();
198228

199229
// are we ready to send another bdp ping?
@@ -220,15 +250,18 @@ impl Recorder {
220250
}
221251

222252
pub(crate) fn record_non_data(&self) {
223-
let shared = if let Some(ref shared) = self.shared {
224-
shared
225-
} else {
226-
return;
227-
};
253+
#[cfg(not(feature = "ffi"))]
254+
{
255+
let shared = if let Some(ref shared) = self.shared {
256+
shared
257+
} else {
258+
return;
259+
};
228260

229-
let mut locked = shared.lock().unwrap();
261+
let mut locked = shared.lock().unwrap();
230262

231-
locked.update_last_read_at();
263+
locked.update_last_read_at();
264+
}
232265
}
233266

234267
/// If the incoming stream is already closed, convert self into
@@ -243,10 +276,13 @@ impl Recorder {
243276
}
244277

245278
pub(super) fn ensure_not_timed_out(&self) -> crate::Result<()> {
246-
if let Some(ref shared) = self.shared {
247-
let locked = shared.lock().unwrap();
248-
if locked.is_keep_alive_timed_out {
249-
return Err(KeepAliveTimedOut.crate_error());
279+
#[cfg(not(feature = "ffi"))]
280+
{
281+
if let Some(ref shared) = self.shared {
282+
let locked = shared.lock().unwrap();
283+
if locked.is_keep_alive_timed_out {
284+
return Err(KeepAliveTimedOut.crate_error());
285+
}
250286
}
251287
}
252288

@@ -261,11 +297,15 @@ impl Ponger {
261297
pub(super) fn poll(&mut self, cx: &mut task::Context<'_>) -> Poll<Ponged> {
262298
let now = Instant::now();
263299
let mut locked = self.shared.lock().unwrap();
300+
#[cfg(not(feature = "ffi"))]
264301
let is_idle = self.is_idle();
265302

266-
if let Some(ref mut ka) = self.keep_alive {
267-
ka.maybe_schedule(is_idle, &locked);
268-
ka.maybe_ping(cx, &mut locked);
303+
#[cfg(not(feature = "ffi"))]
304+
{
305+
if let Some(ref mut ka) = self.keep_alive {
306+
ka.maybe_schedule(is_idle, &locked);
307+
ka.maybe_ping(cx, &mut locked);
308+
}
269309
}
270310

271311
if !locked.is_ping_sent() {
@@ -282,10 +322,13 @@ impl Ponger {
282322
let rtt = now - start;
283323
trace!("recv pong");
284324

285-
if let Some(ref mut ka) = self.keep_alive {
286-
locked.update_last_read_at();
287-
ka.maybe_schedule(is_idle, &locked);
288-
ka.maybe_ping(cx, &mut locked);
325+
#[cfg(not(feature = "ffi"))]
326+
{
327+
if let Some(ref mut ka) = self.keep_alive {
328+
locked.update_last_read_at();
329+
ka.maybe_schedule(is_idle, &locked);
330+
ka.maybe_ping(cx, &mut locked);
331+
}
289332
}
290333

291334
if let Some(ref mut bdp) = self.bdp {
@@ -304,11 +347,14 @@ impl Ponger {
304347
debug!("pong error: {}", e);
305348
}
306349
Poll::Pending => {
307-
if let Some(ref mut ka) = self.keep_alive {
308-
if let Err(KeepAliveTimedOut) = ka.maybe_timeout(cx) {
309-
self.keep_alive = None;
310-
locked.is_keep_alive_timed_out = true;
311-
return Poll::Ready(Ponged::KeepAliveTimedOut);
350+
#[cfg(not(feature = "ffi"))]
351+
{
352+
if let Some(ref mut ka) = self.keep_alive {
353+
if let Err(KeepAliveTimedOut) = ka.maybe_timeout(cx) {
354+
self.keep_alive = None;
355+
locked.is_keep_alive_timed_out = true;
356+
return Poll::Ready(Ponged::KeepAliveTimedOut);
357+
}
312358
}
313359
}
314360
}
@@ -318,6 +364,7 @@ impl Ponger {
318364
Poll::Pending
319365
}
320366

367+
#[cfg(not(feature = "ffi"))]
321368
fn is_idle(&self) -> bool {
322369
Arc::strong_count(&self.shared) <= 2
323370
}
@@ -342,12 +389,14 @@ impl Shared {
342389
self.ping_sent_at.is_some()
343390
}
344391

392+
#[cfg(not(feature = "ffi"))]
345393
fn update_last_read_at(&mut self) {
346394
if self.last_read_at.is_some() {
347395
self.last_read_at = Some(Instant::now());
348396
}
349397
}
350398

399+
#[cfg(not(feature = "ffi"))]
351400
fn last_read_at(&self) -> Instant {
352401
self.last_read_at.expect("keep_alive expects last_read_at")
353402
}
@@ -423,6 +472,7 @@ fn seconds(dur: Duration) -> f64 {
423472

424473
// ===== impl KeepAlive =====
425474

475+
#[cfg(not(feature = "ffi"))]
426476
impl KeepAlive {
427477
fn maybe_schedule(&mut self, is_idle: bool, shared: &Shared) {
428478
match self.state {
@@ -487,18 +537,21 @@ impl KeepAlive {
487537

488538
// ===== impl KeepAliveTimedOut =====
489539

540+
#[cfg(not(feature = "ffi"))]
490541
impl KeepAliveTimedOut {
491542
pub(super) fn crate_error(self) -> crate::Error {
492543
crate::Error::new(crate::error::Kind::Http2).with(self)
493544
}
494545
}
495546

547+
#[cfg(not(feature = "ffi"))]
496548
impl fmt::Display for KeepAliveTimedOut {
497549
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
498550
f.write_str("keep-alive timed out")
499551
}
500552
}
501553

554+
#[cfg(not(feature = "ffi"))]
502555
impl std::error::Error for KeepAliveTimedOut {
503556
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
504557
Some(&crate::error::TimedOut)

src/proto/h2/server.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,13 @@ where
142142

143143
let ping_config = ping::Config {
144144
bdp_initial_window: bdp,
145+
#[cfg(not(feature = "ffi"))]
145146
keep_alive_interval: config.keep_alive_interval,
147+
#[cfg(not(feature = "ffi"))]
146148
keep_alive_timeout: config.keep_alive_timeout,
147149
// If keep-alive is enabled for servers, always enabled while
148150
// idle, so it can more aggressively close dead connections.
151+
#[cfg(not(feature = "ffi"))]
149152
keep_alive_while_idle: true,
150153
};
151154

@@ -360,6 +363,7 @@ where
360363
self.conn.set_target_window_size(wnd);
361364
let _ = self.conn.set_initial_window_size(wnd);
362365
}
366+
#[cfg(not(feature = "ffi"))]
363367
Poll::Ready(ping::Ponged::KeepAliveTimedOut) => {
364368
debug!("keep-alive timed out, closing connection");
365369
self.conn.abrupt_shutdown(h2::Reason::NO_ERROR);

0 commit comments

Comments
 (0)