Privatize fake sources, drop restating docs, remove boxed iterator in capped
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
8b3f91dcb7
commit
d5a0373a75
9 files changed
+25
-38
No files matched your search
@@ -30,7 +30,6 @@ pub enum TokenCmd {
|
|||||||
Revoke { name: String },
|
Revoke { name: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A token row without its hash.
|
|
||||||
#[derive(Debug, Clone, sqlx::FromRow)]
|
#[derive(Debug, Clone, sqlx::FromRow)]
|
||||||
pub struct TokenInfo {
|
pub struct TokenInfo {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -39,7 +38,6 @@ pub struct TokenInfo {
|
|||||||
pub last_used_at: Option<String>,
|
pub last_used_at: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hex sha256 of a token string.
|
|
||||||
pub fn hash_token(token: &str) -> String {
|
pub fn hash_token(token: &str) -> String {
|
||||||
Sha256::digest(token.as_bytes())
|
Sha256::digest(token.as_bytes())
|
||||||
.iter()
|
.iter()
|
||||||
@@ -149,7 +147,6 @@ pub async fn verify(pool: &SqlitePool, token: &str) -> Result<bool> {
|
|||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute a token subcommand against the DB at `config.auth.db_path`.
|
|
||||||
pub async fn run(cmd: TokenCmd, config: &Config) -> Result<()> {
|
pub async fn run(cmd: TokenCmd, config: &Config) -> Result<()> {
|
||||||
let pool = crate::db::open(config).await?;
|
let pool = crate::db::open(config).await?;
|
||||||
match cmd {
|
match cmd {
|
||||||
|
|||||||
@@ -422,7 +422,6 @@ mod tests {
|
|||||||
load(None, &CliOverrides::default()),
|
load(None, &CliOverrides::default()),
|
||||||
Err(ConfigError::Invalid(_))
|
Err(ConfigError::Invalid(_))
|
||||||
));
|
));
|
||||||
// Loopback with auth off is fine.
|
|
||||||
jail.set_env("SKYG_SERVER__BIND", "127.0.0.1");
|
jail.set_env("SKYG_SERVER__BIND", "127.0.0.1");
|
||||||
assert!(load(None, &CliOverrides::default()).is_ok());
|
assert!(load(None, &CliOverrides::default()).is_ok());
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ use sqlx::SqlitePool;
|
|||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
/// Open (creating if needed) `config.auth.db_path` and migrate.
|
|
||||||
pub async fn open(config: &Config) -> Result<SqlitePool> {
|
pub async fn open(config: &Config) -> Result<SqlitePool> {
|
||||||
let path = &config.auth.db_path;
|
let path = &config.auth.db_path;
|
||||||
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
|
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
|
||||||
@@ -51,7 +50,6 @@ fn restrict_files(db: &Path) {
|
|||||||
let _ = db;
|
let _ = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the embedded migrations.
|
|
||||||
pub async fn migrate(pool: &SqlitePool) -> Result<()> {
|
pub async fn migrate(pool: &SqlitePool) -> Result<()> {
|
||||||
sqlx::migrate!("./migrations")
|
sqlx::migrate!("./migrations")
|
||||||
.run(pool)
|
.run(pool)
|
||||||
|
|||||||
+12
-12
@@ -147,20 +147,20 @@ fn capped<T: Serialize>(
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|i| serde_json::to_string(i).map_or(0, |s| s.len() + 1))
|
.map(|i| serde_json::to_string(i).map_or(0, |s| s.len() + 1))
|
||||||
.collect();
|
.collect();
|
||||||
let mut total = 0usize;
|
let fit = |sizes: &mut dyn Iterator<Item = &usize>| {
|
||||||
let mut keep = 0usize;
|
let mut total = 0usize;
|
||||||
let iter: Box<dyn Iterator<Item = usize>> = if keep_newest {
|
sizes
|
||||||
Box::new(sizes.iter().rev().copied())
|
.take_while(|&&sz| {
|
||||||
|
total += sz;
|
||||||
|
total <= MAX_RESULT_BYTES
|
||||||
|
})
|
||||||
|
.count()
|
||||||
|
};
|
||||||
|
let keep = if keep_newest {
|
||||||
|
fit(&mut sizes.iter().rev())
|
||||||
} else {
|
} else {
|
||||||
Box::new(sizes.iter().copied())
|
fit(&mut sizes.iter())
|
||||||
};
|
};
|
||||||
for sz in iter {
|
|
||||||
if total + sz > MAX_RESULT_BYTES {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
total += sz;
|
|
||||||
keep += 1;
|
|
||||||
}
|
|
||||||
let truncated = keep < items.len();
|
let truncated = keep < items.len();
|
||||||
if truncated {
|
if truncated {
|
||||||
if keep_newest {
|
if keep_newest {
|
||||||
|
|||||||
@@ -46,8 +46,7 @@ pub struct BollardDocker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BollardDocker {
|
impl BollardDocker {
|
||||||
/// Never fails or blocks. Reads `sources.docker_socket` and
|
/// Never fails or blocks.
|
||||||
/// `limits.max_tail` from `config`. Follow tasks stop on `shutdown`.
|
|
||||||
pub fn new(config: &Config, shutdown: CancellationToken) -> Self {
|
pub fn new(config: &Config, shutdown: CancellationToken) -> Self {
|
||||||
Self {
|
Self {
|
||||||
socket: config.sources.docker_socket.clone(),
|
socket: config.sources.docker_socket.clone(),
|
||||||
@@ -67,7 +66,6 @@ impl BollardDocker {
|
|||||||
.map_err(|e| map_error(&e, &self.socket_name()))
|
.map_err(|e| map_error(&e, &self.socket_name()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The cached client, connecting on first successful use.
|
|
||||||
fn client(&self) -> Result<Docker, AppError> {
|
fn client(&self) -> Result<Docker, AppError> {
|
||||||
let mut guard = self.client.lock().unwrap_or_else(|p| p.into_inner());
|
let mut guard = self.client.lock().unwrap_or_else(|p| p.into_inner());
|
||||||
if let Some(c) = guard.as_ref() {
|
if let Some(c) = guard.as_ref() {
|
||||||
|
|||||||
+11
-12
@@ -1,8 +1,7 @@
|
|||||||
//! Fixture-backed in-memory sources for tests and for developing surfaces
|
//! Fixture-backed in-memory sources for tests and for developing surfaces
|
||||||
//! without systemd, journald or Docker.
|
//! without systemd, journald or Docker.
|
||||||
//!
|
//!
|
||||||
//! `fake_state(config)` returns a ready `AppState`. Each fake exposes its
|
//! `fake_state(config)` returns a ready `AppState`.
|
||||||
//! fixtures as public fields so tests can replace them.
|
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -37,9 +36,9 @@ pub fn fake_state(config: Config) -> AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FakeSystemd {
|
struct FakeSystemd {
|
||||||
pub units: Vec<UnitStatus>,
|
units: Vec<UnitStatus>,
|
||||||
pub timers: Vec<TimerInfo>,
|
timers: Vec<TimerInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FakeSystemd {
|
impl Default for FakeSystemd {
|
||||||
@@ -125,13 +124,13 @@ impl SystemdSource for FakeSystemd {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FakeJournal {
|
struct FakeJournal {
|
||||||
pub entries: Vec<JournalEntry>,
|
entries: Vec<JournalEntry>,
|
||||||
shutdown: CancellationToken,
|
shutdown: CancellationToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FakeJournal {
|
impl FakeJournal {
|
||||||
pub fn new(shutdown: CancellationToken) -> Self {
|
fn new(shutdown: CancellationToken) -> Self {
|
||||||
let e = |t: &str, p: Priority, unit: &str, msg: &str| JournalEntry {
|
let e = |t: &str, p: Priority, unit: &str, msg: &str| JournalEntry {
|
||||||
timestamp: ts(t),
|
timestamp: ts(t),
|
||||||
priority: Some(p),
|
priority: Some(p),
|
||||||
@@ -215,14 +214,14 @@ impl JournalSource for FakeJournal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FakeDocker {
|
struct FakeDocker {
|
||||||
pub containers: Vec<ContainerStatus>,
|
containers: Vec<ContainerStatus>,
|
||||||
pub logs: Vec<LogLine>,
|
logs: Vec<LogLine>,
|
||||||
shutdown: CancellationToken,
|
shutdown: CancellationToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FakeDocker {
|
impl FakeDocker {
|
||||||
pub fn new(shutdown: CancellationToken) -> Self {
|
fn new(shutdown: CancellationToken) -> Self {
|
||||||
let c = |id: &str, name: &str, state: &str, health: Option<&str>| ContainerStatus {
|
let c = |id: &str, name: &str, state: &str, health: Option<&str>| ContainerStatus {
|
||||||
summary: ContainerSummary {
|
summary: ContainerSummary {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
|
|||||||
@@ -32,8 +32,7 @@ pub struct SdJournal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SdJournal {
|
impl SdJournal {
|
||||||
/// Must not fail or block. Reads `sources.journal_namespace` and
|
/// Must not fail or block.
|
||||||
/// `limits.max_lines` from `config`. Follow tasks stop on `shutdown`.
|
|
||||||
pub fn new(config: &Config, shutdown: CancellationToken) -> Self {
|
pub fn new(config: &Config, shutdown: CancellationToken) -> Self {
|
||||||
let ns = config.sources.journal_namespace.trim();
|
let ns = config.sources.journal_namespace.trim();
|
||||||
Self {
|
Self {
|
||||||
@@ -80,7 +79,6 @@ fn unavailable(e: impl std::fmt::Display) -> AppError {
|
|||||||
AppError::Unavailable(format!("journal: {e}"))
|
AppError::Unavailable(format!("journal: {e}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Open the journal (inside the blocking thread) and apply unit/priority matches.
|
|
||||||
fn open_filtered(ns: Option<&str>, q: &JournalQuery) -> Result<Journal, AppError> {
|
fn open_filtered(ns: Option<&str>, q: &JournalQuery) -> Result<Journal, AppError> {
|
||||||
let opts = OpenOptions::default();
|
let opts = OpenOptions::default();
|
||||||
let mut j = match ns {
|
let mut j = match ns {
|
||||||
|
|||||||
@@ -108,7 +108,6 @@ fn emit(logger: &SdkLogger, r: LogRec) {
|
|||||||
logger.emit(rec);
|
logger.emit(rec);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start the journal tasks and the docker reconciler.
|
|
||||||
pub fn spawn(state: AppState, provider: &SdkLoggerProvider) -> Vec<JoinHandle<()>> {
|
pub fn spawn(state: AppState, provider: &SdkLoggerProvider) -> Vec<JoinHandle<()>> {
|
||||||
let logger = provider.logger("skyg-linux-tracker");
|
let logger = provider.logger("skyg-linux-tracker");
|
||||||
let mut handles = Vec::new();
|
let mut handles = Vec::new();
|
||||||
|
|||||||
@@ -222,7 +222,6 @@ async fn collect(state: &AppState, prev: &Snapshot) -> Snapshot {
|
|||||||
snap
|
snap
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register the gauges and start the poll loop.
|
|
||||||
pub fn spawn(state: AppState, provider: &SdkMeterProvider) -> Vec<JoinHandle<()>> {
|
pub fn spawn(state: AppState, provider: &SdkMeterProvider) -> Vec<JoinHandle<()>> {
|
||||||
// Observations are computed once per poll; callbacks only filter.
|
// Observations are computed once per poll; callbacks only filter.
|
||||||
let current: Arc<RwLock<Arc<Vec<Obs>>>> = Arc::default();
|
let current: Arc<RwLock<Arc<Vec<Obs>>>> = Arc::default();
|
||||||
|
|||||||
Reference in new issue
Block a user