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
//! `activity.subscribe` correlations reserved at ingress.
//!
//! The connection reader hands `activity.subscribe` to a task, because
//! opening a ledger subscription can wait on disk, and handles the next frame
//! itself. A `CANCEL` for the same correlation can therefore be handled before
//! the subscription is installed. The reader reserves the correlation before
//! handing the request off: a cancel that finds the reservation marks it
//! cancelled and settles the stream with its STREAM_END, and the subscribe
//! then installs nothing.
use super::*;
/// What has become of a reserved correlation's stream.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Stream {
/// Still being set up: a CANCEL may claim it.
Pending,
/// A CANCEL claimed it and sent the stream's one STREAM_END.
Cancelled,
/// The subscribe was refused, and its ERROR is the stream's one terminal.
Refused,
}
type Reserved = Arc<Mutex<HashMap<(ConnectionId, u64), Stream>>>;
/// Reserved correlations and what has become of each one's stream.
#[derive(Clone, Default)]
pub(super) struct ActivityReservations(Reserved);
/// Held by the task serving a reserved `activity.subscribe`. Dropping it
/// releases the reservation, whether the subscription was installed, refused,
/// cancelled, or never started because the connection closed first.
pub(crate) struct ActivityReservation {
reserved: Reserved,
key: (ConnectionId, u64),
}
impl Drop for ActivityReservation {
fn drop(&mut self) {
self.reserved
.lock()
.unwrap_or_else(|p| p.into_inner())
.remove(&self.key);
}
}
impl ActivityReservations {
/// Claim a pending reservation for a CANCEL. Returns false when there is
/// none or another CANCEL already claimed it, so the stream settles once.
/// The caller holds the activity stream lock, which a subscribe also holds
/// from its own check of [`Self::cancelled`] until it installs its feed.
pub(super) fn cancel(&self, key: (ConnectionId, u64)) -> bool {
let mut reserved = self.0.lock().unwrap_or_else(|p| p.into_inner());
match reserved.get_mut(&key) {
Some(stream) if *stream == Stream::Pending => {
*stream = Stream::Cancelled;
true
}
_ => false,
}
}
/// Settle a subscribe's refusal `replies` (empty when the subscription
/// was installed) against its reservation, so its correlation ends once:
/// a CANCEL that already claimed the reservation sent the STREAM_END, so
/// the refusal is dropped; otherwise the refusal ends the stream and a
/// later CANCEL finds nothing to cancel. A request without a reservation
/// keeps its replies.
pub(super) fn settle(&self, key: (ConnectionId, u64), replies: Vec<Frame>) -> Vec<Frame> {
if replies.is_empty() {
return replies;
}
let mut reserved = self.0.lock().unwrap_or_else(|p| p.into_inner());
match reserved.get_mut(&key) {
Some(Stream::Cancelled) => Vec::new(),
Some(stream) => {
*stream = Stream::Refused;
replies
}
None => replies,
}
}
/// Whether a CANCEL has claimed this correlation's reservation.
pub(super) fn cancelled(&self, key: (ConnectionId, u64)) -> bool {
self.0
.lock()
.unwrap_or_else(|p| p.into_inner())
.get(&key)
.is_some_and(|stream| *stream == Stream::Cancelled)
}
}
impl ControlHandler {
/// Reserve an `activity.subscribe` request's correlation before the reader
/// hands it to a task. `None` for any other frame, or when the correlation
/// is already reserved by an earlier request on this connection.
pub(crate) fn reserve_activity_subscription(
&self,
connection_id: ConnectionId,
frame: &Frame,
) -> Option<impl Send + 'static> {
let subscribe = frame.header.channel == 0
&& frame.header.ty == FrameType::Request
&& serde_json::from_slice::<ControlOpProbe>(&frame.body)
.is_ok_and(|probe| probe.op == ops::ACTIVITY_SUBSCRIBE);
if !subscribe {
return None;
}
let key = (connection_id, frame.header.corr);
let reserved = &self.activity_reservations.0;
let mut map = reserved.lock().unwrap_or_else(|p| p.into_inner());
if map.contains_key(&key) {
return None;
}
map.insert(key, Stream::Pending);
Some(ActivityReservation {
reserved: Arc::clone(reserved),
key,
})
}
}