Skip to content

Commit

Permalink
perf: use a 'static server in order to avoid atomic reference counts (#9
Browse files Browse the repository at this point in the history
)

* perf: use a 'static server in order to avoid atomic reference counts

* update rust toolchain version in ci

* set last stable version
  • Loading branch information
gabotechs authored Oct 14, 2023
1 parent 0f0c78b commit 434b2e1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-lint-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable-2023-04-20 # Fixed so that it can be cached
toolchain: stable-2023-07-13 # Fixed so that it can be cached
profile: minimal
components: clippy
override: true
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable-2023-04-20 # Fixed so that it can be cached
toolchain: stable-2023-07-13 # Fixed so that it can be cached
override: true
components: llvm-tools-preview

Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable-2023-04-20 # Fixed so that it can be cached
toolchain: stable-2023-07-13 # Fixed so that it can be cached
profile: minimal

- name: Cache
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
members = ["server"]

[dependencies]
lazy_static = "^1.4.0"
signway-server = { path = "server" }
tokio = { version = "1.28.1", features = ["full"] }
tracing = "^0.1.37"
Expand Down
59 changes: 26 additions & 33 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,25 @@ impl<T: SecretGetter> SignwayServer<T> {
res
}

pub async fn start(self) -> Result<()> {
pub async fn start(&'static self) -> Result<()> {
let in_addr: SocketAddr = ([0, 0, 0, 0], self.port).into();

let arc_self = Arc::new(self);
let listener = TcpListener::bind(in_addr).await?;

info!("Server running in {}", in_addr);
loop {
let (stream, _) = listener.accept().await?;

let arc_self = arc_self.clone();

let service = service_fn(move |req| {
let arc_self = arc_self.clone();
async move {
let res = if req.method() == Method::OPTIONS {
Ok(ok())
} else {
arc_self.route_gateway(req).await
};
if let Ok(res) = res {
Ok(arc_self.with_cors_headers(res))
} else {
res
}
let service = service_fn(move |req| async move {
let res = if req.method() == Method::OPTIONS {
Ok(ok())
} else {
self.route_gateway(req).await
};
if let Ok(res) = res {
Ok(self.with_cors_headers(res))
} else {
res
}
});

Expand All @@ -189,10 +183,10 @@ impl<T: SecretGetter> SignwayServer<T> {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::sync::atomic::{AtomicU16, Ordering};

use hyper::http::HeaderValue;
use hyper::StatusCode;
use lazy_static::lazy_static;
use reqwest::header::HeaderMap;
use time::{OffsetDateTime, PrimitiveDateTime};
use url::Url;
Expand All @@ -203,6 +197,16 @@ mod tests {

use super::*;

lazy_static! {
static ref SERVER: SignwayServer<InMemorySecretGetter> =
server_for_testing([("foo", "foo-secret")], 3000);
}

async fn init() -> &'static str {
tokio::spawn(SERVER.start());
"http://localhost:3000"
}

fn server_for_testing<const N: usize>(
config: [(&str, &str); N],
port: u16,
Expand Down Expand Up @@ -234,14 +238,9 @@ mod tests {
}
}

static PORT: AtomicU16 = AtomicU16::new(3000);

#[tokio::test]
async fn simple_get_works() {
let port = PORT.fetch_add(1, Ordering::SeqCst);
tokio::spawn(server_for_testing([("foo", "foo-secret")], port).start());
let host = &format!("http://localhost:{port}");

let host = init().await;
let signer = UrlSigner::new("foo", "foo-secret");
let signed_url = signer.get_signed_url(host, &base_request()).unwrap();

Expand All @@ -260,10 +259,7 @@ mod tests {

#[tokio::test]
async fn options_returns_cors() {
let port = PORT.fetch_add(1, Ordering::SeqCst);
tokio::spawn(server_for_testing([("foo", "foo-secret")], port).start());
let host = &format!("http://localhost:{port}");

let host = init().await;
let response = reqwest::Client::new()
.request(Method::OPTIONS, host)
.send()
Expand Down Expand Up @@ -292,15 +288,12 @@ mod tests {
.get("access-control-allow-methods")
.unwrap_or(&HeaderValue::from_str("NONE").unwrap()),
"*"
)
);
}

#[tokio::test]
async fn signed_with_different_secret_does_not_work() {
let port = PORT.fetch_add(1, Ordering::SeqCst);
tokio::spawn(server_for_testing([("foo", "foo-secret")], port).start());
let host = &format!("http://localhost:{port}");

let host = init().await;
let bad_signer = UrlSigner::new("foo", "bad-secret");

let signed_url = bad_signer.get_signed_url(host, &base_request()).unwrap();
Expand Down
23 changes: 17 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::anyhow;
use lazy_static::lazy_static;
use std::str::FromStr;

use async_trait::async_trait;
use clap::Parser;
use tracing::info;

use signway_server::hyper::header::HeaderName;
use signway_server::hyper::{Body, Response, StatusCode};
use signway_server::{
Expand Down Expand Up @@ -114,10 +113,7 @@ impl OnBytesTransferred for BytesTransferredLogger {
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt().json().init();

fn make_server() -> anyhow::Result<SignwayServer<Config>> {
let args: Args = Args::parse();
let config: Config = args.clone().try_into()?;
let mut server = SignwayServer::from_env(config);
Expand All @@ -134,6 +130,21 @@ async fn main() -> anyhow::Result<()> {
if let Some(value) = args.access_control_allow_origin {
server = server.access_control_allow_origin(&value)?;
}
Ok(server)
}

lazy_static! {
static ref SERVER: anyhow::Result<SignwayServer<Config>> = make_server();
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt().json().init();

let server = match SERVER.as_ref() {
Ok(server) => server,
Err(err) => return Err(anyhow::anyhow!(err))
};

tokio::select! {
result = server.start() => {
Expand Down

0 comments on commit 434b2e1

Please sign in to comment.