Skip to content

API Reference parrot.knowledge.retrieval.single_flight

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.retrieval.single_flight

View source

Single-flight regeneration lock, keyed on (page_id, section_kind).

Spec §6.2/§6.3. Guards WikiSection regeneration to prevent thundering herd on a hot module after a large merge. Keyed on (page_id, section_kind) — not page_id — so two requests needing different sections of the same hot page do not serialize (spec §6.2).

No Redis single-flight lock exists anywhere in knowledge/ or the eventbus integration to reuse. The pattern this module follows is redis-py's .lock(), as used in parrot/auth/oauth2_base.py:519 and parrot/auth/jira_oauth.py:523 — copied, not imported, since those modules solve an unrelated problem (OAuth token refresh). This is new code for T9, per spec §6.3.

Alias SectionKind

Re-exported from parrot.knowledge.retrieval.sections.SectionKind.

Class SingleFlight

De-duplicates concurrent regeneration calls for the same section.

Two layers of protection, both keyed on (page_id, section_kind):

  1. In-process de-duplication (always active): concurrent callers for the same key join the SAME in-flight coroutine rather than each invoking their own — true "single flight" semantics, not just mutual exclusion. This works with or without Redis.
  2. Cross-process Redis lock (only when a redis client is configured): an additional distributed lock so two separate worker processes also coalesce. With no Redis configured, this layer is skipped and the degradation is logged once.
Parameter Type Default Description
redis Any | None None An async redis client, or None.
lock_timeout float _DEFAULT_LOCK_TIMEOUT Redis lock timeout in seconds (unused when redis is None).

Attributes:

Attribute Type Description
redis An async redis-py client (redis.asyncio.Redis-shaped — needs only .lock()), or None to run in-process only.

Method SingleFlight.run_once

Run factory() at most once per (page_id, section_kind) in flight.

If a call for the same key is already running, this joins it instead of invoking factory again — the caller gets the SAME result the first caller gets (or the same exception).

run_once(page_id: str, section_kind: SectionKind, factory: Callable[[], Awaitable[_T]]) -> _T

Parameter Type Default Description
page_id str The WikiPage.page_id being regenerated.
section_kind SectionKind Which SectionKind is being regenerated.
factory Callable[[], Awaitable[_T]] A zero-arg async callable that performs the actual regeneration (out of scope here — T10/T11 supply it).

Returns: _T — factory()'s result — shared across all callers that joined the same in-flight call.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally