Skip to content

Commit d54a19d

Browse files
committed
chore: enrich logging message for ConnectionLoss
`Error::ConnectionLoss` itself carries no cause, thus less useful.
1 parent 1d49afa commit d54a19d

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/session/mod.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl Session {
215215
) {
216216
if let Err(err) = self.serve_session(endpoints, &mut conn, buf, depot, requester, unwatch_requester).await {
217217
self.resolve_serve_error(&err);
218-
info!("enter state {} due to error {}", self.session_state, err);
218+
info!("enter state {} due to {}", self.session_state, err);
219219
depot.error(&err);
220220
} else {
221221
self.change_state(SessionState::Disconnected);
@@ -356,12 +356,15 @@ impl Session {
356356
fn handle_connect_response(&mut self, mut body: &[u8]) -> Result<(), Error> {
357357
let response = record::unmarshal::<ConnectResponse>(&mut body)?;
358358
debug!("received connect response: {response:?}");
359-
if response.session_id == 0 {
359+
let session_id = SessionId(response.session_id);
360+
if session_id.0 == 0 {
360361
return Err(Error::SessionExpired);
361362
} else if !self.is_readonly_allowed() && response.readonly {
362-
return Err(Error::ConnectionLoss);
363+
return Err(Error::UnexpectedError(format!(
364+
"get readonly session {}, while is not allowed since readonly={}, old session readonly={}",
365+
session_id, self.readonly, self.session.readonly
366+
)));
363367
}
364-
let session_id = SessionId(response.session_id);
365368
if self.session.id != session_id {
366369
self.session.id = session_id;
367370
info!(
@@ -434,7 +437,7 @@ impl Session {
434437
},
435438
now = tick.tick() => {
436439
if now >= self.last_recv + self.connector.timeout() {
437-
return Err(Error::ConnectionLoss);
440+
return Err(Error::new_other(format!("no response from connection in {}ms", self.connector.timeout().as_millis()), None));
438441
}
439442
},
440443
}
@@ -462,14 +465,14 @@ impl Session {
462465
if self.session.readonly { Some(self.connector.clone().seek_for_writable(endpoints)) } else { None };
463466
let mut tick = time::interval(self.tick_timeout);
464467
tick.set_missed_tick_behavior(time::MissedTickBehavior::Skip);
465-
let mut channel_closed = false;
468+
let mut err = None;
466469
let mut channel_halted = false;
467470
depot.start();
468471
while !(channel_halted && depot.is_empty() && !conn.wants_write()) {
469472
select! {
470473
Some(endpoint) = Self::poll(&mut seek_for_writable), if seek_for_writable.is_some() => {
471474
seek_for_writable = None;
472-
debug!("succeeds to contact writable server {}", endpoint);
475+
err = Some(Error::new_other(format!("encounter writable server {}", endpoint), None));
473476
channel_halted = true;
474477
},
475478
_ = conn.readable() => {
@@ -488,7 +491,6 @@ impl Session {
488491
depot.push_session(SessionOperation::new_without_body(OpCode::CloseSession));
489492
}
490493
channel_halted = true;
491-
channel_closed = true;
492494
continue;
493495
};
494496
depot.push_session(operation);
@@ -500,7 +502,7 @@ impl Session {
500502
},
501503
now = tick.tick() => {
502504
if now >= self.last_recv + self.connector.timeout() {
503-
return Err(Error::ConnectionLoss);
505+
return Err(Error::new_other(format!("no response from connection in {}ms", self.connector.timeout().as_millis()), None));
504506
}
505507
if self.last_ping.is_none() && now >= self.last_send + self.ping_timeout {
506508
self.send_ping(depot, now);
@@ -509,11 +511,7 @@ impl Session {
509511
},
510512
}
511513
}
512-
if channel_closed {
513-
Err(Error::ClientClosed)
514-
} else {
515-
Err(Error::ConnectionLoss)
516-
}
514+
Err(err.unwrap_or(Error::ClientClosed))
517515
}
518516

519517
fn send_ping(&mut self, depot: &mut Depot, now: Instant) {

0 commit comments

Comments
 (0)