-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectivity.mbt
More file actions
80 lines (69 loc) · 2.2 KB
/
Copy pathconnectivity.mbt
File metadata and controls
80 lines (69 loc) · 2.2 KB
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
// The gRPC sub-connection connectivity state machine (← gRPC `connectivity.State`): each address the
// Channel dials is a `SubConn` that moves through IDLE → CONNECTING → READY, drops to
// TRANSIENT_FAILURE on error (then retries via CONNECTING after backoff), and ends at SHUTDOWN. The
// load balancer picks among the sub-connections that are READY; the connection pool holds them. This
// is the pure state core — the Channel calls the transition methods as real socket events happen.
///|
/// A sub-connection's connectivity state (← gRPC `connectivity.State`).
pub(all) enum ConnectivityState {
Idle
Connecting
Ready
TransientFailure
Shutdown
} derive(Eq, Debug)
///|
/// One address the Channel dials, tracking its connectivity state.
pub struct SubConn {
address : String
mut state : ConnectivityState
}
///|
/// A fresh sub-connection to `address`, starting IDLE.
pub fn SubConn::new(address : String) -> SubConn {
{ address, state: Idle, }
}
///|
/// Begin connecting (IDLE or TRANSIENT_FAILURE → CONNECTING). A no-op once SHUTDOWN or already
/// connecting/ready.
pub fn SubConn::connect(self : SubConn) -> Unit {
match self.state {
Idle | TransientFailure => self.state = Connecting
_ => ()
}
}
///|
/// The connection attempt succeeded (CONNECTING → READY).
pub fn SubConn::on_connected(self : SubConn) -> Unit {
if self.state is Connecting {
self.state = Ready
}
}
///|
/// A connection attempt or an established connection failed (→ TRANSIENT_FAILURE), unless shut down.
pub fn SubConn::on_failure(self : SubConn) -> Unit {
if self.state != Shutdown {
self.state = TransientFailure
}
}
///|
/// A READY connection went idle (READY → IDLE).
pub fn SubConn::on_idle(self : SubConn) -> Unit {
if self.state is Ready {
self.state = Idle
}
}
///|
/// Shut the sub-connection down permanently (→ SHUTDOWN, a terminal state).
pub fn SubConn::shutdown(self : SubConn) -> Unit {
self.state = Shutdown
}
///|
/// Whether this sub-connection can carry RPCs right now.
pub fn SubConn::is_ready(self : SubConn) -> Bool {
self.state is Ready
}
///|
pub extend ConnectivityState with Debug::{to_repr}
///|
pub extend ConnectivityState with Eq::{not_equal, equal}