Skip to content

Commit

Permalink
ListenerClosed using optional error_code
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzilladev committed Nov 22, 2023
1 parent eaa6223 commit 7ab0f6e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
10 changes: 5 additions & 5 deletions ngrok/examples/mingrok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() -> Result<(), Error> {
let (restart_tx, restart_rx) = oneshot::channel();
let restart_tx = Arc::new(Mutex::new(Some(restart_tx)));

let mut tun = ngrok::Session::builder()
let mut fwd = ngrok::Session::builder()
.authtoken_from_env()
.handle_stop_command(move |req| {
let stop_tx = stop_tx.clone();
Expand Down Expand Up @@ -63,18 +63,18 @@ async fn main() -> Result<(), Error> {
.listen_and_forward(forwards_to.clone())
.await?;

info!(url = tun.url(), %forwards_to, "started tunnel");
info!(url = fwd.url(), %forwards_to, "started forwarder");

let mut fut = tun.join().fuse();
let mut fut = fwd.join().fuse();
let mut stop_rx = stop_rx.fuse();
let mut restart_rx = restart_rx.fuse();

select! {
res = fut => return Ok(res??),
res = fut => info!("{res:?}"),
_ = stop_rx => return Ok(()),
_ = restart_rx => {
drop(fut);
let _ = tun.close().await;
let _ = fwd.close().await;
continue
},
}
Expand Down
4 changes: 2 additions & 2 deletions ngrok/src/internals/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,12 @@ pub struct Update {
#[serde(rename_all = "PascalCase")]
pub struct StopTunnel {
/// The id of the tunnel to stop
#[serde(rename = "ClientID")]
#[serde(rename = "Id")]
pub client_id: String,
/// The message on why this tunnel was stopped
pub message: String,
/// An optional ngrok error code
pub error_code: String,
pub error_code: Option<String>,
}

pub type UpdateResp = CommandResp;
Expand Down
7 changes: 5 additions & 2 deletions ngrok/src/internals/raw_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use super::{
rpc::RpcRequest,
};
use crate::{
tunnel::AcceptError::TunnelClosed,
tunnel::AcceptError::ListenerClosed,
Session,
};

Expand Down Expand Up @@ -475,7 +475,10 @@ impl IncomingStreams {
session
.close_tunnel_with_error(
req.client_id,
TunnelClosed(req.message.clone(), req.error_code.clone()),
ListenerClosed {
message: req.message,
error_code: req.error_code,
},
)
.await;
}
Expand Down
11 changes: 8 additions & 3 deletions ngrok/src/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ pub enum AcceptError {
/// An error arose during reconnect
#[error("reconnect error")]
Reconnect(#[from] Arc<ConnectError>),
/// The tunnel was closed.
#[error("tunnel closed")]
TunnelClosed(String, String),
/// The listener was closed.
#[error("listener closed: {message}{}", error_code.clone().map(|s| format!(", {s}")).unwrap_or_else(String::new))]
ListenerClosed {
/// The error message.
message: String,
/// The error code, if any.
error_code: Option<String>,
},
}

#[derive(Clone)]
Expand Down

0 comments on commit 7ab0f6e

Please sign in to comment.