forked from Deuxfleurs/tricot
Compare commits
4 commits
main
...
staging_te
Author | SHA1 | Date | |
---|---|---|---|
|
9209f78a52 | ||
|
b2ba666a11 | ||
|
60e8aacfd7 | ||
|
e9f42cac57 |
5 changed files with 1535 additions and 1160 deletions
796
Cargo.lock
generated
796
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
39
flake.lock
39
flake.lock
|
@ -10,17 +10,17 @@
|
||||||
"rust-overlay": "rust-overlay"
|
"rust-overlay": "rust-overlay"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1666087781,
|
"lastModified": 1713199118,
|
||||||
"narHash": "sha256-trKVdjMZ8mNkGfLcY5LsJJGtdV3xJDZnMVrkFjErlcs=",
|
"narHash": "sha256-MlLdAvk+zXCFUy280sY6LqtykqWXIkKVXo72J7a6HlU=",
|
||||||
"owner": "Alexis211",
|
"owner": "cargo2nix",
|
||||||
"repo": "cargo2nix",
|
"repo": "cargo2nix",
|
||||||
"rev": "a7a61179b66054904ef6a195d8da736eaaa06c36",
|
"rev": "1efb03f2f794ad5eed17e807e858c4da001dbc3e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "Alexis211",
|
"owner": "cargo2nix",
|
||||||
"repo": "cargo2nix",
|
"repo": "cargo2nix",
|
||||||
"rev": "a7a61179b66054904ef6a195d8da736eaaa06c36",
|
"rev": "1efb03f2f794ad5eed17e807e858c4da001dbc3e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -60,11 +60,11 @@
|
||||||
"systems": "systems"
|
"systems": "systems"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1694529238,
|
"lastModified": 1710146030,
|
||||||
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
|
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "flake-utils",
|
"repo": "flake-utils",
|
||||||
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
|
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -75,11 +75,11 @@
|
||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1696234590,
|
"lastModified": 1714030708,
|
||||||
"narHash": "sha256-mgOzQYTvaTT4bFopVOadlndy2RPwLy60rDjIWOGujwo=",
|
"narHash": "sha256-JOGPOxa8N6ySzB7SQBsh0OVz+UXZriyahgvfNHMIY0Y=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "f902cb49892d300ff15cb237e48aa1cad79d68c3",
|
"rev": "b0d52b31f7f4d80f8bf38f0253652125579c35ff",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -91,17 +91,17 @@
|
||||||
},
|
},
|
||||||
"nixpkgs_2": {
|
"nixpkgs_2": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1665657542,
|
"lastModified": 1707091808,
|
||||||
"narHash": "sha256-mojxNyzbvmp8NtVtxqiHGhRfjCALLfk9i/Uup68Y5q8=",
|
"narHash": "sha256-LahKBAfGbY836gtpVNnWwBTIzN7yf/uYM/S0g393r0Y=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "a3073c49bc0163fea6a121c276f526837672b555",
|
"rev": "9f2ee8c91ac42da3ae6c6a1d21555f283458247e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "a3073c49bc0163fea6a121c276f526837672b555",
|
"rev": "9f2ee8c91ac42da3ae6c6a1d21555f283458247e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -117,16 +117,17 @@
|
||||||
"nixpkgs": "nixpkgs"
|
"nixpkgs": "nixpkgs"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1682389182,
|
"lastModified": 1707271822,
|
||||||
"narHash": "sha256-8t2nmFnH+8V48+IJsf8AK51ebXNlVbOSVYOpiqJKvJE=",
|
"narHash": "sha256-/DZsoPH5GBzOpVEGz5PgJ7vh8Q6TcrJq5u8FcBjqAfI=",
|
||||||
"owner": "oxalica",
|
"owner": "oxalica",
|
||||||
"repo": "rust-overlay",
|
"repo": "rust-overlay",
|
||||||
"rev": "74f1a64dd28faeeb85ef081f32cad2989850322c",
|
"rev": "7a94fe7690d2bdfe1aab475382a505e14dc114a6",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "oxalica",
|
"owner": "oxalica",
|
||||||
"repo": "rust-overlay",
|
"repo": "rust-overlay",
|
||||||
|
"rev": "7a94fe7690d2bdfe1aab475382a505e14dc114a6",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
20
flake.nix
20
flake.nix
|
@ -1,22 +1,29 @@
|
||||||
{
|
{
|
||||||
description = "A very basic flake";
|
description = "Tricot, a reverse proxy with consul integration";
|
||||||
|
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/a3073c49bc0163fea6a121c276f526837672b555";
|
# Nixpkgs 23.11 as of 2024-02-07, has rustc v1.73
|
||||||
|
inputs.nixpkgs.url =
|
||||||
|
"github:NixOS/nixpkgs/9f2ee8c91ac42da3ae6c6a1d21555f283458247e";
|
||||||
inputs.cargo2nix = {
|
inputs.cargo2nix = {
|
||||||
# As of 2022-10-18: two small patches over unstable branch, one for clippy and one to fix feature detection
|
# cargo2nix as of 2024-04-25
|
||||||
url = "github:Alexis211/cargo2nix/a7a61179b66054904ef6a195d8da736eaaa06c36";
|
url = "github:cargo2nix/cargo2nix/1efb03f2f794ad5eed17e807e858c4da001dbc3e";
|
||||||
|
|
||||||
# Rust overlay as of 2023-04-25
|
# Rust overlay as of 2024-02-07
|
||||||
inputs.rust-overlay.url =
|
inputs.rust-overlay.url =
|
||||||
"github:oxalica/rust-overlay/74f1a64dd28faeeb85ef081f32cad2989850322c";
|
"github:oxalica/rust-overlay/7a94fe7690d2bdfe1aab475382a505e14dc114a6";
|
||||||
|
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs, cargo2nix }:
|
outputs = { self, nixpkgs, cargo2nix }:
|
||||||
let
|
let
|
||||||
|
targetHost = "x86_64-unknown-linux-musl";
|
||||||
pkgs = import nixpkgs {
|
pkgs = import nixpkgs {
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
|
crossSystem = {
|
||||||
|
config = targetHost;
|
||||||
|
isStatic = true;
|
||||||
|
};
|
||||||
overlays = [ cargo2nix.overlays.default ];
|
overlays = [ cargo2nix.overlays.default ];
|
||||||
};
|
};
|
||||||
packageFun = import ./Cargo.nix;
|
packageFun = import ./Cargo.nix;
|
||||||
|
@ -26,6 +33,7 @@
|
||||||
let
|
let
|
||||||
packageSet = pkgs.rustBuilder.makePackageSet ({
|
packageSet = pkgs.rustBuilder.makePackageSet ({
|
||||||
inherit packageFun rustVersion;
|
inherit packageFun rustVersion;
|
||||||
|
target = targetHost;
|
||||||
} // args);
|
} // args);
|
||||||
in
|
in
|
||||||
packageSet.workspace.tricot {
|
packageSet.workspace.tricot {
|
||||||
|
|
31
src/https.rs
31
src/https.rs
|
@ -41,9 +41,21 @@ pub struct HttpsConfig {
|
||||||
struct HttpsMetrics {
|
struct HttpsMetrics {
|
||||||
requests_received: metrics::Counter<u64>,
|
requests_received: metrics::Counter<u64>,
|
||||||
requests_served: metrics::Counter<u64>,
|
requests_served: metrics::Counter<u64>,
|
||||||
|
requests_in_flight: metrics::UpDownCounter<i64>,
|
||||||
request_proxy_duration: metrics::Histogram<f64>,
|
request_proxy_duration: metrics::Histogram<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct InFlightGuard<'a, 'b> {
|
||||||
|
metrics: &'a HttpsMetrics,
|
||||||
|
tags: &'b [KeyValue],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Drop for InFlightGuard<'a, 'b> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.metrics.requests_in_flight.add(-1, self.tags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn serve_https(
|
pub async fn serve_https(
|
||||||
config: HttpsConfig,
|
config: HttpsConfig,
|
||||||
cert_store: Arc<CertStore>,
|
cert_store: Arc<CertStore>,
|
||||||
|
@ -62,6 +74,10 @@ pub async fn serve_https(
|
||||||
.u64_counter("https_requests_served")
|
.u64_counter("https_requests_served")
|
||||||
.with_description("Total number of requests served over HTTPS")
|
.with_description("Total number of requests served over HTTPS")
|
||||||
.init(),
|
.init(),
|
||||||
|
requests_in_flight: meter
|
||||||
|
.i64_up_down_counter("https_requests_in_flight")
|
||||||
|
.with_description("Current number of requests handled over HTTPS")
|
||||||
|
.init(),
|
||||||
request_proxy_duration: meter
|
request_proxy_duration: meter
|
||||||
.f64_histogram("https_request_proxy_duration")
|
.f64_histogram("https_request_proxy_duration")
|
||||||
.with_description("Duration between time when request was received, and time when backend returned status code and headers")
|
.with_description("Duration between time when request was received, and time when backend returned status code and headers")
|
||||||
|
@ -178,9 +194,7 @@ async fn handle_request(
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
metrics
|
metrics.requests_received.add(1, &[host_tag, method_tag.clone()]);
|
||||||
.requests_received
|
|
||||||
.add(1, &[host_tag, method_tag.clone()]);
|
|
||||||
|
|
||||||
let mut tags = vec![method_tag];
|
let mut tags = vec![method_tag];
|
||||||
let resp = select_target_and_proxy(
|
let resp = select_target_and_proxy(
|
||||||
|
@ -272,6 +286,17 @@ async fn select_target_and_proxy(
|
||||||
);
|
);
|
||||||
proxy_to.calls_in_progress.fetch_add(1, Ordering::SeqCst);
|
proxy_to.calls_in_progress.fetch_add(1, Ordering::SeqCst);
|
||||||
|
|
||||||
|
let tags_in_flight = &tags.clone();
|
||||||
|
metrics.requests_in_flight.add(1, &tags_in_flight);
|
||||||
|
// The guard ensures that we decrement requests_in_flight in all cases where
|
||||||
|
// the current tasks ends, including the case where it gets canceled and
|
||||||
|
// doesn't run to completion (which may happen e.g. if it timeouts).
|
||||||
|
// (Crucially we create the guard before the first .await in this function.)
|
||||||
|
let _guard = InFlightGuard {
|
||||||
|
metrics: &metrics,
|
||||||
|
tags: &tags_in_flight,
|
||||||
|
};
|
||||||
|
|
||||||
// Forward to backend
|
// Forward to backend
|
||||||
debug!("{}{} -> {}", host, path, proxy_to);
|
debug!("{}{} -> {}", host, path, proxy_to);
|
||||||
trace!("Request: {:?}", req);
|
trace!("Request: {:?}", req);
|
||||||
|
|
Loading…
Reference in a new issue