mixins
mixins
¶
Reusable service mixins for BigBrotr.
All service extensions live here as mixin classes. Each mixin uses
cooperative multiple inheritance (super().__init__(**kwargs)) so
that initialization is handled automatically via the MRO — no
explicit _init_*() calls are needed in service constructors.
See Also
BaseService: The base class
that mixin classes are composed with via multiple inheritance.
NetworksConfig:
Provides max_tasks values consumed by
NetworkSemaphoresMixin.
Classes¶
NetworkSemaphores
¶
NetworkSemaphores(networks: NetworksConfig)
Per-network concurrency semaphores.
Creates an asyncio.Semaphore for each operational
NetworkType (clearnet, Tor,
I2P, Lokinet) to cap the number of simultaneous connections.
See Also
NetworksConfig:
Provides max_tasks per network type.
Source code in src/bigbrotr/services/common/mixins.py
Functions¶
get
¶
get(network: NetworkType) -> Semaphore | None
Look up the concurrency semaphore for a network type.
Returns:
-
Semaphore | None–The semaphore, or
Nonefor non-operational networks -
Semaphore | None–(LOCAL, UNKNOWN).
Source code in src/bigbrotr/services/common/mixins.py
NetworkSemaphoresMixin
¶
Mixin providing per-network concurrency semaphores.
Exposes a network_semaphores attribute of type
NetworkSemaphores,
initialized from the networks keyword argument.
Services must pass networks=config.networks in their
super().__init__() call.
See Also
Validator, Monitor, Synchronizer: Services that compose this mixin for bounded concurrency.
Source code in src/bigbrotr/services/common/mixins.py
ConcurrentStreamMixin
¶
Mixin providing concurrent item processing with streaming results.
Adds _iter_concurrent() which launches asyncio.TaskGroup tasks
and streams results through an asyncio.Queue bridge as each worker
completes — enabling progressive metric updates instead of waiting for
all items to finish.
See Also
Finder, Synchronizer, Monitor, Validator: Services that compose this mixin.
GeoReaders
¶
GeoIP database reader container for city and ASN lookups.
Manages the lifecycle of geoip2.database.Reader instances.
Reader initialization is offloaded to a thread via open() to
avoid blocking the event loop.
Attributes:
-
city(Reader | None) –GeoLite2-City reader for geolocation lookups, or
None. -
asn(Reader | None) –GeoLite2-ASN reader for network info lookups, or
None.
See Also
GeoReaderMixin:
Mixin that exposes a geo_readers attribute of this type.
Source code in src/bigbrotr/services/common/mixins.py
Functions¶
open
async
¶
Open GeoIP readers from file paths via asyncio.to_thread.
Parameters:
-
city_path(str | None, default:None) –Path to GeoLite2-City database.
Noneto skip. -
asn_path(str | None, default:None) –Path to GeoLite2-ASN database.
Noneto skip.
Source code in src/bigbrotr/services/common/mixins.py
close
¶
Close readers and set to None. Idempotent.
GeoReaderMixin
¶
Mixin providing GeoIP database reader lifecycle management.
Exposes a geo_readers attribute of type
GeoReaders.
Note
Call geo_readers.close() in a finally block or __aexit__.
See Also
Monitor: The service that composes this mixin for NIP-66 geo/net checks.
Source code in src/bigbrotr/services/common/mixins.py
Clients
¶
Clients(
keys: Keys,
networks: NetworksConfig,
*,
allow_insecure: bool = False,
)
Lazy pool of Nostr clients for event broadcasting.
Each relay is connected on first access via get() and cached
for subsequent calls. Failed connections are remembered so that
the same relay is never retried within a cycle.
Parameters:
-
keys(Keys) –Signing keys for event publishing.
-
networks(NetworksConfig) –Network configuration for proxy URL resolution and per-network connection timeouts.
-
allow_insecure(bool, default:False) –If
True, fall back to insecure transport on SSL failure.
See Also
ClientsMixin:
Mixin that exposes a clients attribute of this type.
connect_relay: Used
internally to establish each connection.
Source code in src/bigbrotr/services/common/mixins.py
Functions¶
get
async
¶
get(relay: Relay) -> Client | None
Return a connected client for a relay, connecting lazily.
Three states per relay URL:
- Unknown — connect now, cache on success, mark failed otherwise.
- Connected — return the cached client immediately.
- Failed — return
Nonewithout retrying.
Parameters:
-
relay(Relay) –Relay to get a client for.
Returns:
-
Client | None–Connected client, or
Noneif the connection failed.
Source code in src/bigbrotr/services/common/mixins.py
get_many
async
¶
get_many(relays: list[Relay]) -> list[Client]
Return connected clients for multiple relays.
Calls get() for each relay and filters out failures.
Parameters:
-
relays(list[Relay]) –Relays to get clients for.
Returns:
-
list[Client]–Connected clients (order preserved, failed relays skipped).
Source code in src/bigbrotr/services/common/mixins.py
disconnect
async
¶
Disconnect all clients and reset state.
Source code in src/bigbrotr/services/common/mixins.py
ClientsMixin
¶
Mixin providing managed Nostr client pool for event broadcasting.
Exposes a clients attribute of type
Clients. Pops a
pre-constructed clients instance from kwargs.
See Also
Monitor: The service that composes this mixin for Kind 0/10166/30166 event publishing.
Source code in src/bigbrotr/services/common/mixins.py
CatalogAccessMixin
¶
Mixin providing schema catalog initialization and discovery.
Creates a Catalog instance
during __init__ and discovers the database schema during
__aenter__. Also provides a _is_table_enabled() helper that
checks table access policy from the service config.
Services must compose this mixin with
BaseService (which provides
_brotr, _logger, and _config).
See Also
Api, Dvm: Services that compose this mixin. Catalog: Schema introspection and query execution.