Serialize writers across processes; a failed update preserves old bytes.
Keep the sidecar lock across purge/replace: locking the replaced data inode
would let a concurrent writer acquire a different inode and lose envelopes.
No file or directory is created by construction or an empty read.
Source code in src/marianne/telemetry/spool.py
| def __init__(self, path: Path | None = None, *, consent_path: Path | None = None) -> None:
from marianne.telemetry.consent import ConsentRecord
self.path = path if path is not None else Path.home() / ".marianne/telemetry.jsonl"
consent_path = consent_path if consent_path is not None else ConsentRecord().path
reserved = {consent_path.resolve(),
consent_path.with_name(consent_path.name + ".lock").resolve()}
spool_paths = {self.path.resolve(),
self.path.with_name(self.path.name + ".lock").resolve(),
self.path.with_name(self.path.name + ".submit.lock").resolve()}
if reserved & spool_paths:
raise ValueError("Telemetry spool must not overlap consent state or locks")
# resolve() catches a symlinked spool whose link name hides a .lock
# target (or a symlinked parent directory renaming the final segment).
if self.path.name.endswith(".lock") or self.path.resolve().name.endswith(".lock"):
raise ValueError("Telemetry spool must not use a reserved .lock filename")
|
Methods:
read
Return staged records without changing or silently discarding them.
Source code in src/marianne/telemetry/spool.py
| def read(self) -> list[dict[str, Any]]:
"""Return staged records without changing or silently discarding them."""
if not self.path.exists() and not self.path.is_symlink():
return []
with self._locked():
return self._read()
|
count
Number of complete staged envelopes.
Source code in src/marianne/telemetry/spool.py
| def count(self) -> int:
"""Number of complete staged envelopes."""
return len(self.read())
|
append
append(envelope, *, authorize=None)
Durably append, rechecking permission after waiting for the lock.
Source code in src/marianne/telemetry/spool.py
| def append(
self, envelope: dict[str, Any], *, authorize: Callable[[], bool] | None = None,
) -> bool:
"""Durably append, rechecking permission after waiting for the lock."""
with self._locked():
if authorize is not None and not authorize():
return False
records = self._read()
records.append(envelope)
self._write(records)
return True
|
acknowledge
Prune only an unchanged submitted prefix, retaining concurrent appends.
Source code in src/marianne/telemetry/spool.py
| def acknowledge(self, snapshot: list[dict[str, Any]]) -> bool:
"""Prune only an unchanged submitted prefix, retaining concurrent appends."""
with self._locked():
records = self._read()
if records[:len(snapshot)] != snapshot:
return False
self._write(records[len(snapshot):])
return True
|
submission
Refuse overlapping flushes without holding the writer lock over HTTP.
Source code in src/marianne/telemetry/spool.py
| @contextmanager
def submission(self) -> Iterator[bool]:
"""Refuse overlapping flushes without holding the writer lock over HTTP."""
self.path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
path = self.path.with_name(self.path.name + ".submit.lock")
fd = os.open(path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
try:
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
yield False
else:
yield True
finally:
os.close(fd)
|
purge
Remove staged data, retaining the stable writer lock.
Source code in src/marianne/telemetry/spool.py
| def purge(self) -> None:
"""Remove staged data, retaining the stable writer lock."""
# A first append can hold the writer lock before the data file exists.
# Revocation must wait for that writer too, then remove its staged data.
lock_path = self.path.with_name(self.path.name + ".lock")
if not self.path.exists() and not self.path.is_symlink() and not lock_path.exists():
return
with self._locked():
self.path.unlink(missing_ok=True)
|