open-net 0.3.2

A cross-platform async networking library with WebSocket clients and network status monitoring.
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
//! Admission-time reservations for native task observations.
//!
//! The existing event receiver owns delivery and callback execution. This layer
//! reserves every terminal delivery before operation admission and shares the
//! payload charge across recipients.

use super::listener_executor::ListenerExecutor;
use crate::common::log::log_def::LogType;
use crate::error::{ErrorKind, ErrorStage};
use crate::subscription::{
    event_channel, EventOverflow, EventPublisher, EventQueueLimit, EventResources,
};
use crate::ws::{
    DispatchLimits, MessageLane, SubscriptionId, TaskEvent, TaskEventOptions, TaskEvents,
};
use crate::{NetError, Result};
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::time::Instant;
use tokio::sync::{OwnedSemaphorePermit, Semaphore, TryAcquireError};
use tokio_util::sync::CancellationToken;

pub(crate) struct NativeTaskObserver {
    state: Mutex<ObserverState>,
    slots: Arc<Semaphore>,
    global: Arc<Capacity>,
    executor: Arc<ListenerExecutor>,
    closed: CancellationToken,
}

struct ObserverState {
    closed: bool,
    registrations: BTreeMap<SubscriptionId, Arc<Registration>>,
}

struct Capacity {
    items: Arc<Semaphore>,
    bytes: Arc<Semaphore>,
    max_items: usize,
    max_bytes: usize,
}

struct Registration {
    publisher: EventPublisher<TaskEvent>,
    ordinary: Capacity,
    urgent: Option<Capacity>,
    state: Mutex<RegistrationState>,
}

struct RegistrationState {
    closed: bool,
    next_id: u64,
    reservations: BTreeMap<u64, Weak<Mutex<Option<DeliveryCapacity>>>>,
}

struct DeliveryCapacity {
    item: OwnedSemaphorePermit,
    bytes: OwnedSemaphorePermit,
    global_item: OwnedSemaphorePermit,
    shared_bytes: Arc<OwnedSemaphorePermit>,
}

/// A fixed, ordered set of recipients captured before waiting for admission.
pub(crate) struct TaskObserverSnapshot {
    registrations: Vec<Arc<Registration>>,
    global: Arc<Capacity>,
    closed: CancellationToken,
}

/// Kept only by an unfinished operation; consuming publication is at most once.
pub(crate) struct TaskEventReservation {
    deliveries: Vec<ReservedDelivery>,
    payload_bytes: usize,
    lane: MessageLane,
}

struct ReservedDelivery {
    registration: Arc<Registration>,
    id: u64,
    capacity: Arc<Mutex<Option<DeliveryCapacity>>>,
}

impl NativeTaskObserver {
    /// Reuses the client's task ListenerExecutor; never creates a thread pool.
    pub(crate) fn new(
        executor: Arc<ListenerExecutor>,
        limits: &DispatchLimits,
    ) -> Result<Arc<Self>> {
        limits.validate()?;
        Ok(Arc::new(Self {
            state: Mutex::new(ObserverState {
                closed: false,
                registrations: BTreeMap::new(),
            }),
            slots: Arc::new(Semaphore::new(limits.task_subscriptions)),
            global: Arc::new(Capacity::new(
                limits.task_events.max_items,
                limits.task_events.max_bytes,
            )),
            executor,
            closed: CancellationToken::new(),
        }))
    }

    /// Isolate recipients per session while sharing the client's outstanding
    /// delivery and payload allowances, including unread older-session events.
    pub(crate) fn for_session(&self) -> Result<Arc<Self>> {
        if self.closed.is_cancelled() {
            return Err(closed_error());
        }
        Ok(Arc::new(Self {
            state: Mutex::new(ObserverState {
                closed: false,
                registrations: BTreeMap::new(),
            }),
            slots: self.slots.clone(),
            global: self.global.clone(),
            executor: self.executor.clone(),
            closed: CancellationToken::new(),
        }))
    }

    pub(crate) fn subscribe(&self, options: TaskEventOptions) -> Result<TaskEvents> {
        options.validate()?;
        self.prune_detached();
        if self.closed.is_cancelled() {
            return Err(closed_error());
        }
        let slot = self.slots.clone().try_acquire_owned().map_err(|_| {
            NetError::from(ErrorKind::SubscriptionLimitReached).with_stage(ErrorStage::Dispatch)
        })?;
        let (publisher, receiver) = event_channel(
            EventQueueLimit {
                max_items: options.max_tasks,
                max_bytes: options.max_payload_bytes,
            },
            EventOverflow::Wait,
            self.executor.clone(),
            Some(slot),
        )?;
        let (ordinary, urgent) = capacity_lanes(&options)?;
        let registration = Arc::new(Registration {
            publisher,
            ordinary,
            urgent,
            state: Mutex::new(RegistrationState {
                closed: false,
                next_id: 0,
                reservations: BTreeMap::new(),
            }),
        });
        let detached = Arc::downgrade(&registration);
        registration
            .publisher
            .on_receiver_closed(Arc::new(move || {
                if let Some(registration) = detached.upgrade() {
                    registration.release_detached();
                }
            }))?;
        let mut state = lock(&self.state);
        if state.closed {
            drop(state);
            return Err(closed_error());
        }
        state.registrations.insert(receiver.id(), registration);
        Ok(receiver)
    }

    pub(crate) fn snapshot(&self) -> Result<TaskObserverSnapshot> {
        self.prune_detached();
        let state = lock(&self.state);
        if state.closed {
            return Err(closed_error());
        }
        Ok(TaskObserverSnapshot {
            registrations: state.registrations.values().cloned().collect(),
            global: self.global.clone(),
            closed: self.closed.clone(),
        })
    }

    /// Freeze new admission; previously reserved terminals still drain to EOF.
    pub(crate) fn close(&self) {
        let registrations = {
            let mut state = lock(&self.state);
            state.closed = true;
            std::mem::take(&mut state.registrations)
        };
        self.closed.cancel();
        for (_, registration) in registrations {
            registration.close();
        }
    }

    /// The client root retires the shared executor after publishing terminals.
    /// Session closure alone must not stop the pool shared by later sessions.
    pub(crate) fn close_executor(&self) {
        if let Err(error) = self.executor.close() {
            crate::log_e!(LogType::WSC; "task_executor_close", "error", format!("{error:?}"));
        }
    }

    fn prune_detached(&self) {
        let retired = {
            let mut state = lock(&self.state);
            let ids: Vec<_> = state
                .registrations
                .iter()
                .filter_map(|(id, registration)| {
                    (!registration.publisher.is_active()).then_some(*id)
                })
                .collect();
            ids.into_iter()
                .filter_map(|id| state.registrations.remove(&id))
                .collect::<Vec<_>>()
        };
        for registration in retired {
            registration.release_detached();
        }
    }
}

impl Drop for NativeTaskObserver {
    fn drop(&mut self) {
        self.close();
    }
}

impl TaskObserverSnapshot {
    pub(crate) fn try_reserve(
        &self,
        payload_bytes: usize,
        lane: MessageLane,
    ) -> Result<TaskEventReservation> {
        loop {
            let registrations = self.active();
            let result = self.try_reserve_active(&registrations, payload_bytes, lane);
            if result.is_err()
                && registrations
                    .iter()
                    .any(|registration| !registration.publisher.is_active())
                && !self.closed.is_cancelled()
            {
                // A detached recipient must not keep the frozen fanout larger
                // than its live set or make an otherwise valid admission fail.
                continue;
            }
            return result;
        }
    }

    fn try_reserve_active(
        &self,
        registrations: &[Arc<Registration>],
        payload_bytes: usize,
        lane: MessageLane,
    ) -> Result<TaskEventReservation> {
        if self.closed.is_cancelled() {
            return Err(closed_error());
        }
        let mut reservation = TaskEventReservation {
            deliveries: Vec::new(),
            payload_bytes,
            lane,
        };
        if registrations.is_empty() {
            return Ok(reservation);
        }
        let count = self.recipient_count(registrations.len())?;
        let bytes = self.global.checked_bytes(payload_bytes)?;
        for registration in registrations {
            registration.lane(lane).checked_bytes(payload_bytes)?;
        }
        let shared = Arc::new(
            self.global
                .bytes
                .clone()
                .try_acquire_many_owned(bytes)
                .map_err(capacity_error)?,
        );
        let mut global_items = self
            .global
            .items
            .clone()
            .try_acquire_many_owned(count)
            .map_err(capacity_error)?;
        for registration in registrations {
            let capacity = registration.lane(lane);
            let item = capacity
                .items
                .clone()
                .try_acquire_owned()
                .map_err(capacity_error)?;
            let bytes = capacity
                .bytes
                .clone()
                .try_acquire_many_owned(capacity.checked_bytes(payload_bytes)?)
                .map_err(capacity_error)?;
            let global_item = global_items.split(1).ok_or_else(internal_error)?;
            if let Some(delivery) = registration.retain(DeliveryCapacity {
                item,
                bytes,
                global_item,
                shared_bytes: shared.clone(),
            })? {
                reservation.deliveries.push(delivery);
            }
        }
        if self.closed.is_cancelled() {
            return Err(closed_error());
        }
        Ok(reservation)
    }

    /// The caller owns the deadline wakeup on the client worker runtime; this
    /// future only checks expiry and therefore supports arbitrary executors.
    pub(crate) async fn reserve(
        &self,
        payload_bytes: usize,
        lane: MessageLane,
        cancel: &CancellationToken,
        deadline: Option<Instant>,
    ) -> Result<TaskEventReservation> {
        loop {
            if cancel.is_cancelled() {
                return Err(NetError::from(ErrorKind::Cancelled));
            }
            if self.closed.is_cancelled() {
                return Err(closed_error());
            }
            if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
                return Err(NetError::from(ErrorKind::TimedOut));
            }
            let registrations = self.active();
            if registrations.is_empty() {
                return Ok(TaskEventReservation {
                    deliveries: Vec::new(),
                    payload_bytes,
                    lane,
                });
            }
            let detached = futures_util::future::select_all(
                registrations
                    .iter()
                    .map(|registration| Box::pin(registration.publisher.receiver_closed())),
            );
            let reservation = async {
                let count = self.recipient_count(registrations.len())?;
                let bytes = self.global.checked_bytes(payload_bytes)?;
                for registration in &registrations {
                    registration.lane(lane).checked_bytes(payload_bytes)?;
                }
                let shared = Arc::new(
                    self.global
                        .bytes
                        .clone()
                        .acquire_many_owned(bytes)
                        .await
                        .map_err(|_| closed_error())?,
                );
                let mut global_items = self
                    .global
                    .items
                    .clone()
                    .acquire_many_owned(count)
                    .await
                    .map_err(|_| closed_error())?;
                let mut deliveries = Vec::new();
                for registration in &registrations {
                    let capacity = registration.lane(lane);
                    let item = capacity
                        .items
                        .clone()
                        .acquire_owned()
                        .await
                        .map_err(|_| closed_error())?;
                    let bytes = capacity
                        .bytes
                        .clone()
                        .acquire_many_owned(capacity.checked_bytes(payload_bytes)?)
                        .await
                        .map_err(|_| closed_error())?;
                    let global_item = global_items.split(1).ok_or_else(internal_error)?;
                    if let Some(delivery) = registration.retain(DeliveryCapacity {
                        item,
                        bytes,
                        global_item,
                        shared_bytes: shared.clone(),
                    })? {
                        deliveries.push(delivery);
                    }
                }
                Ok::<_, NetError>(TaskEventReservation {
                    deliveries,
                    payload_bytes,
                    lane,
                })
            };
            tokio::select! {
                biased;
                _ = cancel.cancelled() => return Err(NetError::from(ErrorKind::Cancelled)),
                _ = self.closed.cancelled() => return Err(closed_error()),
                _ = detached => continue,
                result = reservation => return result,
            }
        }
    }

    fn active(&self) -> Vec<Arc<Registration>> {
        self.registrations
            .iter()
            .filter_map(|registration| {
                if registration.publisher.is_active() {
                    Some(registration.clone())
                } else {
                    registration.release_detached();
                    None
                }
            })
            .collect()
    }

    fn recipient_count(&self, count: usize) -> Result<u32> {
        if count > self.global.max_items {
            return Err(NetError::from(ErrorKind::QueueFull));
        }
        u32::try_from(count).map_err(|_| NetError::from(ErrorKind::QueueFull))
    }
}

impl TaskEventReservation {
    /// Call only after selecting the terminal winner and releasing operation locks.
    pub(crate) fn publish(self, event: TaskEvent) -> Result<()> {
        if event.lane != self.lane || event.source.payload_bytes()? > self.payload_bytes {
            return Err(NetError::input(
                "task_event.source",
                "terminal event differs from its admission reservation",
            ));
        }
        let mut failure = None;
        for delivery in self.deliveries {
            if let Err(error) = delivery.publish(event.clone(), self.payload_bytes.max(1)) {
                crate::log_e!(LogType::WSC; "ws.task_event_reservation.publish", "arg_event_session_id|arg_event_operation_id|arg_event_lane|kind|stage", event.session_id.as_u64(), event.operation_id.as_u64(), format!("{:?}", event.lane), format!("{:?}", error.kind()), error.context().stage.map(|stage| format!("{stage:?}")));
                if failure.is_none() {
                    failure = Some(error);
                }
            }
        }
        failure.map_or(Ok(()), Err)
    }
}

impl Registration {
    fn lane(&self, lane: MessageLane) -> &Capacity {
        match (lane, &self.urgent) {
            (MessageLane::Urgent, Some(urgent)) => urgent,
            _ => &self.ordinary,
        }
    }

    fn retain(self: &Arc<Self>, capacity: DeliveryCapacity) -> Result<Option<ReservedDelivery>> {
        let capacity = Arc::new(Mutex::new(Some(capacity)));
        let mut state = lock(&self.state);
        if state.closed || !self.publisher.is_active() {
            drop(state);
            return Ok(None);
        }
        let id = state.next_id;
        state.next_id = id
            .checked_add(1)
            .ok_or_else(|| NetError::from(ErrorKind::ResourceExhausted))?;
        state.reservations.insert(id, Arc::downgrade(&capacity));
        Ok(Some(ReservedDelivery {
            registration: self.clone(),
            id,
            capacity,
        }))
    }

    fn release_detached(&self) {
        let retired = {
            let mut state = lock(&self.state);
            state.closed = true;
            std::mem::take(&mut state.reservations)
        };
        for (_, reservation) in retired {
            if let Some(capacity) = reservation.upgrade() {
                let released = lock(&capacity).take();
                drop(released);
            }
        }
    }

    fn close(&self) {
        let finish = {
            let mut state = lock(&self.state);
            state.closed = true;
            state.reservations.is_empty()
        };
        if finish {
            finish_publisher(&self.publisher);
        }
    }
}

impl ReservedDelivery {
    fn publish(self, event: TaskEvent, payload_bytes: usize) -> Result<()> {
        let arg_event_session_id = event.session_id.as_u64();
        let arg_event_operation_id = event.operation_id.as_u64();
        let arg_event_lane = event.lane;
        let registration = self.registration.clone();
        let (publication, finish) = {
            let mut state = lock(&registration.state);
            state.reservations.remove(&self.id);
            let capacity = lock(&self.capacity).take();
            let publication = if let Some(capacity) = capacity {
                Some(registration.publisher.prepare_try_publish_with_retained(
                    event,
                    payload_bytes,
                    EventResources {
                        items: Some(capacity.item),
                        bytes: Some(capacity.bytes),
                        shared_bytes: Some(capacity.shared_bytes),
                    },
                    Box::new(capacity.global_item),
                ))
            } else {
                None
            };
            (publication, state.closed && state.reservations.is_empty())
        };
        let result = match publication {
            Some(publication) => publication.dispatch(),
            None => Ok(()),
        };
        if finish {
            finish_publisher(&registration.publisher);
        }
        if let Err(error) = result {
            if error.kind() == ErrorKind::Closed && !registration.publisher.is_active() {
                return Ok(());
            }
            if let Err(delivery_error) = registration.publisher.fail(error.clone()) {
                crate::log_e!(LogType::WSC; "ws.reserved_delivery.publish", "arg_event_session_id|arg_event_operation_id|arg_event_lane|payload_bytes|kind|stage", arg_event_session_id, arg_event_operation_id, format!("{arg_event_lane:?}"), payload_bytes, format!("{:?}", delivery_error.kind()), delivery_error.context().stage.map(|stage| format!("{stage:?}")));
            }
            return Err(error);
        }
        Ok(())
    }
}

impl Drop for ReservedDelivery {
    fn drop(&mut self) {
        let finish = {
            let registration = self.registration.clone();
            let mut state = lock(&registration.state);
            state.reservations.remove(&self.id);
            let finish = state.closed && state.reservations.is_empty();
            drop(state);
            finish.then_some(registration)
        };
        let released = lock(&self.capacity).take();
        drop(released);
        if let Some(registration) = finish {
            finish_publisher(&registration.publisher);
        }
    }
}

impl Capacity {
    fn new(max_items: usize, max_bytes: usize) -> Self {
        Self {
            items: Arc::new(Semaphore::new(max_items)),
            bytes: Arc::new(Semaphore::new(max_bytes)),
            max_items,
            max_bytes,
        }
    }

    fn checked_bytes(&self, bytes: usize) -> Result<u32> {
        let bytes = bytes.max(1);
        if bytes > self.max_bytes {
            return Err(NetError::from(ErrorKind::ItemTooLarge));
        }
        u32::try_from(bytes).map_err(|_| NetError::from(ErrorKind::ItemTooLarge))
    }
}

fn capacity_lanes(options: &TaskEventOptions) -> Result<(Capacity, Option<Capacity>)> {
    let Some(urgent) = &options.urgent_reserve else {
        return Ok((
            Capacity::new(options.max_tasks, options.max_payload_bytes),
            None,
        ));
    };
    let ordinary_items = options
        .max_tasks
        .checked_sub(urgent.max_items)
        .ok_or_else(internal_error)?;
    let ordinary_bytes = options
        .max_payload_bytes
        .checked_sub(urgent.max_bytes)
        .ok_or_else(internal_error)?;
    Ok((
        Capacity::new(ordinary_items, ordinary_bytes),
        Some(Capacity::new(urgent.max_items, urgent.max_bytes)),
    ))
}

fn capacity_error(error: TryAcquireError) -> NetError {
    match error {
        TryAcquireError::Closed => closed_error(),
        TryAcquireError::NoPermits => NetError::from(ErrorKind::QueueFull),
    }
}

fn closed_error() -> NetError {
    NetError::from(ErrorKind::QueueClosed).with_stage(ErrorStage::Dispatch)
}

fn internal_error() -> NetError {
    NetError::from(ErrorKind::Internal).with_stage(ErrorStage::Dispatch)
}

fn finish_publisher(publisher: &EventPublisher<TaskEvent>) {
    if let Err(error) = publisher.finish() {
        if error.kind() != ErrorKind::Closed || publisher.is_active() {
            crate::log_e!(LogType::WSC; "ws.native_task_observer.finish_publisher", "arg_publisher_present|kind|stage", true, format!("{:?}", error.kind()), error.context().stage.map(|stage| format!("{stage:?}")));
        }
    }
}

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    match mutex.lock() {
        Ok(state) => state,
        Err(poisoned) => {
            crate::log_e!(LogType::WSC; "ws.native_task_observer.lock", "arg_mutex_present|error", true, "lock_poisoned_recovered");
            poisoned.into_inner()
        }
    }
}