node: Move `address` field out of `State`
We move the session address to the top-level struct, since it's needed in a bunch of places. This change is required for the rate-limiting code that is coming next.
This commit is contained in:
parent
66799e2211
commit
260b1a428c
|
|
@ -191,18 +191,18 @@ pub fn sessions(node: &Node) -> Result<Option<term::Table<4, term::Label>>, node
|
||||||
term::Label::from(term::format::dim("initial")),
|
term::Label::from(term::format::dim("initial")),
|
||||||
term::Label::blank(),
|
term::Label::blank(),
|
||||||
),
|
),
|
||||||
node::State::Attempted { addr } => (
|
node::State::Attempted => (
|
||||||
addr.to_string().into(),
|
sess.addr.to_string().into(),
|
||||||
term::Label::from(term::format::tertiary("attempted")),
|
term::Label::from(term::format::tertiary("attempted")),
|
||||||
term::Label::blank(),
|
term::Label::blank(),
|
||||||
),
|
),
|
||||||
node::State::Connected { addr, since, .. } => (
|
node::State::Connected { since, .. } => (
|
||||||
addr.to_string().into(),
|
sess.addr.to_string().into(),
|
||||||
term::Label::from(term::format::positive("connected")),
|
term::Label::from(term::format::positive("connected")),
|
||||||
term::format::dim(now - since).into(),
|
term::format::dim(now - since).into(),
|
||||||
),
|
),
|
||||||
node::State::Disconnected { retry_at, .. } => (
|
node::State::Disconnected { retry_at, .. } => (
|
||||||
term::Label::blank(),
|
sess.addr.to_string().into(),
|
||||||
term::Label::from(term::format::negative("disconnected")),
|
term::Label::from(term::format::negative("disconnected")),
|
||||||
term::format::dim(retry_at - now).into(),
|
term::format::dim(retry_at - now).into(),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -221,6 +221,7 @@ impl radicle::node::Handle for Handle {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(nid, s)| radicle::node::Session {
|
.map(|(nid, s)| radicle::node::Session {
|
||||||
nid: *nid,
|
nid: *nid,
|
||||||
|
addr: s.addr.clone(),
|
||||||
state: s.state.clone(),
|
state: s.state.clone(),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
||||||
|
|
@ -718,7 +718,7 @@ where
|
||||||
debug!(target: "service", "Attempted connection to {nid} ({addr})");
|
debug!(target: "service", "Attempted connection to {nid} ({addr})");
|
||||||
|
|
||||||
if let Some(sess) = self.sessions.get_mut(&nid) {
|
if let Some(sess) = self.sessions.get_mut(&nid) {
|
||||||
sess.to_attempted(addr);
|
sess.to_attempted();
|
||||||
} else {
|
} else {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
panic!("Service::attempted: unknown session {nid}@{addr}");
|
panic!("Service::attempted: unknown session {nid}@{addr}");
|
||||||
|
|
@ -730,13 +730,14 @@ where
|
||||||
self.emitter.emit(Event::PeerConnected { nid: remote });
|
self.emitter.emit(Event::PeerConnected { nid: remote });
|
||||||
|
|
||||||
let msgs = self.initial(link);
|
let msgs = self.initial(link);
|
||||||
|
let now = self.time();
|
||||||
|
|
||||||
if link.is_outbound() {
|
if link.is_outbound() {
|
||||||
if let Some(peer) = self.sessions.get_mut(&remote) {
|
if let Some(peer) = self.sessions.get_mut(&remote) {
|
||||||
let attempted = peer.to_connected(self.clock);
|
peer.to_connected(self.clock);
|
||||||
self.outbox.write_all(peer, msgs);
|
self.outbox.write_all(peer, msgs);
|
||||||
|
|
||||||
if let Err(e) = self.addresses.connected(&remote, &attempted, self.time()) {
|
if let Err(e) = self.addresses.connected(&remote, &peer.addr, now) {
|
||||||
error!(target: "service", "Error updating address book with connection: {e}");
|
error!(target: "service", "Error updating address book with connection: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -811,9 +812,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn received_message(&mut self, remote: NodeId, message: Message) {
|
pub fn received_message(&mut self, remote: NodeId, message: Message) {
|
||||||
match self.handle_message(&remote, message) {
|
if let Err(err) = self.handle_message(&remote, message) {
|
||||||
Ok(_) => {}
|
|
||||||
Err(err) => {
|
|
||||||
// If there's an error, stop processing messages from this peer.
|
// If there's an error, stop processing messages from this peer.
|
||||||
// However, we still relay messages returned up to this point.
|
// However, we still relay messages returned up to this point.
|
||||||
self.outbox
|
self.outbox
|
||||||
|
|
@ -823,7 +822,6 @@ where
|
||||||
// process further messages.
|
// process further messages.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle an announcement message.
|
/// Handle an announcement message.
|
||||||
///
|
///
|
||||||
|
|
@ -1106,9 +1104,9 @@ where
|
||||||
|
|
||||||
match (&mut peer.state, message) {
|
match (&mut peer.state, message) {
|
||||||
// Process a peer announcement.
|
// Process a peer announcement.
|
||||||
(session::State::Connected { addr, .. }, Message::Announcement(ann)) => {
|
(session::State::Connected { .. }, Message::Announcement(ann)) => {
|
||||||
let relayer = peer.id;
|
let relayer = peer.id;
|
||||||
let relayer_addr = addr.clone();
|
let relayer_addr = peer.addr.clone();
|
||||||
let announcer = ann.node;
|
let announcer = ann.node;
|
||||||
|
|
||||||
// Returning true here means that the message should be relayed.
|
// Returning true here means that the message should be relayed.
|
||||||
|
|
@ -1320,6 +1318,7 @@ where
|
||||||
nid,
|
nid,
|
||||||
Session::outbound(
|
Session::outbound(
|
||||||
nid,
|
nid,
|
||||||
|
addr.clone(),
|
||||||
persistent,
|
persistent,
|
||||||
self.rng.clone(),
|
self.rng.clone(),
|
||||||
self.config.limits.clone(),
|
self.config.limits.clone(),
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,8 @@ impl Error {
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
/// Peer id.
|
/// Peer id.
|
||||||
pub id: NodeId,
|
pub id: NodeId,
|
||||||
|
/// Peer address.
|
||||||
|
pub addr: Address,
|
||||||
/// Connection direction.
|
/// Connection direction.
|
||||||
pub link: Link,
|
pub link: Link,
|
||||||
/// Whether we should attempt to re-connect
|
/// Whether we should attempt to re-connect
|
||||||
|
|
@ -101,9 +103,10 @@ impl fmt::Display for Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Session {
|
impl Session {
|
||||||
pub fn outbound(id: NodeId, persistent: bool, rng: Rng, limits: Limits) -> Self {
|
pub fn outbound(id: NodeId, addr: Address, persistent: bool, rng: Rng, limits: Limits) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
|
addr,
|
||||||
state: State::Initial,
|
state: State::Initial,
|
||||||
link: Link::Outbound,
|
link: Link::Outbound,
|
||||||
subscribe: None,
|
subscribe: None,
|
||||||
|
|
@ -126,8 +129,8 @@ impl Session {
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
state: State::Connected {
|
|
||||||
addr,
|
addr,
|
||||||
|
state: State::Connected {
|
||||||
since: time,
|
since: time,
|
||||||
ping: PingState::default(),
|
ping: PingState::default(),
|
||||||
fetching: HashSet::default(),
|
fetching: HashSet::default(),
|
||||||
|
|
@ -193,30 +196,26 @@ impl Session {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_attempted(&mut self, addr: Address) {
|
pub fn to_attempted(&mut self) {
|
||||||
assert!(
|
assert!(
|
||||||
self.is_initial(),
|
self.is_initial(),
|
||||||
"Can only transition to 'attempted' state from 'initial' state"
|
"Can only transition to 'attempted' state from 'initial' state"
|
||||||
);
|
);
|
||||||
self.state = State::Attempted { addr };
|
self.state = State::Attempted;
|
||||||
self.attempts += 1;
|
self.attempts += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_connected(&mut self, since: LocalTime) -> Address {
|
pub fn to_connected(&mut self, since: LocalTime) {
|
||||||
self.attempts = 0;
|
self.attempts = 0;
|
||||||
|
|
||||||
let addr = if let State::Attempted { addr } = &self.state {
|
let State::Attempted = &self.state else {
|
||||||
addr.clone()
|
|
||||||
} else {
|
|
||||||
panic!("Session::to_connected: can only transition to 'connected' state from 'attempted' state");
|
panic!("Session::to_connected: can only transition to 'connected' state from 'attempted' state");
|
||||||
};
|
};
|
||||||
self.state = State::Connected {
|
self.state = State::Connected {
|
||||||
addr: addr.clone(),
|
|
||||||
since,
|
since,
|
||||||
ping: PingState::default(),
|
ping: PingState::default(),
|
||||||
fetching: HashSet::default(),
|
fetching: HashSet::default(),
|
||||||
};
|
};
|
||||||
addr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move the session state to "disconnected". Returns any pending RID
|
/// Move the session state to "disconnected". Returns any pending RID
|
||||||
|
|
|
||||||
|
|
@ -72,11 +72,9 @@ pub enum State {
|
||||||
/// Initial state for outgoing connections.
|
/// Initial state for outgoing connections.
|
||||||
Initial,
|
Initial,
|
||||||
/// Connection attempted successfully.
|
/// Connection attempted successfully.
|
||||||
Attempted { addr: Address },
|
Attempted,
|
||||||
/// Initial state after handshake protocol hand-off.
|
/// Initial state after handshake protocol hand-off.
|
||||||
Connected {
|
Connected {
|
||||||
/// Remote address.
|
|
||||||
addr: Address,
|
|
||||||
/// Connected since this time.
|
/// Connected since this time.
|
||||||
since: LocalTime,
|
since: LocalTime,
|
||||||
/// Ping state.
|
/// Ping state.
|
||||||
|
|
@ -375,6 +373,7 @@ impl Command {
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
pub nid: NodeId,
|
pub nid: NodeId,
|
||||||
|
pub addr: Address,
|
||||||
pub state: State,
|
pub state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue