gateway: invisible to users, and tried again when away

Users never hear of the gateway:
- relayed messages look like everyone else's (no "via relay", no accent
  colour) and group with the author's other messages;
- a gateway that cannot be reached or drops is tried again by itself
  (after 2, 5, 15, 30, then every 60 s) with the open chats opened again
  and messages written meanwhile sent, instead of a WebSocket error
  ("connection refused") on screen; it is logged with its URL, once per
  cause;
- a server whose gateway is away is offline, not connecting, so it can
  always be joined;
- failed requests the app made by itself are quiet; what the user did and
  failed is told in plain words; query relay errors are logged;
- no status line about a found gateway; events and streams texts do not
  mention it.

The gateway is looked up once per run even when one is stored: what the
server publishes takes the place of a stored URL (one that moved, or one
typed into an older version that showed the field, which cannot be
edited any more).

The admin guide says a _tsgws record must point at TLS, not at tsgw's own
plain port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VZBWxxMsxUzp4PSTKVzbZN
This commit is contained in:
Claude committed 2026-10-05 20:09:59 +00:00
1 parent f8dcfaf0f5
commit 266148d9cd
16 files changed
+384 -78

No files matched your search

+60 -1
View File
@@ -127,7 +127,12 @@ struct FakeGateway {
impl FakeGateway {
async fn start() -> Self {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
Self::start_on(0).await
}
/// A gateway at `port` of the loopback address (0: any free port).
async fn start_on(port: u16) -> Self {
let listener = TcpListener::bind(("127.0.0.1", port)).await.unwrap();
let url = format!("ws://{}/v1", listener.local_addr().unwrap());
let state: Arc<Mutex<State>> = Arc::default();
let st = state.clone();
@@ -707,3 +712,57 @@ async fn store_history_off_keeps_the_database_empty() {
drop((engine, history));
let _ = std::fs::remove_dir_all(path.parent().unwrap());
}
/// A gateway that is away is tried again by itself and quietly: no error for
/// the user, "connecting" meanwhile, then observing once it is there, with
/// the open chats opened again.
#[tokio::test(flavor = "multi_thread")]
async fn an_unreachable_gateway_is_tried_again_quietly() {
// Nothing listens there yet: the connection is refused.
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
let path = temp_db("retry");
let engine = Engine::start_with(Settings::in_memory(), History::open(&path).unwrap());
let mut rx = engine.subscribe();
engine.send(Command::OpenChat { session: 3, target: LOBBY });
engine.send(Command::ObserveGateway {
session: 3,
url: format!("ws://127.0.0.1:{port}/v1"),
identity: Box::new(tsclientlib::Identity::create()),
});
// The first attempt fails (the next one is 2 s later): still connecting,
// and the user is told nothing.
let mut connecting = false;
let quiet = timeout(Duration::from_millis(1500), async {
loop {
match rx.recv().await {
Ok(Event::Error { message, .. }) => panic!("the user was told: {message}"),
Ok(Event::State { session: 3, state }) => {
// (Off before observing starts.)
assert!(!connecting || state.observe != ObserveState::Off, "gave up");
connecting |= state.observe == ObserveState::Connecting;
}
Ok(_) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {}
Err(e) => panic!("{e}"),
}
}
});
assert!(quiet.await.is_err(), "the event stream ended");
assert!(connecting, "never started observing");
// The gateway comes up: observing, and the open chat is opened again.
let gw = FakeGateway::start_on(port).await;
wait(&mut rx, "observing", |e| match e {
Event::Error { message, .. } => panic!("the user was told: {message}"),
Event::State { session: 3, state } if state.observe == ObserveState::Observing => Some(()),
_ => None,
})
.await;
timeout(Duration::from_secs(5), async {
while !gw.requests().contains(&"open_chat".to_owned()) {
tokio::time::sleep(Duration::from_millis(50)).await;
}
})
.await
.unwrap_or_else(|_| panic!("the chat was not opened again: {:?}", gw.requests()));
drop(engine);
let _ = std::fs::remove_dir_all(path.parent().unwrap());
}