Skip to content

Commit

Permalink
refactor canister resolver to domain resolver, try to get rid of Open…
Browse files Browse the repository at this point in the history
…SSL and some other libs
  • Loading branch information
blind-oracle committed Jun 4, 2024
1 parent 11569cc commit 449428b
Show file tree
Hide file tree
Showing 24 changed files with 322 additions and 217 deletions.
14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ edition = "2021"
ahash = "0.8"
anyhow = "1.0"
arc-swap = "1"
async-scoped = { version = "0.8", features = ["use-tokio"] }
async-trait = "0.1"
axum = "0.7"
axum-extra = "0.9"
axum-server = { version = "0.6", features = ["tls-rustls"] }
axum-macros = "0.4"
backoff = { version = "0.4", features = ["tokio"] }
bytes = "1.5"
candid = "0.10"
chacha20poly1305 = "0.10"
chrono = "0.4"
clap = { version = "4.5", features = ["derive", "string"] }
clap_derive = "4.5"
clickhouse = { version = "0.11", features = ["uuid", "time"] }
# TODO lz4 feature requires C++ compiler to build, fails with MUSL
clickhouse = { git = "https://github.com/blind-oracle/clickhouse.rs.git", default_features = false, features = [
"inserter",
"tls",
"uuid",
"time",
] }
ctrlc = { version = "3.4", features = ["termination"] }
# cloudflare v0.11 is broken, master is partly fixed but unreleased yet.
# see:
# - https://github.com/cloudflare/cloudflare-rs/issues/222
# - https://github.com/cloudflare/cloudflare-rs/issues/236
cloudflare = { git = "https://github.com/blind-oracle/cloudflare-rs.git", feature = [
cloudflare = { git = "https://github.com/blind-oracle/cloudflare-rs.git", default-features = false, features = [
"rustls-tls",
] }
dashmap = "5.5"
Expand All @@ -50,6 +55,7 @@ itertools = "0.12"
humantime = "2.1"
hyper = "1.3"
hyper-util = "0.1"
hyper-rustls = "0.25"
ic-agent = { version = "0.35", features = ["reqwest"] }
#ic-http-gateway = { git = "https://github.com/dfinity/http-gateway" }
ic-http-gateway = { git = "https://github.com/blind-oracle/http-gateway" }
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use reqwest::Url;
use crate::{
core::{AUTHOR_NAME, SERVICE_NAME},
http::dns,
routing::canister::CanisterAlias,
routing::domain::CanisterAlias,
tls::{self, acme},
};

Expand Down
6 changes: 3 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::warn;
use crate::{
cli::Cli,
http, log, metrics,
routing::{self, canister::CanisterResolver},
routing::{self, domain::DomainResolver},
tasks::TaskManager,
tls::{self, cert::Storage},
};
Expand Down Expand Up @@ -71,8 +71,8 @@ pub async fn main(cli: &Cli) -> Result<(), Error> {
// Prepare certificate storage
let storage = Arc::new(Storage::new());

// Prepare canister resolver to infer canister_id from requests
let canister_resolver = CanisterResolver::new(
// Prepare domain resolver to resolve domains & infer canister_id from requests
let canister_resolver = DomainResolver::new(
domains.clone(),
cli.domain.canister_aliases.clone(),
storage.clone(),
Expand Down
8 changes: 4 additions & 4 deletions src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,15 +352,15 @@ impl Conn {
() = tokio::time::sleep(self.options.grace_period) => {},
v = conn.as_mut() => {
if let Err(e) = v {
return Err(anyhow!("Unable to serve connection: {e}"));
return Err(anyhow!("Unable to serve connection: {e:#}"));
}
},
}
}

v = conn.as_mut() => {
if let Err(e) = v {
return Err(anyhow!("Unable to serve connection: {e}"));
return Err(anyhow!("Unable to serve connection: {e:#}"));
}
},
}
Expand Down Expand Up @@ -440,7 +440,7 @@ impl Server {
let (stream, remote_addr) = match v {
Ok(v) => v,
Err(e) => {
warn!("Unable to accept connection: {e}");
warn!("Unable to accept connection: {e:#}");
// Wait few ms just in case that there's an overflown backlog
// so that we don't run into infinite error loop
tokio::time::sleep(Duration::from_millis(10)).await;
Expand All @@ -465,7 +465,7 @@ impl Server {
// Spawn a task to handle connection & track it
self.tracker.spawn(async move {
if let Err(e) = conn.handle(stream).await {
warn!("Server {}: {}: failed to handle connection: {e}", conn.addr, remote_addr);
warn!("Server {}: {}: failed to handle connection: {e:#}", conn.addr, remote_addr);
}

debug!(
Expand Down
14 changes: 7 additions & 7 deletions src/log/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Clickhouse {
let tracker = TaskTracker::new();
tracker.spawn(async move {
if let Err(e) = actor.run(child_token).await {
error!("Clickhouse: error during run: {e}");
error!("Clickhouse: error during run: {e:#}");
}
});

Expand Down Expand Up @@ -100,7 +100,7 @@ impl ClickhouseActor {
&c.log_clickhouse_table
.ok_or_else(|| anyhow!("no table specified"))?,
)?
.with_max_entries(c.log_clickhouse_batch)
.with_max_rows(c.log_clickhouse_batch)
.with_period(Some(c.log_clickhouse_interval))
.with_period_bias(0.1); // add 10% random variance to interval

Expand All @@ -121,7 +121,7 @@ impl ClickhouseActor {

// Drain remaining rows
while let Some(v) = self.rx.recv().await {
self.inserter.write(&v).await.context("unable insert row")?;
self.inserter.write(&v).context("unable insert row")?;
}

// Flush the buffer
Expand All @@ -134,15 +134,15 @@ impl ClickhouseActor {
// If the thresholds are not reached - it doesn't do anything.
_ = interval.tick() => {
match self.inserter.commit().await {
Ok(v) => debug!("Clickhouse: rows inserted: {}", v.entries),
Err(e) => error!("Clickhouse: unable to commit: {e}"),
Ok(v) => debug!("Clickhouse: inserted rows: {}, bytes: {}", v.rows, v.bytes),
Err(e) => error!("Clickhouse: unable to commit: {e:#}"),
}
}

row = self.rx.recv() => {
if let Some(v) = row {
if let Err(e) = self.inserter.write(&v).await {
error!("Clickhouse: unable to insert row: {e}");
if let Err(e) = self.inserter.write(&v) {
error!("Clickhouse: unable to insert row: {e:#}");
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Run for MetricsRunner {
_ = interval.tick() => {
let start = Instant::now();
if let Err(e) = self.update().await {
warn!("Unable to update metrics: {e}");
warn!("Unable to update metrics: {e:#}");
} else {
debug!("Metrics updated in {}ms", start.elapsed().as_millis());
}
Expand Down Expand Up @@ -345,7 +345,7 @@ pub async fn middleware(
.unwrap_or(("no", "no", Duration::ZERO));
let domain = ctx
.as_ref()
.map(|x| x.canister.domain.to_string())
.map(|x| x.domain.name.to_string())
.unwrap_or_else(|| "unknown".into());
let error_cause = error_cause
.clone()
Expand Down Expand Up @@ -384,7 +384,7 @@ pub async fn middleware(
let path = uri.path();
let canister_id = ctx
.as_ref()
.map(|x| x.canister.id.to_string())
.and_then(|x| x.domain.canister_id.map(|v| v.to_string()))
.unwrap_or_else(|| "unknown".into());

let conn_rcvd = conn_info.traffic.rcvd();
Expand Down
2 changes: 1 addition & 1 deletion src/policy/denylist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Run for Denylist {

let lbl = match res {
Err(e) => {
warn!("Denylist update failed: {e}");
warn!("Denylist update failed: {e:#}");
"fail"
}
Ok(v) => {
Expand Down
Loading

0 comments on commit 449428b

Please sign in to comment.