Proxy shell from the design: list, sidebar, header, empty states and instant Clear (#820)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5.5 authored and GitHub committed 2026-11-06 14:45:44 -07:00
1 parent c09ee5cc53
commit c629d4c66c
41 files changed
+3575 -714

No files matched your search

+50 -3
View File
@@ -21,6 +21,8 @@ pub struct ClientApp {
pub bundle_id: Option<String>,
/// The outermost `.app` bundle's path, or the executable's path when there is no bundle.
pub path: String,
/// The working directory of a process outside an app, as it was when first looked up.
pub cwd: Option<String>,
}
/// Finds the app behind each client connection, once per connection, off the caller's thread.
@@ -379,7 +381,14 @@ mod imp {
{
return app.clone();
}
let app = proc::executable(pid).map(|exe| describe(pid, &exe));
let app = proc::executable(pid).map(|exe| {
let mut app = describe(pid, &exe);
// A program's folder names it; an app's is wherever launchd started it.
if app.bundle_id.is_none() {
app.cwd = proc::cwd(pid);
}
app
});
if let Some(key) = key {
self.apps.insert(key, app.clone());
}
@@ -392,7 +401,13 @@ mod imp {
match outermost_bundle(exe) {
Some(bundle) => {
let (name, bundle_id) = bundle_info(bundle);
ClientApp { pid, name, bundle_id, path: bundle.to_string_lossy().into_owned() }
ClientApp {
pid,
name,
bundle_id,
path: bundle.to_string_lossy().into_owned(),
cwd: None,
}
}
None => ClientApp {
pid,
@@ -402,6 +417,7 @@ mod imp {
.unwrap_or_else(|| exe.to_string_lossy().into_owned()),
bundle_id: None,
path: exe.to_string_lossy().into_owned(),
cwd: None,
},
}
}
@@ -698,6 +714,35 @@ mod proc {
Some((info.pbi_start_tvsec, info.pbi_start_tvusec))
}
/// The working directory of `pid`, which only its own user or root can read.
pub fn cwd(pid: i32) -> Option<String> {
let mut info = std::mem::MaybeUninit::<libc::proc_vnodepathinfo>::zeroed();
let size = size_of::<libc::proc_vnodepathinfo>();
let written = unsafe {
libc::proc_pidinfo(
pid,
libc::PROC_PIDVNODEPATHINFO,
0,
info.as_mut_ptr().cast::<c_void>(),
size as c_int,
)
};
if written as usize != size {
return None;
}
let info = unsafe { info.assume_init() };
// A NUL-terminated C string, in libc's nested array.
let bytes: Vec<u8> = info
.pvi_cdir
.vip_path
.iter()
.flatten()
.map(|&c| c as u8)
.take_while(|&b| b != 0)
.collect();
(!bytes.is_empty()).then(|| String::from_utf8_lossy(&bytes).into_owned())
}
/// The path of the executable `pid` runs.
pub fn executable(pid: i32) -> Option<PathBuf> {
let mut buf = vec![0u8; libc::PROC_PIDPATHINFO_MAXSIZE as usize];
@@ -876,6 +921,7 @@ mod tests {
name: "Fake Browser".into(),
bundle_id: Some("com.example.browser".into()),
path: bundle.to_string_lossy().into_owned(),
cwd: None,
}
);
}
@@ -890,7 +936,8 @@ mod tests {
pid: 7,
name: "curl".into(),
bundle_id: None,
path: "/usr/bin/curl".into()
path: "/usr/bin/curl".into(),
cwd: None,
}
);
}
+120 -8
View File
@@ -77,14 +77,26 @@ pub fn open_blobs_read_only(path: &Path) -> Result<BlobManager> {
Ok(BlobManager::new(readers.clone(), readers))
}
/// `sidebar_path(url)`, the app sidebar's place for a URL, for filtering by its nodes.
/// The functions filters use: `sidebar_path(url)`, the app sidebar's place for a URL, and
/// `under_site(url, site)`, for filtering by its nodes; `folder_name(dir)`, for naming a program
/// by its folder; and `is_system(bundle_id, path)`, for macOS's own daemons.
fn add_functions(conn: &mut rusqlite::Connection) -> rusqlite::Result<()> {
conn.create_scalar_function(
"sidebar_path",
1,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC,
|ctx| Ok(crate::flows::sidebar_path(&ctx.get::<String>(0)?)),
)
let flags = FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC;
conn.create_scalar_function("sidebar_path", 1, flags, |ctx| {
Ok(crate::flows::sidebar_path(&ctx.get::<String>(0)?))
})?;
conn.create_scalar_function("under_site", 2, flags, |ctx| {
Ok(crate::flows::under_site(&ctx.get::<String>(0)?, &ctx.get::<String>(1)?))
})?;
conn.create_scalar_function("folder_name", 1, flags, |ctx| {
let dir = ctx.get::<Option<String>>(0)?;
Ok(crate::flows::folder_name(dir.as_deref()).map(str::to_string))
})?;
conn.create_scalar_function("is_system", 2, flags, |ctx| {
let bundle_id = ctx.get::<Option<String>>(0)?;
let path = ctx.get::<Option<String>>(1)?;
Ok(crate::flows::is_system(bundle_id.as_deref(), path.as_deref()))
})
}
fn read_only_pool(path: &Path) -> Result<SqlitePool> {
@@ -163,6 +175,26 @@ mod tests {
let failed =
crate::flows::FlowFilter { status: Some("error".into()), ..Default::default() };
assert_eq!(crate::flows::list(&db, &failed).unwrap().len(), 0);
// Filters by app work without the folder column, and count without the clear mark.
drop(db);
let conn = Connection::open(&path).unwrap();
conn.execute_batch(
"ALTER TABLE flows ADD COLUMN client_name TEXT;
ALTER TABLE flows ADD COLUMN client_bundle_id TEXT;
ALTER TABLE flows ADD COLUMN client_path TEXT;
ALTER TABLE flows DROP COLUMN client_cwd;
DROP TABLE flow_clear_mark;
UPDATE flows SET client_name = 'curl', client_path = '/usr/bin/curl';",
)
.unwrap();
drop(conn);
let db = ProxyQueryManager::open_read_only(&path).unwrap();
let filter =
crate::flows::FlowFilter { app: Some(vec!["curl".into()]), ..Default::default() };
let (page, total, matching) = crate::flows::page(&db, &filter).unwrap();
assert_eq!((page.len(), total, matching), (1, 1, Some(1)));
assert_eq!(page[0].number, 1);
}
#[test]
@@ -198,7 +230,16 @@ mod tests {
.unwrap()
.map(|t| t.unwrap())
.collect();
assert_eq!(tables, vec!["_sqlx_migrations", "flow_messages", "flows", "grpc_schemas"]);
assert_eq!(
tables,
vec![
"_sqlx_migrations",
"flow_clear_mark",
"flow_messages",
"flows",
"grpc_schemas"
]
);
let flow_columns = columns(&conn, "flows");
assert!(flow_columns.contains(&"response_truncated".to_string()));
@@ -207,6 +248,7 @@ mod tests {
"client_name",
"client_bundle_id",
"client_path",
"client_cwd",
"cancelled",
"stream_kind",
"message_count",
@@ -230,4 +272,74 @@ mod tests {
let journal_mode: String = conn.query_row("PRAGMA journal_mode", [], |r| r.get(0)).unwrap();
assert_eq!(journal_mode, "wal");
}
#[test]
fn takes_the_folder_and_clear_mark_after_the_messages_migration() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("proxy.db");
// As a build with every migration up to the messages one left it, with a stream in it.
let conn = Connection::open(&path).unwrap();
conn.execute_batch(
"CREATE TABLE _sqlx_migrations (
version TEXT PRIMARY KEY,
description TEXT NOT NULL,
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
);",
)
.unwrap();
let mut files: Vec<_> = MIGRATIONS
.files()
.map(|f| (f.path().to_string_lossy().into_owned(), f.contents_utf8().unwrap()))
.filter(|(name, _)| name.as_str() < "00000007")
.collect();
files.sort();
assert_eq!(files.len(), 6);
for (name, sql) in files {
conn.execute_batch(sql).unwrap();
conn.execute(
"INSERT INTO _sqlx_migrations (version, description) VALUES (?, ?)",
[&name[..8], &name],
)
.unwrap();
}
conn.execute_batch(
"INSERT INTO flows (id, url, method, stream_kind, message_count, client_path)
VALUES ('fl_ws', 'wss://example.com/socket', 'GET', 'websocket', 2, '/bin/node');
INSERT INTO flow_messages (id, flow_id, seq, direction, kind, timestamp)
VALUES ('fm_1', 'fl_ws', 1, 'client', 'text', CURRENT_TIMESTAMP);",
)
.unwrap();
drop(conn);
drop(ProxyQueryManager::new(&path).unwrap());
let conn = Connection::open(&path).unwrap();
assert!(columns(&conn, "flows").contains(&"client_cwd".to_string()));
let applied: Vec<String> = conn
.prepare("SELECT version FROM _sqlx_migrations ORDER BY version")
.unwrap()
.query_map([], |r| r.get(0))
.unwrap()
.map(|v| v.unwrap())
.collect();
assert_eq!(&applied[applied.len() - 2..], ["00000007", "00000008"]);
let mark_columns = columns(&conn, "flow_clear_mark");
assert_eq!(mark_columns, ["id", "max_rowid", "cleaner", "cleaner_at"]);
drop(conn);
// The stream and its messages come through, and clearing then deleting takes both.
let db = ProxyQueryManager::new(&path).unwrap();
let blobs = yaak_models::init_blob_store(dir.path().join("blobs.sqlite")).unwrap();
let listed = crate::flows::list(&db, &Default::default()).unwrap();
assert_eq!(listed.len(), 1);
assert_eq!((listed[0].id.as_str(), listed[0].message_count), ("fl_ws", 2));
assert_eq!(crate::messages::count(&db, "fl_ws").unwrap(), 1);
assert_eq!(crate::flows::clear(&db).unwrap(), 1);
assert_eq!(
crate::flows::delete_cleared(&db, &blobs, 500, "cln_test"),
Ok(crate::flows::Cleaned::Done)
);
assert_eq!(crate::messages::count(&db, "fl_ws").unwrap(), 0);
}
}
+583 -54
View File
@@ -1,6 +1,6 @@
use crate::bodies;
use crate::db::ProxyQueryManager;
use crate::models::{Flow, FlowBody, FlowIden};
use crate::models::{Flow, FlowBody};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use rusqlite::types::Value;
@@ -22,13 +22,25 @@ pub struct FlowFilter {
#[ts(optional)]
pub host: Option<String>,
/// Only flows under one of these hosts or paths, as the app's sidebar names them:
/// `api.stripe.com` or `api.stripe.com/v1/charges`, with any port after the host.
/// `api.stripe.com` or `api.stripe.com/v1/charges`, with any port after the host. A host
/// written `*.github.com` is a site: `github.com` and every host below it.
#[ts(optional)]
pub under: Option<Vec<String>>,
/// Only flows from one of these apps, by bundle id, or by path for a process outside an
/// app bundle.
/// Only flows from one of these apps: by bundle id, by path for a process outside an app
/// bundle, by name in any case (`slack`), or, for a process outside an app, by the folder
/// it ran in (`fieldwork`).
#[ts(optional)]
pub app: Option<Vec<String>>,
/// Only flows from macOS's own daemons (true), or none of them (false), as [`is_system`]
/// tells them by their executable.
#[ts(optional)]
pub system: Option<bool>,
/// Only flows one of these groups covers, each judged on its own: several sidebar rows
/// selected, `@app:Slack @host:a.com OR @app:Chrome @host:b.com`, are two groups and not every
/// pairing of their apps and hosts. A group's `after`, `before` and `limit` do not apply. Not
/// carried by a query string.
#[ts(optional)]
pub any: Option<Vec<FlowFilter>>,
/// The method, any case.
#[ts(optional)]
pub method: Option<String>,
@@ -50,8 +62,8 @@ pub struct FlowFilter {
}
impl FlowFilter {
/// The filter from a query string's `q`, `host`, `under`, `app`, `method`, `status`,
/// `limit`, `after` and `before`. `under` and `app` may repeat.
/// The filter from a query string's `q`, `host`, `under`, `app`, `system`, `method`,
/// `status`, `limit`, `after` and `before`. `under` and `app` may repeat.
pub fn from_query(query: &str) -> Result<Self, String> {
let mut filter = Self::default();
for (key, value) in url::form_urlencoded::parse(query.as_bytes()) {
@@ -61,6 +73,11 @@ impl FlowFilter {
"host" => filter.host = value,
"under" => filter.under.get_or_insert_default().extend(value),
"app" => filter.app.get_or_insert_default().extend(value),
"system" => {
filter.system = value
.map(|v| v.parse().map_err(|_| format!("Invalid system {v:?}")))
.transpose()?
}
"method" => filter.method = value,
"status" => filter.status = value,
"after" => filter.after = value,
@@ -79,7 +96,8 @@ impl FlowFilter {
pub fn to_query(&self) -> String {
let mut query = url::form_urlencoded::Serializer::new(String::new());
let limit = self.limit.map(|l| l.to_string());
for (key, value) in [("q", &self.q), ("host", &self.host)] {
let system = self.system.map(|s| s.to_string());
for (key, value) in [("q", &self.q), ("host", &self.host), ("system", &system)] {
if let Some(value) = value {
query.append_pair(key, value);
}
@@ -112,12 +130,20 @@ impl FlowFilter {
|| given(&self.status)
|| self.under.is_some()
|| self.app.is_some()
|| self.system.is_some()
|| self.any.as_ref().is_some_and(|groups| groups.iter().any(FlowFilter::narrows))
}
/// The SQL condition for every field but `after`, `before` and `limit`, and its parameters.
/// `grpc` says the database has gRPC statuses: one opened read-only before its migrations
/// have run may not.
fn condition(&self, grpc: bool) -> (String, Vec<Value>) {
/// The SQL condition for every field but `after`, `before` and `limit`, and its parameters,
/// for the database `conn` reads, which may be from before the folder or gRPC columns, when
/// read without migrating.
fn condition_in(&self, conn: &rusqlite::Connection) -> (String, Vec<Value>) {
self.condition(has_column(conn, "client_cwd"), has_column(conn, "grpc_status"))
}
/// [`Self::condition_in`], told which of the later columns the database has: `has_cwd`,
/// the folder a program ran in, and `grpc`, gRPC statuses.
fn condition(&self, has_cwd: bool, grpc: bool) -> (String, Vec<Value>) {
let mut sql = String::from("1");
let mut params: Vec<Value> = Vec::new();
for term in self.q.iter().flat_map(|q| q.split_whitespace()) {
@@ -145,6 +171,11 @@ impl FlowFilter {
// The node itself, or anything below it, by the sidebar's rules.
let mut any = Vec::new();
for node in under {
if let Some(site) = site_of_node(node) {
any.push("under_site(url, ?)");
params.push(site.into());
continue;
}
let node = normalize_node(node);
any.push("(sidebar_path(url) = ? OR substr(sidebar_path(url), 1, ?) = ?)");
let below = format!("{node}/");
@@ -159,8 +190,34 @@ impl FlowFilter {
}
if let Some(apps) = &self.app {
let marks = vec!["?"; apps.len()].join(", ");
sql.push_str(&format!(" AND coalesce(client_bundle_id, client_path) IN ({marks})"));
sql.push_str(&format!(
" AND (coalesce(client_bundle_id, client_path) IN ({marks}) \
OR lower(client_name) IN ({marks})"
));
params.extend(apps.iter().map(|a| a.clone().into()));
params.extend(apps.iter().map(|a| a.to_lowercase().into()));
if has_cwd {
sql.push_str(&format!(
" OR (client_bundle_id IS NULL AND lower(folder_name(client_cwd)) IN ({marks}))"
));
params.extend(apps.iter().map(|a| a.to_lowercase().into()));
}
sql.push(')');
}
match self.system {
Some(true) => sql.push_str(" AND is_system(client_bundle_id, client_path)"),
Some(false) => sql.push_str(" AND NOT is_system(client_bundle_id, client_path)"),
None => {}
}
// Each group whole, then any of them.
if let Some(groups) = self.any.as_ref().filter(|g| !g.is_empty()) {
let mut any = Vec::new();
for group in groups {
let (condition, group_params) = group.condition(has_cwd, grpc);
any.push(format!("({condition})"));
params.extend(group_params);
}
sql.push_str(&format!(" AND ({})", any.join(" OR ")));
}
if let Some(method) = self.method.as_deref().filter(|m| !m.is_empty()) {
sql.push_str(" AND lower(method) = ?");
@@ -231,6 +288,75 @@ fn normalize_node(node: &str) -> String {
}
}
/// The site a sidebar node names, `github.com` for `*.github.com`, lowercased. None for a host or
/// a path.
fn site_of_node(node: &str) -> Option<String> {
let site = node.strip_prefix("*.")?;
(!site.is_empty() && !site.contains('/')).then(|| site.to_ascii_lowercase())
}
/// `url`'s host, without its port, is `site` or a host below it: `api.github.com` is under
/// `github.com`.
pub fn under_site(url: &str, site: &str) -> bool {
let path = sidebar_path(url);
let host = path.split('/').next().unwrap_or_default();
let host = strip_port(host);
host == site || host.strip_suffix(site).is_some_and(|rest| rest.ends_with('.'))
}
/// `host` without a `:port` after it, keeping an IPv6 address's brackets.
fn strip_port(host: &str) -> &str {
if host.starts_with('[') {
return host.find(']').map_or(host, |end| &host[..=end]);
}
host.rsplit_once(':').map_or(host, |(h, _)| h)
}
/// The last part of a directory's path, `fieldwork` for `/Users/me/src/fieldwork`. None for the
/// root, where daemons run, or no path.
pub fn folder_name(path: Option<&str>) -> Option<&str> {
let name = path?.trim_end_matches('/').rsplit('/').next()?;
(!name.is_empty()).then_some(name)
}
/// Where macOS keeps its own daemons and tools.
const SYSTEM_DIRS: &[&str] = &[
"/System/",
"/usr/libexec/",
"/usr/sbin/",
"/sbin/",
"/Library/Apple/",
];
/// Daemons that are macOS's own wherever they run from.
const SYSTEM_PROGRAMS: &[&str] = &[
"akd",
"apsd",
"cloudd",
"mDNSResponder",
"nsurlsessiond",
"ocspd",
"softwareupdated",
"trustd",
];
/// A flow came from one of macOS's own daemons rather than an app or a program someone ran: a
/// process outside an app bundle whose executable is under `/System`, `/usr/libexec` and the
/// like, or one of a few daemons by name. WebKit's helper processes, which also live under
/// `/System`, send apps' traffic and are not. `isSystemFlow` in the app's `lib/search.ts`
/// follows the same rules.
pub fn is_system(bundle_id: Option<&str>, path: Option<&str>) -> bool {
let Some(path) = path else { return false };
if bundle_id.is_some() {
return false;
}
let name = path.rsplit('/').next().unwrap_or_default();
if name.starts_with("com.apple.WebKit.") {
return false;
}
SYSTEM_DIRS.iter().any(|dir| path.starts_with(dir)) || SYSTEM_PROGRAMS.contains(&name)
}
/// The SQL condition for a status term: `404` exactly, `40` as the start of one, or a class
/// such as `4xx`. None when the term is not one.
fn status_condition(term: &str, params: &mut Vec<Value>) -> Option<String> {
@@ -276,13 +402,17 @@ pub fn page(
}
/// The database has the gRPC status column, added by a later migration.
fn has_grpc_status(conn: &rusqlite::Connection) -> bool {
conn.prepare("SELECT grpc_status FROM flows LIMIT 0").is_ok()
/// The flows table has `column`: a database opened read-only before its migrations have run may
/// not.
fn has_column(conn: &rusqlite::Connection, column: &str) -> bool {
conn.prepare(&format!("SELECT {column} FROM flows LIMIT 0")).is_ok()
}
fn list_in(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<Vec<Flow>, String> {
let (condition, mut params) = filter.condition(has_grpc_status(conn));
let mut sql = format!("SELECT * FROM flows WHERE {condition}");
let (condition, mut params) = filter.condition_in(conn);
// The rowid, for the flow's number.
let mut sql = format!("SELECT rowid, * FROM flows WHERE {condition}");
push_clear_mark(conn, &mut sql, &mut params);
if let Some(id) = &filter.after {
// By rowid, which only grows as flows are written.
let rowid: i64 = conn
@@ -312,44 +442,205 @@ fn list_in(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<Vec<Flow>
}
fn count_matching(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<usize, String> {
let (condition, params) = filter.condition(has_grpc_status(conn));
conn.query_row(
&format!("SELECT COUNT(*) FROM flows WHERE {condition}"),
rusqlite::params_from_iter(params),
|r| r.get::<_, i64>(0),
)
.map(|n| n as usize)
.map_err(|e| e.to_string())
let (condition, mut params) = filter.condition_in(conn);
let mut sql = format!("SELECT COUNT(*) FROM flows WHERE {condition}");
push_clear_mark(conn, &mut sql, &mut params);
conn.query_row(&sql, rusqlite::params_from_iter(params), |r| r.get::<_, i64>(0))
.map(|n| n as usize)
.map_err(|e| e.to_string())
}
/// Deletes every flow and their bodies, in a transaction in each database. The flows' deletion
/// commits only after their bodies' has, so when clearing the bodies fails the flows stay, to be
/// cleared again. Returns how many there were.
pub fn clear(db: &ProxyQueryManager, blobs: &BlobManager) -> Result<usize, String> {
/// Leaves out cleared flows, when a clear is pending; otherwise adds nothing, so SQLite keeps its
/// cheapest plan.
fn push_clear_mark(conn: &rusqlite::Connection, sql: &mut String, params: &mut Vec<Value>) {
let mark = clear_mark(conn);
if mark > 0 {
sql.push_str(" AND rowid > ?");
params.push(mark.into());
}
}
/// The rowid at or below which flows are cleared, or 0 when none are waiting to be deleted. 0
/// too for a database from before the mark, read without migrating.
pub fn clear_mark(conn: &rusqlite::Connection) -> i64 {
conn.query_row("SELECT max_rowid FROM flow_clear_mark WHERE id = 1", [], |r| r.get(0))
.unwrap_or(0)
}
/// Clears every flow at once: marks the newest flow's rowid, so no listing or count sees it or
/// any before it, and leaves the deleting to [`delete_cleared`]. Flows written afterwards are
/// unaffected. Returns how many flows were cleared.
pub fn clear(db: &ProxyQueryManager) -> Result<usize, String> {
db.with_tx(|c| {
let conn = c.conn().resolve();
let mut stmt = conn.prepare("SELECT id FROM flows")?;
let ids = stmt.query_map([], |r| r.get(0))?.collect::<Result<Vec<String>, _>>()?;
conn.execute("DELETE FROM flows", [])?;
conn.execute("DELETE FROM flow_messages", [])?;
// Only the deleted flows' bodies, so a flow still in flight keeps what it has written.
let mark = clear_mark(conn);
let (newest, cleared): (Option<i64>, i64) = conn.query_row(
"SELECT max(rowid), count(*) FROM flows WHERE rowid > ?",
[mark],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
if let Some(newest) = newest {
conn.execute(
"INSERT INTO flow_clear_mark (id, max_rowid) VALUES (1, ?) \
ON CONFLICT (id) DO UPDATE SET max_rowid = excluded.max_rowid",
[newest],
)?;
}
Ok::<_, yaak_database::Error>(cleared as usize)
})
.map_err(|e| format!("Failed to clear flows: {e}"))
}
/// Flows deleted per transaction while deleting cleared ones, so a flow being written never
/// waits long behind it.
pub const DELETE_BATCH: usize = 500;
/// How long a cleaner may go without a batch before another process takes the deleting over.
const CLEANER_LEASE_MS: i64 = 30_000;
/// What a batch of deleting cleared flows left.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cleaned {
/// More cleared flows wait.
More,
/// None are left, and the mark is gone.
Done,
/// Another process holds the lease and is deleting them.
Elsewhere,
}
/// Deletes up to `batch` cleared flows, their messages and their bodies, as `cleaner`, the
/// process's name for itself. Once none are left, removes the mark in the same transaction as
/// the last of them, so a clear that raised it meanwhile is never lost.
///
/// One process deletes at a time, the one holding the mark's lease, which each batch renews:
/// the app and the CLI share the database, and two cleaners could otherwise race. The bodies go
/// first, in their own transaction, and the rows after, so when deleting the bodies fails the
/// flows stay, to be deleted again. The two writers are never held together: a flow being
/// written holds the bodies' while it waits for the flows', and the other order would stall them
/// both.
pub fn delete_cleared(
db: &ProxyQueryManager,
blobs: &BlobManager,
batch: usize,
cleaner: &str,
) -> Result<Cleaned, String> {
let fail = |e: String| format!("Failed to delete cleared flows: {e}");
if clear_mark_in(db) == 0 {
return Ok(Cleaned::Done);
}
if !take_lease(db, cleaner).map_err(|e| fail(e.to_string()))? {
return Ok(Cleaned::Elsewhere);
}
let rows = read_cleared(db, batch).map_err(|e| fail(e.to_string()))?;
// Their bodies and their messages' payloads, by flow, so a flow still in flight keeps what
// it has written.
if !rows.is_empty() {
blobs
.with_tx(|b| {
for id in &ids {
for (_, id) in &rows {
b.delete_chunks(&Flow::body_id(id, FlowBody::Request))?;
b.delete_chunks(&Flow::body_id(id, FlowBody::Response))?;
b.delete_chunks_like(&format!("{id}.msg.%"))?;
}
Ok::<_, yaak_models::error::Error>(())
})
.map_err(|e| yaak_database::Error::Database(format!("flow bodies: {e}")))?;
Ok::<_, yaak_database::Error>(ids.len())
})
.map_err(|e| format!("Failed to clear flows: {e}"))
.map_err(|e| fail(format!("flow bodies: {e}")))?;
}
delete_rows(db, &rows, cleaner).map_err(|e| fail(e.to_string()))
}
fn now_ms() -> i64 {
chrono::Utc::now().timestamp_millis()
}
/// Takes or renews the lease on deleting cleared flows for `cleaner`. False while another
/// process holds it, or when nothing is cleared.
fn take_lease(db: &ProxyQueryManager, cleaner: &str) -> Result<bool, yaak_database::Error> {
db.with_tx(|c| {
let now = now_ms();
let taken = c.conn().resolve().execute(
"UPDATE flow_clear_mark SET cleaner = ?1, cleaner_at = ?2 WHERE id = 1 \
AND (cleaner IS NULL OR cleaner = ?1 OR cleaner_at IS NULL OR cleaner_at < ?3)",
rusqlite::params![cleaner, now, now - CLEANER_LEASE_MS],
)?;
Ok(taken == 1)
})
}
/// The next `batch` cleared flows, by rowid and id.
fn read_cleared(db: &ProxyQueryManager, batch: usize) -> rusqlite::Result<Vec<(i64, String)>> {
db.with_conn(|c| {
let conn = c.conn().resolve();
let mut stmt =
conn.prepare("SELECT rowid, id FROM flows WHERE rowid <= ? ORDER BY rowid LIMIT ?")?;
stmt.query_map([clear_mark(conn), batch as i64], |r| Ok((r.get(0)?, r.get(1)?)))?.collect()
})
}
/// Deletes `rows`, read a moment ago, and their messages, while `cleaner` still holds the lease
/// and only those still under the mark with the same id: read since, the mark may be gone and a
/// new flow have the rowid again. Drops the mark once nothing is left under it.
fn delete_rows(
db: &ProxyQueryManager,
rows: &[(i64, String)],
cleaner: &str,
) -> Result<Cleaned, yaak_database::Error> {
db.with_tx(|c| {
let conn = c.conn().resolve();
let held: Option<Option<String>> = conn
.query_row("SELECT cleaner FROM flow_clear_mark WHERE id = 1", [], |r| r.get(0))
.ok();
match held {
// The mark went meanwhile: a rowid read may be a new flow's now.
None => return Ok(Cleaned::Done),
Some(held) if held.as_deref() != Some(cleaner) => return Ok(Cleaned::Elsewhere),
Some(_) => {}
}
let mark = clear_mark(conn);
let mut delete =
conn.prepare("DELETE FROM flows WHERE rowid = ? AND id = ? AND rowid <= ?")?;
let mut delete_messages = conn.prepare("DELETE FROM flow_messages WHERE flow_id = ?")?;
for (rowid, id) in rows {
if delete.execute(rusqlite::params![rowid, id, mark])? > 0 {
delete_messages.execute([id])?;
}
}
// With none left under it, the mark goes in this transaction, since an empty table
// hands out rowids from 1 again and a new flow would fall under it.
let left: bool =
conn.query_row("SELECT EXISTS (SELECT 1 FROM flows WHERE rowid <= ?)", [mark], |r| {
r.get(0)
})?;
if left {
conn.execute("UPDATE flow_clear_mark SET cleaner_at = ? WHERE id = 1", [now_ms()])?;
Ok(Cleaned::More)
} else {
conn.execute("DELETE FROM flow_clear_mark WHERE id = 1", [])?;
Ok(Cleaned::Done)
}
})
}
/// [`clear_mark`], read from `db`.
pub fn clear_mark_in(db: &ProxyQueryManager) -> i64 {
db.with_conn(|c| clear_mark(c.conn().resolve()))
}
/// A flow by id, with its number. None once it is cleared, though its row may not be deleted
/// yet.
pub fn find(db: &ProxyQueryManager, flow_id: &str) -> Option<Flow> {
db.with_conn(|c| c.find_optional::<Flow>(FlowIden::Id, flow_id))
db.with_conn(|c| find_in(c.conn().resolve(), flow_id))
}
/// [`find`], inside a transaction or on a connection already held.
pub fn find_in(conn: &rusqlite::Connection, flow_id: &str) -> Option<Flow> {
conn.query_row(
"SELECT rowid, * FROM flows WHERE id = ? AND rowid > ?",
rusqlite::params![flow_id, clear_mark(conn)],
Flow::from_row,
)
.ok()
}
pub fn get(db: &ProxyQueryManager, flow_id: &str) -> Result<Flow, String> {
@@ -361,9 +652,14 @@ pub fn count(db: &ProxyQueryManager) -> Result<usize, String> {
}
fn count_in(conn: &rusqlite::Connection) -> Result<usize, String> {
conn.query_row("SELECT COUNT(*) FROM flows", [], |r| r.get::<_, i64>(0))
.map(|n| n as usize)
.map_err(|e| e.to_string())
// Without a clear pending, SQLite counts over its smallest index rather than the table.
let mark = clear_mark(conn);
let count = if mark == 0 {
conn.query_row("SELECT COUNT(*) FROM flows", [], |r| r.get::<_, i64>(0))
} else {
conn.query_row("SELECT COUNT(*) FROM flows WHERE rowid > ?", [mark], |r| r.get(0))
};
count.map(|n| n as usize).map_err(|e| e.to_string())
}
/// A body in JSON: text when it is UTF-8, base64 when it is not.
@@ -648,7 +944,7 @@ mod tests {
let plan: Vec<String> = db.with_conn(|c| {
let conn = c.conn().resolve();
let (condition, params) =
FlowFilter { q: Some("stripe".into()), ..Default::default() }.condition(true);
FlowFilter { q: Some("stripe".into()), ..Default::default() }.condition_in(conn);
let sql = format!(
"EXPLAIN QUERY PLAN SELECT * FROM flows WHERE {condition} \
ORDER BY started_at DESC, id DESC LIMIT 500"
@@ -732,7 +1028,7 @@ mod tests {
}
#[test]
fn clear_deletes_flows_and_their_bodies() {
fn clear_marks_flows_then_deletes_them_in_batches() {
use yaak_database::UpdateSource;
use yaak_models::blob_manager::BodyChunkWriter;
let dir = tempfile::tempdir().unwrap();
@@ -743,25 +1039,258 @@ mod tests {
writer.write(b"body").unwrap();
writer.finish().unwrap();
};
for id in ["fl_a", "fl_b"] {
let write_flow = |id: &str| {
write_body(id, FlowBody::Request);
write_body(id, FlowBody::Response);
let flow = Flow { id: id.into(), ..Default::default() };
db.with_tx(|tx| tx.upsert(&flow, &UpdateSource::Background)).unwrap();
};
let rows = || {
db.with_conn(|c| {
c.conn()
.resolve()
.query_row("SELECT count(*) FROM flows", [], |r| r.get::<_, i64>(0))
.unwrap()
})
};
let ids = |filter: &FlowFilter| -> Vec<String> {
list(&db, filter).unwrap().into_iter().map(|f| f.id).collect()
};
for id in ["fl_a", "fl_b", "fl_c"] {
write_flow(id);
}
// A flow still in flight has written a body but not its row.
write_body("fl_live", FlowBody::Request);
// Bodies that fail to clear keep their flows, to clear again.
let read_only = crate::db::open_blobs_read_only(&dir.path().join("blobs.sqlite")).unwrap();
assert!(clear(&db, &read_only).is_err());
assert_eq!(count(&db).unwrap(), 2);
assert_eq!(clear(&db, &blobs).unwrap(), 2);
// Cleared at once: no listing, count or lookup sees them, though their rows remain.
assert_eq!(clear(&db).unwrap(), 3);
assert_eq!(count(&db).unwrap(), 0);
let body_ids = blobs.connect().list_body_ids().unwrap();
assert_eq!(body_ids, vec![Flow::body_id("fl_live", FlowBody::Request)]);
assert_eq!(clear(&db, &blobs).unwrap(), 0);
assert_eq!(ids(&FlowFilter::default()), Vec::<String>::new());
assert!(find(&db, "fl_a").is_none());
assert_eq!(rows(), 3);
assert_eq!(db.with_conn(|c| clear_mark(c.conn().resolve())), 3);
// A flow written after the clear is unaffected, and a second clear takes only it.
write_flow("fl_d");
assert_eq!(ids(&FlowFilter::default()), vec!["fl_d"]);
assert_eq!(find(&db, "fl_d").map(|f| f.number), Some(4));
let narrowed = FlowFilter { q: Some("example".into()), ..Default::default() };
assert_eq!(page(&db, &narrowed).unwrap().1, 1);
// Bodies that fail to delete keep their flows, to delete again.
let read_only = crate::db::open_blobs_read_only(&dir.path().join("blobs.sqlite")).unwrap();
assert!(delete_cleared(&db, &read_only, 2, "cln_a").is_err());
assert_eq!(rows(), 4);
// In batches, the mark going with the last of them.
assert_eq!(delete_cleared(&db, &blobs, 2, "cln_a"), Ok(Cleaned::More));
assert_eq!(rows(), 2);
assert_eq!(delete_cleared(&db, &blobs, 2, "cln_a"), Ok(Cleaned::Done));
assert_eq!(rows(), 1);
assert_eq!(db.with_conn(|c| clear_mark(c.conn().resolve())), 0);
assert_eq!(ids(&FlowFilter::default()), vec!["fl_d"]);
let mut body_ids = blobs.connect().list_body_ids().unwrap();
body_ids.sort();
assert_eq!(
body_ids,
vec![
Flow::body_id("fl_d", FlowBody::Request),
Flow::body_id("fl_d", FlowBody::Response),
Flow::body_id("fl_live", FlowBody::Request),
]
);
assert_eq!(clear(&db).unwrap(), 1);
assert_eq!(clear(&db).unwrap(), 0);
assert_eq!(delete_cleared(&db, &blobs, DELETE_BATCH, "cln_a"), Ok(Cleaned::Done));
assert_eq!(rows(), 0);
// An empty table numbers from 1 again, which no stale mark may hide.
write_flow("fl_e");
assert_eq!(ids(&FlowFilter::default()), vec!["fl_e"]);
assert_eq!(delete_cleared(&db, &blobs, DELETE_BATCH, "cln_a"), Ok(Cleaned::Done));
assert_eq!(rows(), 1);
}
#[test]
fn one_cleaner_at_a_time_and_never_a_reused_rowid() {
use yaak_database::UpdateSource;
let dir = tempfile::tempdir().unwrap();
let db = ProxyQueryManager::new(&dir.path().join("proxy.db")).unwrap();
let blobs = yaak_models::init_blob_store(dir.path().join("blobs.sqlite")).unwrap();
let write_flow = |id: &str| {
let flow = Flow { id: id.into(), ..Default::default() };
db.with_tx(|tx| tx.upsert(&flow, &UpdateSource::Background)).unwrap().0
};
let rows = || -> Vec<String> {
db.with_conn(|c| {
let conn = c.conn().resolve();
let mut stmt = conn.prepare("SELECT id FROM flows ORDER BY rowid").unwrap();
stmt.query_map([], |r| r.get(0)).unwrap().map(|r| r.unwrap()).collect()
})
};
for id in ["fl_a", "fl_b", "fl_c"] {
write_flow(id);
}
assert_eq!(clear(&db).unwrap(), 3);
// The app takes the lease; the CLI, sharing the database, leaves the deleting to it.
assert_eq!(delete_cleared(&db, &blobs, 1, "cln_app"), Ok(Cleaned::More));
assert_eq!(delete_cleared(&db, &blobs, 1, "cln_cli"), Ok(Cleaned::Elsewhere));
assert_eq!(rows(), vec!["fl_b", "fl_c"]);
// A lease gone stale passes to the next cleaner.
let exec = |sql: &str| {
db.with_tx(|c| Ok::<_, yaak_database::Error>(c.conn().resolve().execute(sql, [])?))
.unwrap()
};
exec("UPDATE flow_clear_mark SET cleaner_at = 0");
assert_eq!(delete_cleared(&db, &blobs, 1, "cln_cli"), Ok(Cleaned::More));
assert_eq!(delete_cleared(&db, &blobs, 1, "cln_app"), Ok(Cleaned::Elsewhere));
// A batch read before the last one finished: by then the mark is gone, the table empty,
// and a new flow has the same rowid as the one read. It is not deleted.
let stale = read_cleared(&db, DELETE_BATCH).unwrap();
assert_eq!(stale.iter().map(|(_, id)| id.as_str()).collect::<Vec<_>>(), vec!["fl_c"]);
assert_eq!(delete_cleared(&db, &blobs, DELETE_BATCH, "cln_cli"), Ok(Cleaned::Done));
exec(&format!("INSERT INTO flows (rowid, id) VALUES ({}, 'fl_new')", stale[0].0));
assert_eq!(delete_rows(&db, &stale, "cln_cli").unwrap(), Cleaned::Done);
assert_eq!(rows(), vec!["fl_new"]);
// Under a new mark, the same rowid with another id is not the flow read either.
assert_eq!(clear(&db).unwrap(), 1);
exec("UPDATE flow_clear_mark SET cleaner = 'cln_cli'");
assert_eq!(delete_rows(&db, &stale, "cln_cli").unwrap(), Cleaned::More);
assert_eq!(rows(), vec!["fl_new"]);
}
#[test]
fn numbers_flows_by_rowid_and_keeps_it_on_update() {
use yaak_database::UpdateSource;
let dir = tempfile::tempdir().unwrap();
let db = ProxyQueryManager::new(&dir.path().join("proxy.db")).unwrap();
let write = |flow: &Flow| db.with_tx(|tx| tx.upsert(flow, &UpdateSource::Background));
let (a, _) = write(&Flow { id: "fl_a".into(), ..Default::default() }).unwrap();
let (b, _) = write(&Flow { id: "fl_b".into(), ..Default::default() }).unwrap();
assert_eq!((a.number, b.number), (1, 2));
let (a, created) = write(&Flow { status: Some(200), ..a }).unwrap();
assert_eq!((a.number, created), (1, false));
let numbers: Vec<i64> =
list(&db, &FlowFilter::default()).unwrap().iter().map(|f| f.number).collect();
assert_eq!(numbers, vec![2, 1]);
assert_eq!(find(&db, "fl_b").map(|f| f.number), Some(2));
}
#[test]
fn filters_by_app_name_folder_site_and_system() {
use yaak_database::UpdateSource;
let dir = tempfile::tempdir().unwrap();
let db = ProxyQueryManager::new(&dir.path().join("proxy.db")).unwrap();
let flows = [
Flow {
id: "fl_slack".into(),
url: "https://edgeapi.slack.com/cache".into(),
client_name: Some("Slack".into()),
client_bundle_id: Some("com.tinyspeck.slackmacgap".into()),
client_path: Some("/Applications/Slack.app".into()),
..Default::default()
},
Flow {
id: "fl_node".into(),
url: "https://api.github.com:443/repos".into(),
client_name: Some("node".into()),
client_path: Some("/opt/homebrew/bin/node".into()),
client_cwd: Some("/Users/me/src/fieldwork".into()),
..Default::default()
},
Flow {
id: "fl_daemon".into(),
url: "https://github.com/login".into(),
client_name: Some("nsurlsessiond".into()),
client_path: Some("/usr/libexec/nsurlsessiond".into()),
client_cwd: Some("/".into()),
..Default::default()
},
Flow {
id: "fl_none".into(),
url: "http://notgithub.com/".into(),
..Default::default()
},
];
for flow in &flows {
db.with_tx(|tx| tx.upsert(flow, &UpdateSource::Background)).unwrap();
}
let ids = |filter: FlowFilter| -> Vec<String> {
let mut ids: Vec<String> =
list(&db, &filter).unwrap().into_iter().map(|f| f.id).collect();
ids.sort();
ids
};
let app = |a: &str| FlowFilter { app: Some(vec![a.into()]), ..Default::default() };
assert_eq!(ids(app("slack")), vec!["fl_slack"]);
assert_eq!(ids(app("com.tinyspeck.slackmacgap")), vec!["fl_slack"]);
assert_eq!(ids(app("Fieldwork")), vec!["fl_node"]);
assert_eq!(ids(app("/opt/homebrew/bin/node")), vec!["fl_node"]);
let under = |n: &str| FlowFilter { under: Some(vec![n.into()]), ..Default::default() };
assert_eq!(ids(under("*.github.com")), vec!["fl_daemon", "fl_node"]);
assert_eq!(ids(under("api.github.com")), vec!["fl_node"]);
let system = |s| FlowFilter { system: Some(s), ..Default::default() };
assert_eq!(ids(system(true)), vec!["fl_daemon"]);
assert_eq!(ids(system(false)), vec!["fl_node", "fl_none", "fl_slack"]);
// Groups, as two sidebar rows under different apps write them, each judged whole:
// flattened, the apps and hosts would pair across rows and match both flows.
let group = |a: &str, n: &str| FlowFilter {
app: Some(vec![a.into()]),
under: Some(vec![n.into()]),
..Default::default()
};
let crossed = FlowFilter {
any: Some(vec![
group("fieldwork", "github.com"),
group("nsurlsessiond", "api.github.com"),
]),
..Default::default()
};
assert_eq!(ids(crossed.clone()), Vec::<String>::new());
assert_eq!(page(&db, &crossed).unwrap().2, Some(0));
let flattened = FlowFilter {
app: Some(vec!["fieldwork".into(), "nsurlsessiond".into()]),
under: Some(vec!["github.com".into(), "api.github.com".into()]),
..Default::default()
};
assert_eq!(ids(flattened), vec!["fl_daemon", "fl_node"]);
let either = FlowFilter {
any: Some(vec![
group("fieldwork", "api.github.com"),
FlowFilter { app: Some(vec!["slack".into()]), ..Default::default() },
]),
q: Some("https".into()),
..Default::default()
};
assert_eq!(ids(either), vec!["fl_node", "fl_slack"]);
}
#[test]
fn tells_system_daemons_apart() {
assert!(is_system(None, Some("/usr/libexec/nsurlsessiond")));
assert!(is_system(None, Some("/System/Library/PrivateFrameworks/X.framework/geod")));
assert!(is_system(None, Some("/somewhere/else/trustd")));
assert!(!is_system(None, Some("/usr/bin/curl")));
assert!(!is_system(None, Some("/opt/homebrew/bin/node")));
// An app in /System is an app, and WebKit's helpers send apps' traffic.
assert!(!is_system(Some("com.apple.Music"), Some("/System/Applications/Music.app")));
assert!(!is_system(
None,
Some(
"/System/Library/Frameworks/WebKit.framework/XPCServices/com.apple.WebKit.Networking.xpc/Contents/MacOS/com.apple.WebKit.Networking"
)
));
assert!(!is_system(None, None));
assert_eq!(folder_name(Some("/Users/me/src/fieldwork/")), Some("fieldwork"));
assert_eq!(folder_name(Some("/")), None);
assert!(under_site("https://api.github.com:8443/x", "github.com"));
assert!(!under_site("https://notgithub.com/", "github.com"));
}
#[test]
+136 -10
View File
@@ -27,7 +27,7 @@ use crate::grpc::{DecodedGrpcMessage, GrpcSchema, GrpcState};
use crate::helper::HelperState;
use crate::macos::{SystemProxyBackend, SystemProxyStatus};
use crate::messages::FlowMessage;
use crate::models::{Flow, FlowBody, FlowIden, ModelPayload, ProxyHeader, StreamKind, find_header};
use crate::models::{Flow, FlowBody, ModelPayload, ProxyHeader, StreamKind, find_header};
use crate::save::{ClientDb, SaveFlowRequest, SaveFlowResponse, WorkspaceSummary};
use crate::settings::{HttpVersion, ProxySettings};
use base64::Engine;
@@ -40,7 +40,7 @@ use std::io::Write;
use std::net::{SocketAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::sync::{Arc, Mutex, OnceLock, PoisonError};
use std::time::{Duration, SystemTime};
use ts_rs::TS;
use yaak_database::{ModelChangeEvent, UpdateSource, UpsertModelInfo};
@@ -72,6 +72,12 @@ pub struct ProxyCtx {
system_proxy_on: Arc<AtomicBool>,
/// Why the engine last failed to start, until it starts.
start_error: Arc<Mutex<Option<String>>>,
/// Cleared flows are being deleted in the background. Held while the deleter decides it is
/// done, so a clear either sees it running or starts another.
deleting_cleared: Arc<Mutex<bool>>,
/// This context's name as the one deleting cleared flows, which the database leases to one
/// process at a time.
cleaner_id: String,
pub db: ProxyQueryManager,
pub blobs: BlobManager,
pub events: RpcEventEmitter,
@@ -98,11 +104,23 @@ impl ProxyCtx {
recording: Arc::new(Mutex::new(RecordingLog::default())),
system_proxy_on: Arc::new(AtomicBool::new(false)),
start_error: Arc::new(Mutex::new(None)),
deleting_cleared: Arc::new(Mutex::new(false)),
cleaner_id: yaak_database::generate_prefixed_id("cln"),
db,
blobs,
events,
grpc: GrpcState::default(),
})
}
// A clear the last run did not finish deleting.
.with_cleared_deleting())
}
fn with_cleared_deleting(self) -> Self {
let pending = self.db.with_conn(|c| flows::clear_mark(c.conn().resolve())) > 0;
if pending {
self.delete_cleared();
}
self
}
/// The protoc that compiles `.proto` files attached for gRPC decoding, as the app bundles
@@ -596,7 +614,7 @@ impl ProxyCtx {
self.db
.with_tx(|tx| {
// Cleared while saving: recording it would bring the flow back without bodies.
if tx.find_optional::<Flow>(FlowIden::Id, flow_id).is_none() {
if flows::find_in(tx.conn().resolve(), flow_id).is_none() {
return Ok(());
}
let (flow, created) = tx.upsert(&flow, &UpdateSource::Background)?;
@@ -611,15 +629,89 @@ impl ProxyCtx {
Ok(saved)
}
/// Deletes every flow and its bodies, and tells the app so its list empties. Returns how
/// many there were.
/// Clears every flow at once and tells the app so its list empties, then deletes them and
/// their bodies in the background. Returns how many there were.
pub fn clear_flows(&self) -> Result<usize, String> {
let cleared = flows::clear(&self.db, &self.blobs)?;
let cleared = flows::clear(&self.db)?;
// After the commit, so the app reloads what is left. Flows written before the clear sent
// their events before it began.
self.events.emit("flows_cleared", &FlowsClearedPayload {});
self.delete_cleared();
Ok(cleared)
}
/// Deletes cleared flows on a background thread, a batch per transaction, unless one is
/// already doing so. Yields the writer between batches, so new flows are written meanwhile.
fn delete_cleared(&self) {
if !claim(&self.deleting_cleared) {
return;
}
let (db, blobs, running, cleaner) = (
self.db.clone(),
self.blobs.clone(),
self.deleting_cleared.clone(),
self.cleaner_id.clone(),
);
let spawned = std::thread::Builder::new().name("proxy-clear".into()).spawn(move || {
lower_thread_priority();
let mut retry = DELETE_PAUSE;
loop {
match flows::delete_cleared(&db, &blobs, flows::DELETE_BATCH, &cleaner) {
Ok(flows::Cleaned::More) => {
retry = DELETE_PAUSE;
std::thread::sleep(DELETE_PAUSE);
}
// Another process, the app or the CLI, is deleting them; checked again now
// and then, in case it stops before it is done.
Ok(flows::Cleaned::Elsewhere) => std::thread::sleep(CLEANER_WAIT),
Ok(flows::Cleaned::Done) => {
*running.lock().unwrap_or_else(PoisonError::into_inner) = false;
// A clear since the last batch saw this running and left its flows to
// it, so they are deleted here unless another thread has taken over.
if flows::clear_mark_in(&db) == 0 || !claim(&running) {
return;
}
}
// A busy database, most likely: tried again, waiting longer each time, so a
// clear is never left half done until the next launch.
Err(e) => {
warn!("{e}");
std::thread::sleep(retry);
retry = (retry * 4).min(DELETE_RETRY_MAX);
}
}
}
});
if let Err(e) = spawned {
warn!("Failed to start deleting cleared flows: {e}");
*self.deleting_cleared.lock().unwrap_or_else(PoisonError::into_inner) = false;
}
}
}
/// Sets `flag`, returning false when it was already set.
fn claim(flag: &Mutex<bool>) -> bool {
let mut set = flag.lock().unwrap_or_else(PoisonError::into_inner);
!std::mem::replace(&mut *set, true)
}
/// How long deleting cleared flows rests between batches, leaving the database to new flows.
const DELETE_PAUSE: Duration = Duration::from_millis(5);
/// The longest deleting cleared flows waits before trying a failed batch again.
const DELETE_RETRY_MAX: Duration = Duration::from_secs(5);
/// How often a process checks on cleared flows another process is deleting.
const CLEANER_WAIT: Duration = Duration::from_secs(15);
/// Runs the calling thread below the app's own work, where the platform has priorities: macOS's
/// utility class, not background, whose disk throttling can stall a delete for as long as the
/// Mac is busy.
fn lower_thread_priority() {
#[cfg(target_os = "macos")]
unsafe {
libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_UTILITY, 0);
}
}
/// Something accepts connections on `127.0.0.1:<port>`.
@@ -1399,8 +1491,10 @@ fn settings(ctx: &ProxyCtx) -> Result<ProxySettings, RpcError> {
ctx.settings().map_err(rpc_error)
}
/// A flow by id, with its number; not one that is cleared, though its row may wait to be
/// deleted.
fn find_flow(ctx: &ProxyCtx, flow_id: &str) -> Result<Flow, RpcError> {
ctx.db.with_conn(|db| db.find_one::<Flow>(FlowIden::Id, flow_id)).map_err(rpc_error)
flows::get(&ctx.db, flow_id).map_err(rpc_error)
}
fn rpc_error(e: impl ToString) -> RpcError {
@@ -1853,6 +1947,7 @@ impl FlowSink {
flow.client_name = Some(app.name);
flow.client_bundle_id = app.bundle_id;
flow.client_path = Some(app.path);
flow.client_cwd = app.cwd;
}
let persisted = f.persisted;
let written = self.db.with_tx(|ctx| self.write_flow(ctx, flow, persisted));
@@ -1879,6 +1974,7 @@ impl FlowSink {
flow.client_name = Some(app.name);
flow.client_bundle_id = app.bundle_id;
flow.client_path = Some(app.path);
flow.client_cwd = app.cwd;
}
let written = self.db.with_tx(|ctx| self.write_flow(ctx, flow, f.persisted));
match written {
@@ -1898,7 +1994,8 @@ impl FlowSink {
persisted: bool,
) -> Result<bool, yaak_database::Error> {
if persisted {
let Some(stored) = ctx.find_optional::<Flow>(FlowIden::Id, &flow.id) else {
// Not one cleared since, whose row may wait to be deleted.
let Some(stored) = flows::find_in(ctx.conn().resolve(), &flow.id) else {
return Ok(false);
};
flow.saved_request_id = stored.saved_request_id;
@@ -1926,8 +2023,14 @@ impl FlowSink {
}
}
/// The flow is stored and not cleared: a cleared one's row may wait to be deleted.
fn flow_exists(conn: &rusqlite::Connection, flow_id: &str) -> bool {
conn.query_row("SELECT 1 FROM flows WHERE id = ?1", [flow_id], |_| Ok(())).is_ok()
conn.query_row(
"SELECT 1 FROM flows WHERE id = ?1 AND rowid > ?2",
rusqlite::params![flow_id, flows::clear_mark(conn)],
|_| Ok(()),
)
.is_ok()
}
fn clamp_ms(ms: u64) -> i32 {
@@ -2176,8 +2279,28 @@ mod tests {
let names: Vec<_> = event_rx.try_iter().map(|e| e.event).collect();
assert_eq!(names.iter().filter(|n| **n == "message_write").count(), 3, "{names:?}");
// Gone from listings at once, and with its messages and their payloads once the
// background delete has run.
ctx.clear_flows().unwrap();
assert_eq!(flows::count(&ctx.db).unwrap(), 0);
wait_for_cleared(&ctx);
assert!(!ctx.blobs.connect().body_exists(&stored[1].blob_id()).unwrap());
let left: i64 = ctx.db.with_conn(|c| {
c.conn()
.resolve()
.query_row("SELECT count(*) FROM flow_messages", [], |r| r.get(0))
.unwrap()
});
assert_eq!(left, 0);
}
/// Waits for the background delete a clear starts to finish.
fn wait_for_cleared(ctx: &ProxyCtx) {
let deadline = std::time::Instant::now() + Duration::from_secs(10);
while ctx.db.with_conn(|c| flows::clear_mark(c.conn().resolve())) > 0 {
assert!(std::time::Instant::now() < deadline, "cleared flows were not deleted");
std::thread::sleep(Duration::from_millis(10));
}
}
#[test]
@@ -2204,6 +2327,9 @@ mod tests {
drop(tx);
event_loop.join().unwrap();
assert_eq!(flows::count(&ctx.db).unwrap(), 0, "not brought back by its end");
// The messages written before the clear go with the background delete.
wait_for_cleared(&ctx);
assert_eq!(flows::count(&ctx.db).unwrap(), 0, "not brought back by its end");
assert_eq!(messages::count(&ctx.db, &flow.id).unwrap(), 0);
let blobs = ctx.blobs.connect().list_body_ids().unwrap();
assert!(blobs.iter().all(|b| !b.starts_with(&flow.id)), "{blobs:?}");
+11
View File
@@ -23,6 +23,10 @@ pub struct ProxyHeader {
#[enum_def(table_name = "flows")]
pub struct Flow {
pub id: String,
/// The flow's short handle, `#1429`: its rowid, which grows as flows are written and is kept
/// across updates. Zero when read without it.
#[ts(type = "number")]
pub number: i64,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub started_at: NaiveDateTime,
@@ -66,6 +70,9 @@ pub struct Flow {
pub client_bundle_id: Option<String>,
/// Its app bundle's path, or its executable's outside an app.
pub client_path: Option<String>,
/// The working directory of a process outside an app, whose folder names it: `node` in
/// `fieldwork`.
pub client_cwd: Option<String>,
/// The flow carries a stream of messages, listed with `list_messages`. It is written as the
/// stream opens and again as messages land; it is open until `elapsedMs`, `error` or
/// `cancelled` is set.
@@ -222,6 +229,7 @@ impl UpsertModelInfo for Flow {
(ClientName, self.client_name.into()),
(ClientBundleId, self.client_bundle_id.into()),
(ClientPath, self.client_path.into()),
(ClientCwd, self.client_cwd.into()),
(FlowIden::StreamKind, self.stream_kind.map(|k| k.as_str()).into()),
(MessageCount, self.message_count.into()),
(GrpcService, self.grpc_service.into()),
@@ -257,6 +265,7 @@ impl UpsertModelInfo for Flow {
FlowIden::ClientName,
FlowIden::ClientBundleId,
FlowIden::ClientPath,
FlowIden::ClientCwd,
FlowIden::StreamKind,
FlowIden::MessageCount,
FlowIden::GrpcService,
@@ -274,6 +283,7 @@ impl UpsertModelInfo for Flow {
let response_headers: String = r.get("response_headers")?;
Ok(Self {
id: r.get("id")?,
number: added_column(r, "rowid")?.unwrap_or_default(),
created_at: r.get("created_at")?,
updated_at: r.get("updated_at")?,
started_at: r.get("started_at")?,
@@ -299,6 +309,7 @@ impl UpsertModelInfo for Flow {
client_name: added_column(r, "client_name")?,
client_bundle_id: added_column(r, "client_bundle_id")?,
client_path: added_column(r, "client_path")?,
client_cwd: added_column(r, "client_cwd")?,
stream_kind: added_column::<String>(r, "stream_kind")?
.and_then(|k| StreamKind::parse(&k)),
message_count: added_column(r, "message_count")?.unwrap_or_default(),