Skip to content

Commit c7b073d

Browse files
authored
Merge pull request #207 from Dstack-TEE/gw-rt
gw: Seperate proxy runtime from Rocket
2 parents 550deef + a247731 commit c7b073d

File tree

7 files changed

+56
-10
lines changed

7 files changed

+56
-10
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
8787
safe-write = "0.1.2"
8888
nix = "0.29.0"
8989
sd-notify = "0.4.5"
90+
jemallocator = "0.5.4"
9091

9192
# Serialization/Parsing
9293
bon = "3.4.0"

gateway/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ serde-duration.workspace = true
4545
reqwest = { workspace = true, features = ["json"] }
4646
hyper = { workspace = true, features = ["server", "http1"] }
4747
hyper-util = { version = "0.1", features = ["tokio"] }
48+
jemallocator.workspace = true
4849

4950
[target.'cfg(unix)'.dependencies]
5051
nix = { workspace = true, features = ["resource"] }

gateway/gateway.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ buffer_size = 8192
6060
connect_top_n = 3
6161
localhost_enabled = false
6262
app_address_ns_prefix = "_dstack-app-address"
63+
workers = 32
6364

6465
[core.proxy.timeouts]
6566
# Timeout for establishing a connection to the target app.

gateway/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub struct ProxyConfig {
7777
pub buffer_size: usize,
7878
pub connect_top_n: usize,
7979
pub localhost_enabled: bool,
80+
pub workers: usize,
8081
pub app_address_ns_prefix: String,
8182
}
8283

gateway/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ mod models;
2020
mod proxy;
2121
mod web_routes;
2222

23+
#[global_allocator]
24+
static ALLOCATOR: jemallocator::Jemalloc = jemallocator::Jemalloc;
25+
2326
fn app_version() -> String {
2427
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
2528
const VERSION: &str = git_version::git_version!(

gateway/src/proxy.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,15 @@ async fn handle_connection(
145145
}
146146
}
147147

148-
pub async fn run(config: &ProxyConfig, proxy: Proxy) -> Result<()> {
148+
#[inline(never)]
149+
pub async fn proxy_main(config: &ProxyConfig, proxy: Proxy) -> Result<()> {
150+
let workers_rt = tokio::runtime::Builder::new_multi_thread()
151+
.thread_name("proxy-worker")
152+
.enable_all()
153+
.worker_threads(config.workers)
154+
.build()
155+
.expect("Failed to build Tokio runtime");
156+
149157
let dotted_base_domain = {
150158
let base_domain = config.base_domain.as_str();
151159
let base_domain = base_domain.strip_prefix(".").unwrap_or(base_domain);
@@ -174,7 +182,7 @@ pub async fn run(config: &ProxyConfig, proxy: Proxy) -> Result<()> {
174182
info!(%from, "new connection");
175183
let proxy = proxy.clone();
176184
let dotted_base_domain = dotted_base_domain.clone();
177-
tokio::spawn(
185+
workers_rt.spawn(
178186
async move {
179187
let _conn_entered = conn_entered;
180188
let timeouts = &proxy.config.proxy.timeouts;
@@ -211,14 +219,24 @@ fn next_connection_id() -> usize {
211219
}
212220

213221
pub fn start(config: ProxyConfig, app_state: Proxy) {
214-
tokio::spawn(async move {
215-
if let Err(err) = run(&config, app_state).await {
216-
error!(
217-
"error on {}:{}: {err:?}",
218-
config.listen_addr, config.listen_port
219-
);
220-
}
221-
});
222+
std::thread::Builder::new()
223+
.name("proxy-main".to_string())
224+
.spawn(move || {
225+
// Create a new single-threaded runtime
226+
let rt = tokio::runtime::Builder::new_current_thread()
227+
.enable_all()
228+
.build()
229+
.expect("Failed to build Tokio runtime");
230+
231+
// Run the proxy_main function in this runtime
232+
if let Err(err) = rt.block_on(proxy_main(&config, app_state)) {
233+
error!(
234+
"error on {}:{}: {err:?}",
235+
config.listen_addr, config.listen_port
236+
);
237+
}
238+
})
239+
.expect("Failed to spawn proxy-main thread");
222240
}
223241

224242
#[cfg(test)]

0 commit comments

Comments
 (0)