Skip to content

Commit 68e0263

Browse files
committed
fix: shellflip conditional compilation for non unix platform
1 parent 14cf2cf commit 68e0263

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

Cargo.lock

-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ clap = { version = "4", features = ["derive"] }
2323
moka = { version = "0.12", features = ["sync"] }
2424
smol_str = { version = "0.2.1", features = ["serde"] }
2525
anyhow = "1.0"
26-
shellflip = "2.1.0"
2726

2827
[workspace.lints.rust]
2928
unused_extern_crates = 'warn'
@@ -47,9 +46,7 @@ anyhow = { workspace = true }
4746
hyper = { workspace = true }
4847
http = { workspace = true }
4948
tokio = { workspace = true }
50-
tokio-util = { workspace = true }
5149
wasmtime = { workspace = true }
52-
wasmtime-wasi = { workspace = true }
5350
smol_str = { workspace = true }
5451
async-trait = {workspace = true}
5552
clap = { version = "4.5", features = ["derive"] }
@@ -62,6 +59,7 @@ secret = { path = "crates/secret" }
6259
hyper-tls = "0.6"
6360
hyper-util = { version = "0.1", features = ["client", "client-legacy", "http1", "tokio"] }
6461
http-body-util = "0.1"
65-
shellflip = {workspace = true}
6662
bytesize = "1.3.0"
6763

64+
[target.'cfg(target_family = "unix")'.dependencies]
65+
shellflip = "2.1.1"

crates/http-service/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ async-trait = "0.1"
3939
wasmtime-wasi-http = "20.0.2"
4040
hyper-util = { version = "0.1", features = ["server", "server-graceful"] }
4141
http-body-util = "0.1"
42-
shellflip = {workspace = true}
4342
bytes = "1.6"
44-
uri = "0.4"
43+
44+
[target.'cfg(target_family = "unix")'.dependencies]
45+
shellflip = "2.1.1"
4546

4647
[dev-dependencies]
4748
claims = "0.7"

crates/http-service/src/lib.rs

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::net::SocketAddr;
2-
use std::os::fd::OwnedFd;
3-
use std::sync::{Arc, Weak};
2+
use std::sync::Arc;
43
use std::time::Duration;
54

65
pub use crate::executor::ExecutorFactory;
@@ -24,7 +23,6 @@ use runtime::{
2423
WasmEngine, WasmEngineBuilder,
2524
};
2625
use secret::SecretStrategy;
27-
use shellflip::{ShutdownHandle, ShutdownSignal};
2826
use smol_str::SmolStr;
2927
use state::HttpState;
3028
use tokio::{net::TcpListener, time::error::Elapsed};
@@ -35,6 +33,11 @@ pub mod state;
3533

3634
pub(crate) static TRACEPARENT: &str = "traceparent";
3735

36+
#[cfg(target_family = "unix")]
37+
type OwnedFd = std::os::fd::OwnedFd;
38+
#[cfg(not(target_family = "unix"))]
39+
type OwnedFd = std::os::raw::c_int;
40+
3841
#[cfg(feature = "metrics")]
3942
const HTTP_LABEL: &[&str; 1] = &["http"];
4043

@@ -47,7 +50,8 @@ const FASTEDGE_EXECUTION_PANIC: u16 = 533;
4750
pub struct HttpConfig {
4851
pub all_interfaces: bool,
4952
pub port: u16,
50-
pub cancel: Weak<ShutdownHandle>,
53+
#[cfg(target_family = "unix")]
54+
pub cancel: std::sync::Weak<shellflip::ShutdownHandle>,
5155
pub listen_fd: Option<OwnedFd>,
5256
pub backoff: u64,
5357
}
@@ -87,9 +91,15 @@ where
8791
/// Run hyper http service
8892
async fn run(self, config: Self::Config) -> Result<()> {
8993
let listener = if let Some(fd) = config.listen_fd {
90-
let listener = std::net::TcpListener::from(fd);
91-
listener.set_nonblocking(true)?;
92-
TcpListener::from_std(listener)?
94+
#[cfg(target_family = "unix")]
95+
{
96+
let listener = std::net::TcpListener::from(fd);
97+
listener.set_nonblocking(true)?;
98+
TcpListener::from_std(listener)?
99+
}
100+
101+
#[cfg(not(target_family = "unix"))]
102+
panic!("listen_fd is not supported on this platform")
93103
} else {
94104
let interface: [u8; 4] = if config.all_interfaces {
95105
[0, 0, 0, 0]
@@ -99,17 +109,22 @@ where
99109
let listen_addr = SocketAddr::from((interface, config.port));
100110
TcpListener::bind(listen_addr).await?
101111
};
112+
102113
let listen_addr = listener.local_addr()?;
103114
tracing::info!("Listening on http://{}", listen_addr);
104115
let mut backoff = 1;
105116
let self_ = Arc::new(self);
106117
let graceful = hyper_util::server::graceful::GracefulShutdown::new();
118+
#[cfg(target_family = "unix")]
107119
let mut signal = config
108120
.cancel
109121
.upgrade()
110-
.map(|s| ShutdownSignal::from(s.as_ref()))
122+
.map(|s| shellflip::ShutdownSignal::from(s.as_ref()))
111123
.unwrap_or_default();
112124

125+
#[cfg(not(target_family = "unix"))]
126+
let signal = signal::Signal {};
127+
113128
loop {
114129
tokio::select! {
115130
conn = listener.accept() => {
@@ -131,7 +146,7 @@ where
131146
}
132147
});
133148

134-
let connection = http1::Builder::new().keep_alive(true).serve_connection(io, service);
149+
let connection = http1::Builder::new().keep_alive(true).serve_connection(io, service);
135150
let connection = graceful.watch(connection);
136151
tokio::spawn(async move {
137152
if let Err(error) = connection.await {
@@ -586,6 +601,17 @@ fn app_req_headers(geo: impl Iterator<Item = (SmolStr, SmolStr)>) -> HeaderMap {
586601
headers
587602
}
588603

604+
#[cfg(not(target_family = "unix"))]
605+
pub(crate) mod signal {
606+
pub(crate) struct Signal;
607+
608+
impl Signal {
609+
pub(crate) async fn on_shutdown(&self) {
610+
tokio::signal::ctrl_c().await.expect("ctrl-c");
611+
}
612+
}
613+
}
614+
589615
#[cfg(test)]
590616
mod tests {
591617
use test_case::test_case;

src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use runtime::{
2525
componentize_if_necessary, App, ContextT, ExecutorCache, PreCompiledLoader, Router,
2626
SecretValue, WasiVersion, WasmConfig, WasmEngine,
2727
};
28-
use shellflip::ShutdownCoordinator;
2928
use smol_str::{SmolStr, ToSmolStr};
3029
use std::collections::HashMap;
3130
use std::path::PathBuf;
@@ -95,7 +94,8 @@ struct CliContext {
9594
#[tokio::main]
9695
async fn main() -> anyhow::Result<()> {
9796
pretty_env_logger::init();
98-
let shutdown_coordinator = ShutdownCoordinator::new();
97+
#[cfg(target_family = "unix")]
98+
let shutdown_coordinator = shellflip::ShutdownCoordinator::new();
9999
let args = Cli::parse();
100100
let config = WasmConfig::default();
101101
let engine = Engine::new(&config)?;
@@ -157,6 +157,7 @@ async fn main() -> anyhow::Result<()> {
157157
let http = http.run(HttpConfig {
158158
all_interfaces: false,
159159
port: run.port,
160+
#[cfg(target_family = "unix")]
160161
cancel: shutdown_coordinator.handle_weak(),
161162
listen_fd: None,
162163
backoff: 64,
@@ -166,6 +167,7 @@ async fn main() -> anyhow::Result<()> {
166167
res?
167168
},
168169
_ = tokio::signal::ctrl_c() => {
170+
#[cfg(target_family = "unix")]
169171
shutdown_coordinator.shutdown().await
170172
}
171173
}

0 commit comments

Comments
 (0)