streaming
streaming
¶
Data-driven event streaming algorithm with binary-split fallback.
Provides stream_events — the core windowing algorithm that streams
Nostr events in ascending (created_at, id) order, ensuring
completeness even when a relay truncates responses.
_fetch_validated is the single source of truth for event validation.
stream_events orchestrates windowing, sorting, and domain conversion.
This module lives in bigbrotr.utils (not services) because it has
no service-layer dependencies — only nostr_sdk and bigbrotr.models.
Classes¶
Functions¶
stream_events
async
¶
stream_events(
client: Client,
filters: list[Filter],
start_time: int,
end_time: int,
limit: int,
request_timeout: float,
idle_timeout: float,
max_event_size: int | None = None,
) -> AsyncIterator[Event]
Stream all events matching filters in [start_time, end_time],
yielded as domain Event objects in ascending (created_at, id) order.
Uses a data-driven windowing algorithm for completeness: when a fetch
returns limit events (possible truncation), a verification re-fetch
at min_created_at determines whether all events have been captured.
Falls back to binary-split windowing on inconsistent relay responses.
Validation (filter matching, signature verification, deduplication) is
handled by _fetch_validated. This function orchestrates windowing, then
sorts and converts raw events to domain Event models at each yield
boundary via _to_domain_events.
The idle_timeout is progress-based: the timer resets every time an
event is yielded. A relay that produces events slowly but steadily will
never be killed by it. A relay that connects but stops producing events
is abandoned after idle_timeout seconds.
Parameters:
-
client(Client) –Connected nostr-sdk
Clientwith the target relay added. -
filters(list[Filter]) –Pre-validated base
Filterobjects withoutsince/until/limit(useSynchronizerConfig.filters). -
start_time(int) –Inclusive lower timestamp bound (since).
-
end_time(int) –Inclusive upper timestamp bound (until).
-
limit(int) –Max events per relay request (REQ limit).
-
request_timeout(float) –Seconds to wait for each
stream_eventscall. -
idle_timeout(float) –Seconds without yielding an event before the stream is abandoned. The timer starts when the function enters its main loop and resets on every yield.
Yields:
-
AsyncIterator[Event]–Domain
Eventobjects in ascending(created_at, id)order.
Source code in src/bigbrotr/utils/streaming.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |