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" ) ) ]
2122use std:: fmt;
23+ #[ cfg( not( feature = "ffi" ) ) ]
2224use std:: future:: Future ;
25+ #[ cfg( not( feature = "ffi" ) ) ]
2326use std:: pin:: Pin ;
2427use std:: sync:: { Arc , Mutex } ;
2528use std:: task:: { self , Poll } ;
@@ -28,7 +31,9 @@ use std::time::{Duration, Instant};
2831use h2:: { Ping , PingPong } ;
2932use tracing:: { debug, trace} ;
3033
34+ #[ cfg_attr( feature = "ffi" , allow( unused) ) ]
3135use crate :: common:: time:: Time ;
36+ #[ cfg_attr( feature = "ffi" , allow( unused) ) ]
3237use crate :: rt:: Sleep ;
3338
3439type 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
93103pub ( 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
109122pub ( 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" ) ) ]
150167struct 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" ) ) ]
163182enum KeepAliveState {
164183 Init ,
165184 Scheduled ( Instant ) ,
@@ -168,17 +187,27 @@ enum KeepAliveState {
168187
169188pub ( super ) enum Ponged {
170189 SizeUpdate ( WindowSize ) ,
190+ #[ cfg( not( feature = "ffi" ) ) ]
171191 KeepAliveTimedOut ,
172192}
173193
194+ #[ cfg( not( feature = "ffi" ) ) ]
174195#[ derive( Debug ) ]
175196pub ( super ) struct KeepAliveTimedOut ;
176197
177198// ===== impl Config =====
178199
179200impl 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" ) ) ]
426476impl 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" ) ) ]
490541impl 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" ) ) ]
496548impl 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" ) ) ]
502555impl std:: error:: Error for KeepAliveTimedOut {
503556 fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
504557 Some ( & crate :: error:: TimedOut )
0 commit comments