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
//! [`Stats`]: what one connection has done, counted as it happens.
/// Counters and gauges for one connection: traffic both ways, the outcomes of
/// our requests, and what is outstanding now.
///
/// The endpoint keeps them as plain integers under the lock it takes for
/// every message anyway, so counting costs a few additions per message and
/// no atomics; [`Endpoint::stats`](crate::Endpoint::stats) and
/// [`Peer::stats`](crate::Peer::stats) take a snapshot. Counters start at 0
/// and only grow (they wrap after 2^64); gauges are the value at the
/// snapshot.
///
/// | Field | Kind | Counts | Normal | Worth a look |
/// |---|---|---|---|---|
/// | `bodies_in`, `bytes_in` | counter | message bodies handled (a batch counts once) and their bytes, framing excluded | grows with traffic | `bytes_in / bodies_in` near `max_message_bytes` |
/// | `requests_in`, `notifications_in`, `responses_in` | counter | messages from the peer, batch elements included | | |
/// | `invalid_in` | counter | malformed messages and messages skipped as oversized | 0 | any growth: a broken or hostile peer |
/// | `bodies_out`, `bytes_out` | counter | message bodies sent (a batch counts once) and their bytes | | |
/// | `requests_out`, `notifications_out`, `responses_out` | counter | messages sent, batch elements included | | |
/// | `errors_out` | counter | error responses sent, of `responses_out` | low | a large share: handlers failing |
/// | `timed_out`, `evicted`, `cancelled_out` | counter | our requests that ended so | 0 | timeouts: a slow or stuck peer; evictions: raise `max_pending` |
/// | `cancelled_in` | counter | the peer's requests it cancelled | | |
/// | `panics` | counter | handler panics contained | 0 | any: a bug in a handler |
/// | `pending` | gauge | our requests awaiting an answer | small | near `max_pending` |
/// | `in_flight` | gauge | deferred requests being answered | | near `max_in_flight` |
/// | `held` | gauge | messages held until the gate opens | 0 once open | |
///
/// # Examples
///
/// ```
/// use rpc_io::{Endpoint, Router};
///
/// let router = Router::new().request("ping", |_: &mut (), _, _| Ok("pong"));
/// let (tx, _rx) = std::sync::mpsc::channel::<String>();
/// let mut endpoint = Endpoint::new(tx, router, ());
/// endpoint.handle(br#"[{"jsonrpc":"2.0","id":1,"method":"ping"},{"jsonrpc":"2.0","id":2,"method":"nope"}]"#)?;
/// let stats = endpoint.stats();
/// assert_eq!((stats.bodies_in, stats.requests_in), (1, 2));
/// assert_eq!((stats.bodies_out, stats.responses_out, stats.errors_out), (1, 2, 1));
/// # Ok::<(), rpc_io::Error>(())
/// ```
/// What one incoming body held, counted before its turn.
pub
/// Adds one to a counter.
pub