Skip to content

Commit 1877132

Browse files
authored
Merge pull request #70 from rainshowerLabs/0.3.3
Blutgang 0.3.3
2 parents cbb4e7b + 15a2105 commit 1877132

20 files changed

Lines changed: 961 additions & 299 deletions

Cargo.lock

Lines changed: 163 additions & 160 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "blutgang"
3-
version = "0.3.2"
3+
version = "0.3.3-rc1"
44
edition = "2021"
55
authors = ["makemake <vukasin@gostovic.me>, Rainshower Labs, contributors"]
66
license-file = "LICENSE"

example_config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ ma_length = 100
1212
sort_on_startup = true
1313
# Enable health checking
1414
health_check = true
15+
# Enable content type header checking. Set this to `true` if you want
16+
# Blutgang to be JSON-RPC compliant.
17+
header_check = true
1518
# Acceptable time to wait for a response in ms
1619
ttl = 30
1720
# How many times to retry a request before giving up
@@ -55,7 +58,7 @@ compression = false
5558
# Print DB profile when dropped. Doesn't do anything for now.
5659
print_profile = false
5760
# Frequency of flushes in ms
58-
flush_every_ms = 24000
61+
flush_every_ms = 240
5962

6063
# Add separate RPCs as TOML tables
6164
# DO NOT name an rpc `blutgang`, `admin`, or `sled`

flake.nix

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
pkgs = import nixpkgs {
1515
inherit system overlays;
1616
};
17-
cargoMeta = builtins.fromTOML (builtins.readFile ./Cargo.toml);
18-
in {
17+
cargoMeta = builtins.fromTOML (builtins.readFile ./Cargo.toml);
18+
in
19+
{
1920
packages.default = pkgs.rustPlatform.buildRustPackage {
2021
pname = cargoMeta.package.name;
2122
version = cargoMeta.package.version;
@@ -33,7 +34,6 @@
3334
extensions = [ "rust-src" "rustfmt-preview" "rust-analyzer" ];
3435
})
3536
];
36-
3737
cargoBuildFlags = [ "--profile maxperf" ];
3838
};
3939

@@ -43,8 +43,11 @@
4343
pkg-config
4444
openssl
4545
systemd
46-
(rust-bin.stable.latest.default.override {
47-
extensions = [ "rust-src" "rustfmt-preview" "rust-analyzer"];
46+
clang
47+
gdb
48+
python311Packages.requests
49+
(rust-bin.stable.latest.default.override {
50+
extensions = [ "rust-src" "rustfmt-preview" "rust-analyzer" ];
4851
})
4952
];
5053

@@ -53,13 +56,17 @@
5356
export RUSTC_WRAPPER=$(which sccache)
5457
export OLD_PS1="$PS1" # Preserve the original PS1
5558
export PS1="nix-shell:blutgang $PS1" # Customize this line as needed
59+
60+
# Set NIX_LD and NIX_LD_LIBRARY_PATH for rust-analyzer
61+
export NIX_LD_LIBRARY_PATH="${pkgs.lib.makeLibraryPath [ pkgs.glibc pkgs.gcc-unwrapped.lib ]}"
62+
export NIX_LD="${pkgs.stdenv.cc}/nix-support/dynamic-linker"
5663
'';
5764

58-
# reser PS1
65+
# reset ps1
5966
shellExitHook = ''
6067
export PS1="$OLD_PS1"
6168
'';
6269
};
6370
}
6471
);
65-
}
72+
}

shell.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pkgs.mkShell {
1515
pkgs.pkg-config
1616
pkgs.openssl
1717
pkgs.systemdLibs
18-
pkgs.cargo2nix
1918
];
2019

2120
shellHook = ''

src/admin/accept.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
use crate::log_info;
1+
use crate::{
2+
admin::liveready::{
3+
accept_health_request,
4+
accept_readiness_request,
5+
LiveReadyRequestSnd,
6+
},
7+
log_info,
8+
};
29
use http_body_util::Full;
310
use hyper::{
411
body::Bytes,
@@ -131,7 +138,14 @@ pub async fn accept_admin_request(
131138
poverty_list_rwlock: Arc<RwLock<Vec<Rpc>>>,
132139
cache: Arc<Db>,
133140
config: Arc<RwLock<Settings>>,
141+
liveness_request_tx: LiveReadyRequestSnd,
134142
) -> Result<hyper::Response<Full<Bytes>>, Infallible> {
143+
if tx.uri().path() == "/ready" {
144+
return accept_readiness_request(liveness_request_tx).await;
145+
} else if tx.uri().path() == "/health" {
146+
return accept_health_request(liveness_request_tx).await;
147+
}
148+
135149
let mut tx = incoming_to_value(tx).await.unwrap();
136150

137151
// If we have JWT enabled check that tx is valid

src/admin/listener.rs

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
use std::sync::{
2-
Arc,
3-
RwLock,
1+
use std::{
2+
net::SocketAddr,
3+
sync::{
4+
Arc,
5+
RwLock,
6+
},
47
};
58

69
use sled::Db;
710

811
use crate::{
9-
admin::accept::accept_admin_request,
12+
admin::{
13+
accept::accept_admin_request,
14+
liveready::{
15+
liveness_monitor,
16+
LiveReadyRequestSnd,
17+
LiveReadyUpdateRecv,
18+
},
19+
},
1020
log_info,
1121
Rpc,
1222
Settings,
@@ -17,7 +27,10 @@ use hyper::{
1727
service::service_fn,
1828
};
1929
use hyper_util_blutgang::rt::TokioIo;
20-
use tokio::net::TcpListener;
30+
use tokio::{
31+
net::TcpListener,
32+
sync::mpsc,
33+
};
2134

2235
macro_rules! accept_admin {
2336
(
@@ -26,6 +39,7 @@ macro_rules! accept_admin {
2639
$poverty_list_rwlock:expr,
2740
$cache:expr,
2841
$config:expr,
42+
$liveness_request_tx:expr,
2943
) => {
3044
// Bind the incoming connection to our service
3145
if let Err(err) = http1::Builder::new()
@@ -39,6 +53,7 @@ macro_rules! accept_admin {
3953
Arc::clone($poverty_list_rwlock),
4054
Arc::clone($cache),
4155
Arc::clone($config),
56+
$liveness_request_tx.clone(),
4257
);
4358
response
4459
}),
@@ -50,24 +65,17 @@ macro_rules! accept_admin {
5065
};
5166
}
5267

53-
// Used for listening to admin requests as its own tokio task.
54-
//
55-
// Similar to what you'd find in main/balancer
56-
pub async fn listen_for_admin_requests(
68+
async fn admin_api_server(
5769
rpc_list_rwlock: Arc<RwLock<Vec<Rpc>>>,
5870
poverty_list_rwlock: Arc<RwLock<Vec<Rpc>>>,
5971
cache: Arc<Db>,
6072
config: Arc<RwLock<Settings>>,
73+
address: SocketAddr,
74+
liveness_request_tx: LiveReadyRequestSnd,
6175
) -> Result<(), Box<dyn std::error::Error>> {
62-
let address;
63-
{
64-
let config_guard = config.read().unwrap();
65-
address = config_guard.admin.address;
66-
}
67-
6876
// Create a listener and bind to it
6977
let listener = TcpListener::bind(address).await?;
70-
log_info!("Bound admin to: {}", address);
78+
log_info!("Bound admin API to: {}", address);
7179

7280
loop {
7381
let (stream, socketaddr) = listener.accept().await?;
@@ -81,6 +89,7 @@ pub async fn listen_for_admin_requests(
8189
let poverty_list_rwlock_clone = Arc::clone(&poverty_list_rwlock);
8290
let cache_clone = Arc::clone(&cache);
8391
let config_clone = Arc::clone(&config);
92+
let liveness_request_tx_clone = liveness_request_tx.clone();
8493

8594
// Spawn a tokio task to serve multiple connections concurrently
8695
tokio::task::spawn(async move {
@@ -90,7 +99,40 @@ pub async fn listen_for_admin_requests(
9099
&poverty_list_rwlock_clone,
91100
&cache_clone,
92101
&config_clone,
102+
&liveness_request_tx_clone,
93103
);
94104
});
95105
}
96106
}
107+
108+
// Used for listening to admin requests as its own tokio task.
109+
// Also used for k8s liveness/readiness probes.
110+
//
111+
// Similar to what you'd find in main/balancer
112+
pub async fn listen_for_admin_requests(
113+
rpc_list_rwlock: Arc<RwLock<Vec<Rpc>>>,
114+
poverty_list_rwlock: Arc<RwLock<Vec<Rpc>>>,
115+
cache: Arc<Db>,
116+
config: Arc<RwLock<Settings>>,
117+
liveness_receiver: LiveReadyUpdateRecv,
118+
) -> Result<(), Box<dyn std::error::Error>> {
119+
let address;
120+
{
121+
let config_guard = config.read().unwrap();
122+
address = config_guard.admin.address;
123+
}
124+
125+
// Spawn thread for monitoring the current liveness status of Blutgang
126+
let (liveness_request_tx, liveness_request_rx) = mpsc::channel(16);
127+
tokio::spawn(liveness_monitor(liveness_receiver, liveness_request_rx));
128+
129+
admin_api_server(
130+
rpc_list_rwlock,
131+
poverty_list_rwlock,
132+
cache,
133+
config,
134+
address,
135+
liveness_request_tx,
136+
)
137+
.await
138+
}

0 commit comments

Comments
 (0)