Skip to content

Commit

Permalink
Fix shutdown-after such that it now functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsenkbeil committed Aug 18, 2022
1 parent a0c7c49 commit 1ff3ef2
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 111 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed

- `shutdown-after` cli parameter and config option now properly shuts down
server after N seconds with no connections

## [0.17.5] - 2022-08-18
### Fixed

Expand Down
16 changes: 13 additions & 3 deletions distant-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
ConnectionId, DistantMsg, DistantRequestData, DistantResponseData,
};
use async_trait::async_trait;
use distant_net::{Reply, Server, ServerCtx};
use distant_net::{Reply, Server, ServerConfig, ServerCtx};
use log::*;
use std::{io, path::PathBuf, sync::Arc};

Expand Down Expand Up @@ -39,9 +39,9 @@ where

impl DistantApiServer<LocalDistantApi, <LocalDistantApi as DistantApi>::LocalData> {
/// Creates a new server using the [`LocalDistantApi`] implementation
pub fn local() -> io::Result<Self> {
pub fn local(config: ServerConfig) -> io::Result<Self> {
Ok(Self {
api: LocalDistantApi::initialize()?,
api: LocalDistantApi::initialize(config)?,
})
}
}
Expand All @@ -60,6 +60,11 @@ fn unsupported<T>(label: &str) -> io::Result<T> {
pub trait DistantApi {
type LocalData: Send + Sync;

/// Returns config associated with API server
fn config(&self) -> ServerConfig {
ServerConfig::default()
}

/// Invoked whenever a new connection is established, providing a mutable reference to the
/// newly-created local data. This is a way to support modifying local data before it is used.
#[allow(unused_variables)]
Expand Down Expand Up @@ -385,6 +390,11 @@ where
type Response = DistantMsg<DistantResponseData>;
type LocalData = D;

/// Overridden to leverage [`DistantApi`] implementation of `config`
fn config(&self) -> ServerConfig {
T::config(&self.api)
}

/// Overridden to leverage [`DistantApi`] implementation of `on_accept`
async fn on_accept(&self, local_data: &mut Self::LocalData) {
T::on_accept(&self.api, local_data).await
Expand Down
11 changes: 9 additions & 2 deletions distant-core/src/api/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
DistantApi, DistantCtx,
};
use async_trait::async_trait;
use distant_net::ServerConfig;
use log::*;
use std::{
io,
Expand All @@ -25,13 +26,15 @@ use state::*;
/// impementation of the API instead of a proxy to another machine as seen with
/// implementations on top of SSH and other protocol
pub struct LocalDistantApi {
config: ServerConfig,
state: GlobalState,
}

impl LocalDistantApi {
/// Initialize the api instance
pub fn initialize() -> io::Result<Self> {
pub fn initialize(config: ServerConfig) -> io::Result<Self> {
Ok(Self {
config,
state: GlobalState::initialize()?,
})
}
Expand All @@ -41,6 +44,10 @@ impl LocalDistantApi {
impl DistantApi for LocalDistantApi {
type LocalData = ConnectionState;

fn config(&self) -> ServerConfig {
self.config.clone()
}

/// Injects the global channels into the local connection
async fn on_accept(&self, local_data: &mut Self::LocalData) {
local_data.process_channel = self.state.process.clone_channel();
Expand Down Expand Up @@ -547,7 +554,7 @@ mod tests {
DistantCtx<ConnectionState>,
mpsc::Receiver<DistantResponseData>,
) {
let api = LocalDistantApi::initialize().unwrap();
let api = LocalDistantApi::initialize(Default::default()).unwrap();
let (reply, rx) = make_reply(buffer);
let mut local_data = ConnectionState::default();
DistantApi::on_accept(&api, &mut local_data).await;
Expand Down
2 changes: 1 addition & 1 deletion distant-core/tests/manager_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn should_be_able_to_establish_a_single_connection_and_communicate() {
let (t1, t2) = FramedTransport::pair(100);

// Spawn a server on one end
let _ = DistantApiServer::local()
let _ = DistantApiServer::local(Default::default())
.unwrap()
.start(OneshotListener::from_value(t2.into_split()))?;

Expand Down
2 changes: 1 addition & 1 deletion distant-core/tests/stress/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DistantClientCtx {
let key = SecretKey::default();
let codec = XChaCha20Poly1305Codec::from(key.clone());

if let Ok(api) = LocalDistantApi::initialize() {
if let Ok(api) = LocalDistantApi::initialize(Default::default()) {
let port: PortRange = "0".parse().unwrap();
let port = {
let server_ref = DistantApiServer::new(api)
Expand Down
8 changes: 8 additions & 0 deletions distant-net/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};

mod config;
pub use config::*;

mod connection;
pub use connection::*;

Expand Down Expand Up @@ -31,6 +34,11 @@ pub trait Server: Send {
/// Type of data to store locally tied to the specific connection
type LocalData: Send + Sync;

/// Returns configuration tied to server instance
fn config(&self) -> ServerConfig {
ServerConfig::default()
}

/// Invoked immediately on server start, being provided the raw listener to use (untyped
/// transport), and returning the listener when ready to start (enabling servers that need to
/// tweak a listener to do so)
Expand Down
10 changes: 10 additions & 0 deletions distant-net/src/server/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Represents a general-purpose set of properties tied with a server instance
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServerConfig {
/// If provided, will cause server to shut down if duration is exceeded with no active
/// connections
pub shutdown_after: Option<Duration>,
}
Loading

0 comments on commit 1ff3ef2

Please sign in to comment.