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
//! [`Config`]: how an endpoint behaves, with [`Gate`] and [`Cancellation`].
use ;
use crate::;
/// An endpoint's settings.
///
/// | Setting | Default | Meaning |
/// |---|---|---|
/// | `limits` | [`Limits::new`] | budgets on incoming messages |
/// | `max_pending` | 1024 | our requests awaiting answers; one more evicts the oldest |
/// | `max_in_flight` | 1024 | deferred requests being answered; more are refused with `SERVER_OVERLOADED` |
/// | `request_timeout` | none | deadline for our requests that set none themselves |
/// | `cancellation` | [`Cancellation::lsp`] | the cancel notification, both ways; `None` turns cancellation off |
/// | `gate` | none | an opening request every other request must wait for |
///
/// `#[non_exhaustive]`: start from [`Config::new`] and chain `with_*`.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use rpc_io::{Cancellation, Config, Gate};
///
/// let lsp = Config::new()
/// .with_gate(Some(Gate::new("initialize").allow("exit")))
/// .with_request_timeout(Some(Duration::from_secs(30)));
/// let mcp = Config::new().with_cancellation(Some(Cancellation::mcp()));
/// assert_eq!(lsp.max_pending, 1024);
/// assert_eq!(mcp.cancellation.map(|c| c.id_member), Some("requestId".into()));
/// ```
/// How a protocol spells request cancellation: a notification naming the id
/// of the request to cancel.
///
/// Both directions use it. When the peer sends it for one of our deferred
/// requests, the request's [`CancelToken`](crate::CancelToken) is set and, if
/// `reply` is set, the request is answered at once with that error code (LSP:
/// a cancelled request still gets exactly one response); with `reply: None`
/// it gets no response at all (MCP: the receiver "SHOULD NOT send a response
/// for the cancelled request"), and a reply the handler sends later is
/// dropped. When one of our requests times out, is evicted, or is cancelled,
/// the peer is sent this notification.
///
/// The cancel notification is consumed by the endpoint; it never reaches the
/// [`Router`](crate::Router).
///
/// # Examples
///
/// ```
/// use rpc_io::{Cancellation, ErrorCode};
///
/// let lsp = Cancellation::lsp();
/// assert_eq!((lsp.method.as_ref(), lsp.id_member.as_ref()), ("$/cancelRequest", "id"));
/// assert_eq!(lsp.reply, Some(ErrorCode::REQUEST_CANCELLED));
///
/// // LexerSketch's API (specs/STUDIO.md ยง7.1): `$/cancel {id}`, error 1005.
/// let studio = Cancellation::new("$/cancel", "id", Some(ErrorCode(1005)));
/// assert_eq!(studio.reply, Some(ErrorCode(1005)));
/// ```
/// An opening request that must succeed before anything else is served (LSP's
/// `initialize`, MCP's `initialize`, LexerSketch's `auth`).
///
/// Until the opening request has been answered successfully:
///
/// - other requests are answered with `code` (default
/// [`SERVER_NOT_INITIALIZED`](ErrorCode::SERVER_NOT_INITIALIZED));
/// - notifications are dropped (reported as [`Event::Dropped`](crate::Event)),
/// except the methods in `allow` (LSP's `exit`);
/// - everything this side sends other than responses (notifications and
/// requests, from handlers or other threads) is held back, then sent in
/// order right after the opening response; if the opening request fails,
/// what was held is discarded and held requests end with
/// [`CallError::Cancelled`](crate::CallError::Cancelled). A held request
/// that times out or is cancelled is simply never sent, and an "answer" to
/// one is treated as unmatched: the peer has never seen it.
///
/// The opening request fails when it is answered with an error, and also when
/// the peer cancels it under a [`Cancellation`] that sends no reply; the gate
/// then shuts again and a new opening request is served. Afterwards (once it
/// succeeded) a repeated opening request is answered with `INVALID_REQUEST`,
/// as is an opening request inside a batch. A gate belongs on the side that
/// receives the opening request; on the side that sends it, it would hold that
/// side's own messages forever.
///
/// Held messages are kept in memory without a limit (only requests are
/// bounded, by [`Config::max_pending`]): send little before the gate opens.
///
/// # Examples
///
/// ```
/// use rpc_io::{ErrorCode, Gate};
///
/// let gate = Gate::new("initialize").allow("exit");
/// assert_eq!(gate.code, ErrorCode::SERVER_NOT_INITIALIZED);
/// assert_eq!(gate.allow, ["exit"]);
/// ```