mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 19:47:38 +01:00
The three Python packages pinned ruff twice at different versions: requirements.txt at 0.16.7, .pre-commit-config.yaml at v0.15.2. Only the older one ever ran, so `select = ["ALL"]` was enforced two minor versions behind. Measured before aligning: v0.15.2 reports 0 findings, every 0.16.x reports CPY001 on each file -- 70 crdt-sync, 23 freedays, 71 gatelock, and no other rule. Resolved by adding the notice rather than ignoring the rule, so the gate keeps its teeth. Two gatelock modules sat at exactly the 250-line cap, so a one-line header tipped them to 251. Both are split along a real seam rather than shaved: _widget_fills.py colour arithmetic over LockConfig, no tkinter import _surfaces_focus.py selects among existing surfaces, never mutates them The focus split keeps _surfaces.py's documented containment (it stays the only module that creates, moves or destroys windows), and the static guard in test_recovery_invariants.py globs _surfaces*.py, so the new sibling is covered with no list to extend. widgets.py re-exports the fills helpers, which keeps gatelock.widgets the single public surface its tests import. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019kMvSMtsVMB3d2uoZ56pbp
59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""Pooled HTTP sessions, so every request does not re-handshake.
|
|
|
|
Every call site in this package used the module-level ``requests.get`` /
|
|
``put`` / ``post`` / ``delete`` helpers, each of which opens a throwaway
|
|
connection: fresh DNS, TCP and TLS on every single request. Measured against
|
|
the two real backends on 2026-08-21 that costs 82ms per request to
|
|
``api.github.com`` and 51ms to Realtime Database -- on a 27-request sync tick,
|
|
most of the wall clock.
|
|
|
|
A :class:`requests.Session` keeps the connection alive between requests, and
|
|
is a drop-in for the module-level helpers: same verb names, same signatures.
|
|
So each transport module binds one of these to the name ``requests`` and its
|
|
call sites are unchanged.
|
|
|
|
**One session per module, deliberately not one global.** The tests patch the
|
|
verb on the module attribute (``patch.object(fb.requests, "get", ...)``), and
|
|
several use sequence-consuming ``side_effect`` lists. Were all three modules
|
|
to share a single object, a call from one would consume another's queued
|
|
response and the failure would look like a logic bug. Per-module also happens
|
|
to be per-host here -- GitHub, Realtime Database, and the token endpoint are
|
|
three different origins -- so pooling loses nothing by the split.
|
|
|
|
Not thread-safe, in that :class:`requests.Session` is not documented to be;
|
|
this package spawns no threads and its consumers drive sync from one.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import requests
|
|
from requests.adapters import HTTPAdapter, Retry
|
|
|
|
_RETRY_TOTAL = 3
|
|
_RETRY_BACKOFF_SECONDS = 0.3
|
|
_RETRY_STATUSES = (429, 500, 502, 503, 504)
|
|
|
|
|
|
def new_session() -> requests.Session:
|
|
"""Return a connection-pooling session that retries transient GET failures.
|
|
|
|
Retries are restricted to ``GET``. A ``PUT`` here carries a Contents-API
|
|
``sha`` (and a Realtime Database write is a full-node replace); replaying
|
|
one whose response was merely lost would push a second write built on a
|
|
now-stale precondition, against a remote another device reads. Reads are
|
|
idempotent, so only they are replayed.
|
|
"""
|
|
session = requests.Session()
|
|
retry = Retry(
|
|
total=_RETRY_TOTAL,
|
|
backoff_factor=_RETRY_BACKOFF_SECONDS,
|
|
status_forcelist=_RETRY_STATUSES,
|
|
allowed_methods=frozenset({"GET"}),
|
|
raise_on_status=False,
|
|
)
|
|
adapter = HTTPAdapter(max_retries=retry)
|
|
session.mount("https://", adapter)
|
|
session.mount("http://", adapter)
|
|
return session
|