open-net 0.3.5

A cross-platform async networking library for Rust with HTTP and WebSocket clients, network status monitoring, configurable retries, WebSocket reconnection and heartbeats, and managed client lifecycles. Reuse common networking infrastructure across applications to avoid repeated implementation and debugging, helping save development time and AI coding tokens.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
use super::source::{SourceCore, SourceState};
use super::*;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::task::{Context, Poll, Waker};
use tokio::sync::OwnedSemaphorePermit;

type Callback<T> = dyn Fn(CallbackContext, Result<T>) + Send + Sync + 'static;
type Driver = dyn Fn() -> Result<()> + Send + Sync + 'static;

pub(super) struct Registration<T> {
    pub(super) id: SubscriptionId,
    source: Weak<SourceCore<T>>,
    read_committed: bool,
    // Retain only snapshots after the source owner goes away. In particular,
    // this does not retain the source's executor or subscription budget.
    committed_state: Option<Arc<Mutex<SourceState<T>>>>,
    pub(super) state: Mutex<RegistrationState<T>>,
    #[cfg(all(test, feature = "ws-client"))]
    after_pending: Mutex<Option<Box<dyn FnOnce() + Send>>>,
}
pub(super) struct RegistrationState<T> {
    executor: Option<Arc<dyn CallbackExecutor>>,
    active: bool,
    initial: Option<Arc<T>>,
    initial_revision: u64,
    latest: Arc<T>,
    revision: u64,
    seen: u64,
    source_closed: bool,
    pending_error: Option<NetError>,
    final_error: Option<NetError>,
    callback: Option<Arc<Callback<T>>>,
    driver: Option<Arc<Driver>>,
    scheduled: bool,
    running: bool,
    permit: Option<Arc<OwnedSemaphorePermit>>,
    receive_waker: Option<Waker>,
    close_waker: Option<Waker>,
    lifetime: Option<Box<dyn Send + Sync>>,
}
enum Delivery<T> {
    Value(Arc<T>),
    Error(NetError),
    Done,
    Pending,
}
impl<T> RegistrationState<T> {
    fn next(&mut self) -> Delivery<T> {
        if !self.active {
            return Delivery::Done;
        }
        if let Some(initial) = self.initial.take() {
            self.seen = self.initial_revision;
            return Delivery::Value(initial);
        }
        if self.seen != self.revision {
            self.seen = self.revision;
            return Delivery::Value(self.latest.clone());
        }
        if let Some(error) = self.pending_error.take() {
            return Delivery::Error(error);
        }
        if self.source_closed {
            Delivery::Done
        } else {
            Delivery::Pending
        }
    }
    fn pending(&self) -> bool {
        self.initial.is_some()
            || self.seen != self.revision
            || self.pending_error.is_some()
            || self.source_closed
    }
}
impl<T> Registration<T> {
    #[allow(clippy::too_many_arguments)]
    pub(super) fn new(
        id: SubscriptionId,
        source: &Arc<SourceCore<T>>,
        executor: Arc<dyn CallbackExecutor>,
        current: Arc<T>,
        revision: u64,
        closed: bool,
        failure: Option<NetError>,
        permit: OwnedSemaphorePermit,
        read_committed: bool,
    ) -> Self {
        Self {
            id,
            source: Arc::downgrade(source),
            read_committed,
            committed_state: read_committed.then(|| source.state.clone()),
            #[cfg(all(test, feature = "ws-client"))]
            after_pending: Mutex::new(None),
            state: Mutex::new(RegistrationState {
                executor: Some(executor),
                active: true,
                initial: Some(current.clone()),
                initial_revision: revision,
                latest: current,
                revision,
                seen: revision,
                source_closed: closed,
                pending_error: failure.clone(),
                final_error: failure,
                callback: None,
                driver: None,
                scheduled: false,
                running: false,
                permit: Some(Arc::new(permit)),
                receive_waker: None,
                close_waker: None,
                lifetime: None,
            }),
        }
    }
    pub(super) fn is_active(&self) -> bool {
        lock(&self.state).active
    }
    #[cfg_attr(not(any(feature = "http-client", test)), allow(dead_code))]
    pub(super) fn bind_lifetime(&self, lifetime: Box<dyn Send + Sync>) -> Result<()> {
        let mut state = lock(&self.state);
        if !state.active || state.source_closed {
            drop(state);
            return Err(NetError::from(ErrorKind::Closed));
        }
        if state.lifetime.is_some() {
            drop(state);
            return Err(NetError::from(ErrorKind::InvalidInput));
        }
        state.lifetime = Some(lifetime);
        Ok(())
    }
    #[cfg(all(test, feature = "ws-client"))]
    pub(super) fn after_pending_for_test(&self, hook: impl FnOnce() + Send + 'static) {
        let previous = lock(&self.after_pending).replace(Box::new(hook));
        drop(previous);
    }
    #[cfg(all(test, feature = "ws-client"))]
    fn run_pending_hook(&self) {
        let hook = lock(&self.after_pending).take();
        if let Some(hook) = hook {
            hook();
        }
    }
    pub(super) fn update(
        &self,
        value: Arc<T>,
        revision: u64,
        closed: bool,
        failure: Option<NetError>,
    ) {
        let retired = {
            let mut state = lock(&self.state);
            if !state.active || revision < state.revision {
                return;
            }
            if state.source_closed {
                // A committed-cache read may have imported the terminal value
                // before its deferred notification. Preserve that notification
                // without reinstalling an already consumed terminal error.
                if !self.read_committed || !closed || revision != state.revision {
                    return;
                }
                (None, None, None, state.receive_waker.take(), None)
            } else {
                let old = std::mem::replace(&mut state.latest, value.clone());
                state.revision = revision;
                state.source_closed = closed;
                let old_pending = std::mem::replace(&mut state.pending_error, failure.clone());
                let old_error = std::mem::replace(&mut state.final_error, failure.clone());
                let lifetime = if closed { state.lifetime.take() } else { None };
                (
                    Some(old),
                    old_pending,
                    old_error,
                    state.receive_waker.take(),
                    lifetime,
                )
            }
        };
        let (old, old_pending, old_error, waker, lifetime) = retired;
        // SDK ownership is retired before any user-controlled destructor or wake.
        drop(lifetime);
        drop((old, old_pending, old_error));
        if let Some(waker) = waker {
            waker.wake();
        }
        let _ = self.schedule();
    }
    /// Refresh only on a consumer's own stack. No lifecycle publisher calls this
    /// path, and no user value, lifetime token or waker is released under a lock.
    fn refresh_committed(&self) {
        let Some(source) = &self.committed_state else {
            return;
        };
        let (value, revision, closed, failure) = {
            let source = lock(source);
            (
                source.current.clone(),
                source.revision,
                source.closed,
                source.failure.clone(),
            )
        };
        let retired = {
            let mut state = lock(&self.state);
            if !state.active
                || state.source_closed
                || revision < state.revision
                || (revision == state.revision && !closed)
            {
                drop(state);
                return;
            }
            let old = std::mem::replace(&mut state.latest, value);
            state.revision = revision;
            state.source_closed = closed;
            let pending_error = std::mem::replace(&mut state.pending_error, failure.clone());
            let final_error = std::mem::replace(&mut state.final_error, failure);
            let lifetime = if closed { state.lifetime.take() } else { None };
            (old, pending_error, final_error, lifetime)
        };
        let (old, pending_error, final_error, lifetime) = retired;
        drop(lifetime);
        drop((old, pending_error, final_error));
    }
    pub(super) fn unsubscribe(&self) -> bool {
        let retired = {
            let mut state = lock(&self.state);
            if !state.active {
                return false;
            }
            state.active = false;
            let close_waker = if state.running {
                None
            } else {
                state.close_waker.take()
            };
            (
                state.initial.take(),
                state.pending_error.take(),
                state.callback.take(),
                state.driver.take(),
                state.executor.take(),
                state.permit.take(),
                state.receive_waker.take(),
                close_waker,
                state.lifetime.take(),
            )
        };
        if let Some(source) = self.source.upgrade() {
            source.remove(self.id);
        }
        let (initial, error, callback, driver, executor, permit, receive, close, lifetime) =
            retired;
        drop(lifetime);
        drop((initial, error, callback, driver, executor, permit));
        if let Some(waker) = receive {
            waker.wake();
        }
        if let Some(waker) = close {
            waker.wake();
        }
        true
    }
    fn schedule(&self) -> Result<()> {
        let driver = {
            let mut state = lock(&self.state);
            if !state.active || state.scheduled || !state.pending() {
                return Ok(());
            }
            let Some(driver) = state.driver.clone() else {
                return Ok(());
            };
            state.scheduled = true;
            driver
        };
        if let Err(error) = driver() {
            self.fail(error.clone());
            return Err(error);
        }
        Ok(())
    }
    fn fail(&self, error: NetError) {
        {
            let mut state = lock(&self.state);
            state.final_error.get_or_insert_with(|| error.clone());
        }
        crate::log_e!(crate::common::log::log_def::LogType::Common; "subscription", "id|kind", self.id.as_u64(), format!("{:?}", error.kind()));
        self.unsubscribe();
    }
    pub(super) fn poll_close(&self, cx: &mut Context<'_>) -> Poll<Result<()>> {
        let new_waker = cx.waker().clone();
        let (result, old) = {
            let mut state = lock(&self.state);
            if state.running {
                (Poll::Pending, state.close_waker.replace(new_waker))
            } else {
                (
                    Poll::Ready(state.final_error.clone().map_or(Ok(()), Err)),
                    Some(new_waker),
                )
            }
        };
        drop(old);
        result
    }
}
impl<T: Clone> Registration<T> {
    pub(super) fn current(&self) -> T {
        self.refresh_committed();
        let value = lock(&self.state).latest.clone();
        (*value).clone()
    }
    pub(super) fn poll_next(&self, cx: &mut Context<'_>) -> Poll<Option<Result<T>>> {
        self.refresh_committed();
        let new_waker = cx.waker().clone();
        let (delivery, old) = {
            let mut state = lock(&self.state);
            let delivery = state.next();
            let old = if matches!(delivery, Delivery::Pending) {
                state.receive_waker.replace(new_waker)
            } else {
                Some(new_waker)
            };
            (delivery, old)
        };
        drop(old);
        match delivery {
            Delivery::Value(value) => Poll::Ready(Some(Ok((*value).clone()))),
            Delivery::Error(error) => Poll::Ready(Some(Err(error))),
            Delivery::Done => {
                self.unsubscribe();
                Poll::Ready(None)
            }
            Delivery::Pending => Poll::Pending,
        }
    }
}
impl<T: Clone + Send + Sync + 'static> Registration<T> {
    pub(super) fn install_callback(
        self: &Arc<Self>,
        callback: impl Fn(CallbackContext, Result<T>) + Send + Sync + 'static,
    ) -> Result<()> {
        let executor = lock(&self.state)
            .executor
            .clone()
            .ok_or_else(|| NetError::from(ErrorKind::Closed))?;
        executor.ensure_ready()?;
        let callback: Arc<Callback<T>> = Arc::new(callback);
        let weak = Arc::downgrade(self);
        let permit = lock(&self.state)
            .permit
            .clone()
            .ok_or_else(|| NetError::from(ErrorKind::Closed))?;
        let driver: Arc<Driver> = Arc::new(move || {
            let weak = weak.clone();
            let abandoned = weak.clone();
            // A cancelled, queued job still occupies capacity until the existing pool retires it.
            let permit = permit.clone();
            super::dispatch::submit_tracked(
                executor.as_ref(),
                Box::new(move || {
                    let _permit = permit;
                    if let Some(registration) = weak.upgrade() {
                        registration.run();
                    }
                }),
                move |error| {
                    if let Some(registration) = abandoned.upgrade() {
                        if registration.is_active() {
                            registration.fail(error);
                        }
                    }
                },
            )
        });
        {
            let mut state = lock(&self.state);
            if !state.active {
                return Err(NetError::from(ErrorKind::Closed));
            }
            state.callback = Some(callback.clone());
            state.driver = Some(driver.clone());
        }
        self.schedule()
    }
    fn run(self: Arc<Self>) {
        {
            let mut state = lock(&self.state);
            if !state.active {
                state.scheduled = false;
                return;
            }
            state.running = true;
        }
        let mut completion = RunnerCompletion {
            registration: &self,
            armed: true,
        };
        // The outer boundary also covers callback captures and caught-panic payload destructors.
        let outcome = catch_unwind(AssertUnwindSafe(|| self.run_callbacks(&mut completion)));
        self.complete_runner(outcome);
    }
    fn run_callbacks(self: &Arc<Self>, completion: &mut RunnerCompletion<'_, T>) {
        loop {
            self.refresh_committed();
            let (delivery, callback, close) = {
                let mut state = lock(&self.state);
                let delivery = state.next();
                let callback = match delivery {
                    Delivery::Value(_) | Delivery::Error(_) => state.callback.clone(),
                    Delivery::Done | Delivery::Pending => None,
                };
                let close = if matches!(delivery, Delivery::Pending) {
                    // The idle decision and retirement are one transition. A later
                    // publication can submit its successor before the pool closes.
                    state.running = false;
                    state.scheduled = false;
                    completion.armed = false;
                    state.close_waker.take()
                } else {
                    None
                };
                (delivery, callback, close)
            };
            if let Some(waker) = close {
                waker.wake();
            }
            let outcome = match (delivery, callback) {
                (Delivery::Value(value), Some(callback)) => {
                    let context = self.context();
                    catch_unwind(AssertUnwindSafe(|| {
                        let _scope = super::CallbackScope::enter();
                        callback(context, Ok((*value).clone()))
                    }))
                }
                (Delivery::Error(error), Some(callback)) => {
                    let context = self.context();
                    catch_unwind(AssertUnwindSafe(|| {
                        let _scope = super::CallbackScope::enter();
                        callback(context, Err(error))
                    }))
                }
                (Delivery::Done, _) => {
                    self.unsubscribe();
                    return;
                }
                (Delivery::Pending, _) => {
                    #[cfg(all(test, feature = "ws-client"))]
                    self.run_pending_hook();
                    return;
                }
                (_, None) => return,
            };
            if !self.complete_callback(outcome) {
                return;
            }
        }
    }
    pub(super) fn complete_callback(&self, outcome: std::thread::Result<()>) -> bool {
        match outcome {
            Ok(()) => true,
            Err(payload) => {
                self.fail(callback_fault());
                drop(payload);
                false
            }
        }
    }
    fn complete_runner(&self, outcome: std::thread::Result<()>) {
        if let Err(payload) = outcome {
            let cleanup = catch_unwind(AssertUnwindSafe(|| {
                self.fail(callback_fault());
                drop(payload);
            }));
            if let Err(secondary) = cleanup {
                discard_cleanup_panic(self.id, secondary);
            }
        }
    }
    fn context(self: &Arc<Self>) -> CallbackContext {
        let control: Arc<dyn SubscriptionControl> = self.clone();
        CallbackContext {
            id: self.id,
            control: Arc::downgrade(&control),
        }
    }
    fn finish_run(&self) {
        let waker = {
            let mut state = lock(&self.state);
            state.running = false;
            state.scheduled = false;
            state.close_waker.take()
        };
        if let Some(waker) = waker {
            waker.wake();
        }
        let _ = self.schedule();
    }
}
impl<T: Clone + Send + Sync + 'static> SubscriptionControl for Registration<T> {
    fn is_active(&self) -> bool {
        self.is_active()
    }
    fn unsubscribe(&self) -> bool {
        self.unsubscribe()
    }
    fn poll_close(&self, cx: &mut Context<'_>) -> Poll<Result<()>> {
        self.poll_close(cx)
    }
}

fn callback_fault() -> NetError {
    NetError::from(ErrorKind::CallbackPanicked).with_stage(crate::error::ErrorStage::Dispatch)
}

// Borrowing keeps the completion guard itself from owning a final user snapshot or capture.
struct RunnerCompletion<'a, T: Clone + Send + Sync + 'static> {
    registration: &'a Registration<T>,
    armed: bool,
}
impl<T: Clone + Send + Sync + 'static> Drop for RunnerCompletion<'_, T> {
    fn drop(&mut self) {
        // A normal idle runner already retired under the state lock. Its old
        // guard must never clear flags belonging to a concurrently started job.
        if !self.armed {
            return;
        }
        if std::thread::panicking() {
            let cleanup = catch_unwind(AssertUnwindSafe(|| {
                self.registration.fail(callback_fault())
            }));
            if let Err(secondary) = cleanup {
                discard_cleanup_panic(self.registration.id, secondary);
            }
        }
        let completion = catch_unwind(AssertUnwindSafe(|| self.registration.finish_run()));
        if let Err(secondary) = completion {
            let cleanup = catch_unwind(AssertUnwindSafe(|| {
                self.registration.fail(callback_fault())
            }));
            if let Err(payload) = cleanup {
                discard_cleanup_panic(self.registration.id, payload);
            }
            discard_cleanup_panic(self.registration.id, secondary);
        }
    }
}
fn discard_cleanup_panic(id: SubscriptionId, payload: Box<dyn std::any::Any + Send>) {
    crate::log_e!(crate::common::log::log_def::LogType::Common;
        "subscription_cleanup", "id|kind", id.as_u64(), format!("{:?}", ErrorKind::CallbackPanicked));
    // Only a second panic caught while retiring a runner is leaked: its destructor may panic
    // again. Normal callback captures, state values and the first caught payload are released.
    std::mem::forget(payload);
}

#[cfg(all(test, feature = "ws-client"))]
#[path = "runner_retirement_tests.rs"]
mod runner_retirement_tests;

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Default)]
    struct HeldExecutor {
        jobs: Mutex<Vec<Box<dyn FnOnce() + Send>>>,
    }
    impl CallbackExecutor for HeldExecutor {
        fn ensure_ready(&self) -> Result<()> {
            Ok(())
        }
        fn submit(&self, job: Box<dyn FnOnce() + Send>) -> Result<()> {
            let mut jobs = lock(&self.jobs);
            jobs.try_reserve(1)
                .map_err(|error| NetError::with_source(ErrorKind::ResourceExhausted, error))?;
            jobs.push(job);
            Ok(())
        }
    }

    #[derive(Default)]
    struct RetiringExecutor {
        job: Mutex<Option<Box<dyn FnOnce() + Send>>>,
    }
    impl CallbackExecutor for RetiringExecutor {
        fn ensure_ready(&self) -> Result<()> {
            Ok(())
        }
        fn submit(&self, job: Box<dyn FnOnce() + Send>) -> Result<()> {
            let old = lock(&self.job).replace(job);
            drop(old);
            Ok(())
        }
    }

    #[test]
    fn accepted_state_job_retired_without_execution_ends_subscription(
    ) -> std::result::Result<(), crate::BoxError> {
        let executor = Arc::new(RetiringExecutor::default());
        let (_publisher, source) = StateSource::new(1, executor.clone(), 1)?;
        let subscription = source.subscribe()?.into_callback(|_, _| {})?;
        let queued = lock(&executor.job).take();
        drop(queued);
        if subscription.is_active() {
            return Err("retired job left a subscription waiting forever".into());
        }
        let error = futures::executor::block_on(subscription.close())
            .err()
            .ok_or("retired job lost its dispatch failure")?;
        if error.kind() != ErrorKind::RuntimeUnavailable
            || error.context().stage != Some(crate::error::ErrorStage::Dispatch)
        {
            return Err(format!("incorrect retired job error: {error:?}").into());
        }
        drop(source.subscribe()?);
        Ok(())
    }

    #[test]
    fn completion_guard_retires_running_callback_after_caught_runner_fault(
    ) -> std::result::Result<(), crate::BoxError> {
        let (_publisher, source) = StateSource::new(1, Arc::new(HeldExecutor::default()), 1)?;
        let receiver = source.subscribe()?;
        let registration = receiver.registration.clone();
        let subscription = receiver.into_callback(|_, _| {})?;
        {
            let mut state = lock(&registration.state);
            state.running = true;
        }
        {
            let _completion = RunnerCompletion {
                registration: &registration,
                armed: true,
            };
            // Feed the outer catch boundary's error branch without generating an actual panic.
            registration.complete_runner(Err(Box::new("captured runner fault")));
        }
        let mut cx = Context::from_waker(futures::task::noop_waker_ref());
        if !matches!(registration.poll_close(&mut cx), Poll::Ready(Err(error)) if error.kind() == ErrorKind::CallbackPanicked)
        {
            return Err("runner fault left close pending or lost its error".into());
        }
        if subscription.is_active() {
            return Err("runner fault left delivery active".into());
        }
        if futures::executor::block_on(subscription.close()).is_ok() {
            return Err("close swallowed runner fault".into());
        }
        Ok(())
    }
}