use std::sync::Arc;
use std::time::Duration;
use sapphire_bridge_api::{BRIDGE_NOT_RUNNING, RELAYED, RESTART};
use sapphire_bridge_client::BridgeClient;
use sapphire_ipc::{Router, RpcError};
use serde_json::Value;
use crate::sync::{BridgeConnector, SyncRuntime};
pub(crate) struct OwnLink {
connector: BridgeConnector,
current: tokio::sync::Mutex<Option<Arc<BridgeClient>>>,
}
pub(crate) enum BridgeLink {
Sync(Arc<SyncRuntime>),
Own(OwnLink),
}
impl BridgeLink {
pub(crate) fn own(connector: BridgeConnector) -> BridgeLink {
BridgeLink::Own(OwnLink {
connector,
current: tokio::sync::Mutex::new(None),
})
}
async fn client(&self) -> Result<Arc<BridgeClient>, RpcError> {
match self {
BridgeLink::Sync(runtime) => {
if let Some(client) = runtime.connected_bridge() {
return Ok(client);
}
let mut changes = runtime.bridge_changes();
runtime.reconnect_now();
let open = changes.wait_for(|slot| slot.as_ref().is_some_and(|c| !c.is_closed()));
match tokio::time::timeout(SYNC_WAIT, open).await {
Ok(Ok(slot)) => Ok(Arc::clone(slot.as_ref().expect("an open connection"))),
_ => Err(not_running()),
}
}
BridgeLink::Own(own) => {
let mut current = own.current.lock().await;
if let Some(client) = current.as_ref().filter(|c| !c.is_closed()) {
return Ok(Arc::clone(client));
}
*current = None;
match own.connector.connect().await {
Ok(Some(client)) => {
let client = Arc::new(client);
*current = Some(Arc::clone(&client));
Ok(client)
}
Ok(None) => Err(not_running()),
Err(err) => {
tracing::warn!("could not connect to sapphire-bridge: {err}");
Err(RpcError::internal(err.to_string()))
}
}
}
}
}
async fn lost(&self, client: &Arc<BridgeClient>) {
if let BridgeLink::Own(own) = self {
let mut current = own.current.lock().await;
if current.as_ref().is_some_and(|c| Arc::ptr_eq(c, client)) {
*current = None;
}
}
}
async fn replaced(&self, old: &Arc<BridgeClient>) {
old.closed().await;
match self {
BridgeLink::Sync(runtime) => {
let mut changes = runtime.bridge_changes();
loop {
if changes
.borrow_and_update()
.as_ref()
.is_some_and(|c| !Arc::ptr_eq(c, old) && !c.is_closed())
{
return;
}
runtime.reconnect_now();
tokio::select! {
changed = changes.changed() => if changed.is_err() {
std::future::pending::<()>().await;
},
() = tokio::time::sleep(RECONNECT_INTERVAL) => {}
}
}
}
BridgeLink::Own(own) => loop {
{
let mut current = own.current.lock().await;
if current
.as_ref()
.is_some_and(|c| !Arc::ptr_eq(c, old) && !c.is_closed())
{
return;
}
match own.connector.connect().await {
Ok(Some(client)) => {
*current = Some(Arc::new(client));
return;
}
Ok(None) => {}
Err(err) => tracing::debug!("could not connect to sapphire-bridge: {err}"),
}
}
tokio::time::sleep(RECONNECT_INTERVAL).await;
},
}
}
}
const RECONNECT_INTERVAL: Duration = Duration::from_millis(100);
const SYNC_WAIT: Duration = Duration::from_secs(1);
pub(crate) fn not_running() -> RpcError {
RpcError {
code: BRIDGE_NOT_RUNNING,
message: "no sapphire-bridge is running".to_owned(),
data: None,
}
}
const RESTART_WAIT: Duration = Duration::from_secs(30);
pub(crate) struct Relay {
link: BridgeLink,
restart_wait: Duration,
}
impl Relay {
pub(crate) fn new(link: BridgeLink) -> Relay {
Relay {
link,
restart_wait: RESTART_WAIT,
}
}
#[cfg(test)]
fn restart_wait(mut self, wait: Duration) -> Relay {
self.restart_wait = wait;
self
}
pub(crate) fn routes(self: Arc<Self>, router: Router) -> Router {
self.routes_for(router, RELAYED)
}
pub(crate) fn routes_for(self: Arc<Self>, router: Router, methods: &[&'static str]) -> Router {
methods.iter().fold(router, |router, &method| {
let relay = Arc::clone(&self);
router.method(method, move |ctx| {
let relay = Arc::clone(&relay);
async move { relay.forward(method, ctx.params).await }
})
})
}
pub(crate) async fn bridge(&self) -> Result<Arc<BridgeClient>, RpcError> {
self.link.client().await
}
async fn forward(&self, method: &'static str, params: Value) -> Result<Value, RpcError> {
let result = self.call(method, params).await;
match &result {
Ok(_) => tracing::debug!(method, outcome = "ok", "relayed a bridge call"),
Err(e) => tracing::debug!(method, outcome = e.code, "relayed a bridge call"),
}
result
}
async fn call(&self, method: &'static str, params: Value) -> Result<Value, RpcError> {
let client = self.link.client().await?;
let result = match client.call_raw(method, params).await {
Ok(result) => result,
Err(sapphire_ipc::Error::Rpc(e)) => return Err(e),
Err(_) => {
self.link.lost(&client).await;
return Err(not_running());
}
};
if method == RESTART
&& tokio::time::timeout(self.restart_wait, self.link.replaced(&client))
.await
.is_err()
{
return Err(RpcError::internal(format!(
"the bridge did not come back within {} s",
RESTART_WAIT.as_secs()
)));
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use std::collections::VecDeque;
use std::sync::Mutex;
use sapphire_ipc::ManagedBy;
use sapphire_workspace::AppContext;
use serde_json::json;
use super::*;
use crate::sync::testing::StubBridge;
static CTX: AppContext = AppContext::new("sapphire-relaytest");
type Bridges = Arc<Mutex<VecDeque<BridgeClient>>>;
fn shared() -> (Bridges, BridgeConnector) {
let bridges: Bridges = Arc::default();
let connector = BridgeConnector::new({
let bridges = Arc::clone(&bridges);
move || {
let next = bridges.lock().unwrap().pop_front();
async move { Ok(next) }
}
});
(bridges, connector)
}
fn backed_off(connector: BridgeConnector) -> Arc<SyncRuntime> {
Arc::new(
SyncRuntime::connecting(&CTX, connector, "/bin/true".into(), ManagedBy::Service)
.with_reconnect_backoff(Duration::from_secs(60)),
)
}
fn queued(bridges: Vec<BridgeClient>) -> BridgeConnector {
let bridges = Arc::new(Mutex::new(VecDeque::from(bridges)));
BridgeConnector::new(move || {
let next = bridges.lock().unwrap().pop_front();
async move { Ok(next) }
})
}
async fn restarting_stub() -> (Arc<StubBridge>, BridgeClient) {
let (stub, client) = StubBridge::start_client_in(std::env::temp_dir()).await;
stub.answer(RESTART, json!({}));
let stub = Arc::new(stub);
let going = Arc::clone(&stub);
tokio::spawn(async move {
while going.calls().is_empty() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
going.disconnect();
});
(stub, client)
}
#[tokio::test(flavor = "multi_thread")]
async fn a_bridge_that_refuses_the_connection_is_reported_as_it_said() {
let refusal = || sapphire_ipc::Error::ApiVersionMismatch {
running: 1,
ours: 2,
server_version: "9.9.9".into(),
};
let relay = Relay::new(BridgeLink::own(BridgeConnector::new(move || async move {
Err(refusal())
})));
let e = relay.forward("bridge.status", json!({})).await.unwrap_err();
assert_ne!(e.code, BRIDGE_NOT_RUNNING);
assert_eq!(e.message, refusal().to_string());
}
#[tokio::test(flavor = "multi_thread")]
async fn no_bridge_is_reported_as_not_running() {
let relay = Relay::new(BridgeLink::own(queued(Vec::new())));
let e = relay.forward("bridge.status", json!({})).await.unwrap_err();
assert_eq!(e.code, BRIDGE_NOT_RUNNING);
assert_eq!(e.message, "no sapphire-bridge is running");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_sync_link_finds_a_bridge_started_while_the_runtime_backs_off() {
let (bridges, connector) = shared();
let runtime = backed_off(connector);
runtime.connect().await.unwrap();
let keeping = tokio::spawn(Arc::clone(&runtime).keep_connected());
tokio::time::sleep(Duration::from_millis(200)).await;
let (stub, client) = StubBridge::start_client_in(std::env::temp_dir()).await;
stub.answer("embed.settings", json!({"from": "new"}));
bridges.lock().unwrap().push_back(client);
let relay = Relay::new(BridgeLink::Sync(Arc::clone(&runtime)));
let answer = tokio::time::timeout(
Duration::from_secs(5),
relay.forward("embed.settings", json!({})),
)
.await
.expect("answered well within the backoff")
.unwrap();
assert_eq!(answer, json!({"from": "new"}));
keeping.abort();
}
#[tokio::test(flavor = "multi_thread")]
async fn a_sync_link_restart_does_not_wait_out_the_runtime_backoff() {
let (bridges, connector) = shared();
let (_old, client) = restarting_stub().await;
bridges.lock().unwrap().push_back(client);
let runtime = backed_off(connector);
runtime.connect().await.unwrap();
assert!(runtime.is_connected());
let keeping = tokio::spawn(Arc::clone(&runtime).keep_connected());
let coming = tokio::spawn({
let runtime = Arc::clone(&runtime);
let bridges = Arc::clone(&bridges);
async move {
while runtime.is_connected() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
tokio::time::sleep(Duration::from_millis(300)).await;
let (new, next) = StubBridge::start_client_in(std::env::temp_dir()).await;
new.answer("embed.settings", json!({"from": "new"}));
bridges.lock().unwrap().push_back(next);
new
}
});
let relay =
Relay::new(BridgeLink::Sync(Arc::clone(&runtime))).restart_wait(Duration::from_secs(5));
let ack = relay.forward(RESTART, json!({})).await.unwrap();
assert_eq!(ack, json!({}));
let _new = coming.await.unwrap();
let answer = relay.forward("embed.settings", json!({})).await.unwrap();
assert_eq!(answer, json!({"from": "new"}));
keeping.abort();
}
#[tokio::test(flavor = "multi_thread")]
async fn a_restart_that_never_comes_back_fails_after_the_wait() {
let (_stub, client) = restarting_stub().await;
let relay = Relay::new(BridgeLink::own(queued(vec![client])))
.restart_wait(Duration::from_millis(300));
let e = relay.forward(RESTART, json!({})).await.unwrap_err();
assert_eq!(e.message, "the bridge did not come back within 30 s");
}
#[tokio::test(flavor = "multi_thread")]
async fn an_own_link_restart_returns_once_it_reaches_the_new_bridge() {
let (_old, client) = restarting_stub().await;
let (new, next) = StubBridge::start_client_in(std::env::temp_dir()).await;
new.answer("embed.settings", json!({"from": "new"}));
let relay = Relay::new(BridgeLink::own(queued(vec![client, next])))
.restart_wait(Duration::from_secs(5));
let ack = relay.forward(RESTART, json!({})).await.unwrap();
assert_eq!(ack, json!({}));
let answer = relay.forward("embed.settings", json!({})).await.unwrap();
assert_eq!(answer, json!({"from": "new"}));
}
}