-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re_web_viewer_server
no longer needs tokio, split out sync code path (
#6030) ### What * Followup to #6005 * another piece of #5907 Removes tokio dependency from re_web_viewer_server: * async `WebViewerServer` no longer uses tokio internally, instead use `future_channel::oneshot` for shutdown * `WebViewerServerHandle` is now optional (but default enabled) and spawns a thread that internally uses the tiny pollster crate * removed `re_web_viewer_server`'s main file - it's not used by any test and may thus rot * this allows us to also remove the clap dependency from this crate⚠️ Rerun serve/connect still needs a tokio runtime with this change⚠️ : Likely the last piece after this is `re_sdk_comms` which is very similar in nature to `re_ws_comms`. ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using examples from latest `main` build: [rerun.io/viewer](https://rerun.io/viewer/pr/6030?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [rerun.io/viewer](https://rerun.io/viewer/pr/6030?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! - [PR Build Summary](https://build.rerun.io/pr/6030) - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) To run all checks from `main`, comment on the PR with `@rerun-bot full-check`.
- Loading branch information
Showing
7 changed files
with
96 additions
and
118 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::{server_url, WebViewerServer, WebViewerServerError, WebViewerServerPort}; | ||
|
||
/// Sync handle for the [`WebViewerServer`] | ||
/// | ||
/// When dropped, the server will be shut down. | ||
pub struct WebViewerServerHandle { | ||
local_addr: std::net::SocketAddr, | ||
shutdown: Option<( | ||
futures_channel::oneshot::Sender<()>, | ||
std::thread::JoinHandle<()>, | ||
)>, | ||
} | ||
|
||
impl Drop for WebViewerServerHandle { | ||
fn drop(&mut self) { | ||
re_log::info!("Shutting down web server on {}", self.server_url()); | ||
|
||
if let Some((shutdown_tx, thread_handle)) = self.shutdown.take() { | ||
if shutdown_tx.send(()).is_err() { | ||
re_log::error!("Failed to send shutdown signal to web server thread."); | ||
} | ||
thread_handle.join().ok(); | ||
} | ||
} | ||
} | ||
|
||
impl WebViewerServerHandle { | ||
/// Create new [`WebViewerServer`] to host the Rerun Web Viewer on a specified port. | ||
/// Returns a [`WebViewerServerHandle`] that will shutdown the server when dropped. | ||
/// | ||
/// A port of 0 will let the OS choose a free port. | ||
/// | ||
/// Internally spawns a thread to run the server. | ||
/// If you instead want to use the server in your async runtime (e.g. [`tokio`](https://docs.rs/tokio/latest/tokio/) or [`smol`](https://docs.rs/smol/latest/smol/)), use [`WebViewerServer`]. | ||
pub fn new( | ||
bind_ip: &str, | ||
requested_port: WebViewerServerPort, | ||
) -> Result<Self, WebViewerServerError> { | ||
let (shutdown_tx, shutdown_rx) = futures_channel::oneshot::channel(); | ||
|
||
let web_server = WebViewerServer::new(bind_ip, requested_port)?; | ||
|
||
let local_addr = web_server.server.local_addr(); | ||
|
||
let server_serve_future = async { | ||
if let Err(err) = web_server.serve_with_graceful_shutdown(shutdown_rx).await { | ||
re_log::error!("Web server failed: {err}"); | ||
} | ||
}; | ||
|
||
let thread_handle = std::thread::Builder::new() | ||
.name("WebViewerServerHandle".to_owned()) | ||
.spawn(move || pollster::block_on(server_serve_future)) | ||
.map_err(WebViewerServerError::ThreadSpawnFailed)?; | ||
|
||
let slf = Self { | ||
local_addr, | ||
shutdown: Some((shutdown_tx, thread_handle)), | ||
}; | ||
|
||
re_log::info!("Started web server on {}", slf.server_url()); | ||
|
||
Ok(slf) | ||
} | ||
|
||
/// Includes `http://` prefix | ||
pub fn server_url(&self) -> String { | ||
server_url(&self.local_addr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters