utils
utils
¶
Monitor service utility functions.
Pure helpers for health check result inspection and retry logic.
Classes¶
CheckResult
¶
Bases: NamedTuple
Result of a single relay health check.
Each field contains the typed NIP metadata container if that check was run
and produced data, or None if the check was skipped (disabled in config)
or failed completely. Use has_data to test whether any check produced
results.
Attributes:
-
generated_at(int) –Unix timestamp when the health check was performed.
-
nip11_info(Nip11InfoMetadata | None) –NIP-11 relay information document (name, description, pubkey, etc.).
-
nip66_rtt(Nip66RttMetadata | None) –Round-trip times for open/read/write operations in milliseconds.
-
nip66_ssl(Nip66SslMetadata | None) –SSL certificate validation (valid, expiry timestamp, issuer).
-
nip66_geo(Nip66GeoMetadata | None) –Geolocation data (country, city, coordinates, timezone, geohash).
-
nip66_net(Nip66NetMetadata | None) –Network information (IP address, ASN, organization).
-
nip66_dns(Nip66DnsMetadata | None) –DNS resolution data (IPs, CNAME, nameservers, reverse DNS).
-
nip66_http(Nip66HttpMetadata | None) –HTTP metadata (server software and framework headers).
See Also
MetadataFlags: Boolean flags controlling which check types are computed and stored.
Functions¶
log_success
¶
Extract success status from a metadata result's logs object.
Source code in src/bigbrotr/services/monitor/utils.py
log_reason
¶
Extract failure reason from a metadata result's logs object.
Source code in src/bigbrotr/services/monitor/utils.py
extract_result
¶
Extract a successful result from asyncio.gather output.
Returns None if the key is absent or the result is an exception.
Source code in src/bigbrotr/services/monitor/utils.py
collect_metadata
¶
collect_metadata(
successful: list[tuple[Relay, CheckResult]],
store: MetadataFlags,
) -> list[RelayMetadata]
Build storable metadata records from successful health check results.
Iterates over successful relay/result pairs and collects metadata for
each check type enabled in store. Field names in CheckResult,
MetadataFlags, and MetadataType are aligned by convention
(e.g. nip11_info, nip66_rtt).
Parameters:
-
successful(list[tuple[Relay, CheckResult]]) –Relays with their health check results.
-
store(MetadataFlags) –Flags controlling which metadata types to include.
Returns:
-
list[RelayMetadata]–List of RelayMetadata
-
list[RelayMetadata]–ready for batch insertion.
Source code in src/bigbrotr/services/monitor/utils.py
retry_fetch
async
¶
retry_fetch(
relay: Relay,
coro_factory: Callable[[], Coroutine[Any, Any, _T]],
retry: RetryConfig,
operation: str,
wait: Callable[[float], Coroutine[Any, Any, bool]]
| None = None,
) -> _T | None
Execute a metadata fetch with exponential backoff retry.
Retries on network failures up to retry.max_attempts times.
Returns the result (possibly with success=False) or None on
exception.
Parameters:
-
relay(Relay) –Target relay (used for logging context).
-
coro_factory(Callable[[], Coroutine[Any, Any, _T]]) –Factory producing a fresh coroutine per attempt.
-
retry(RetryConfig) –Backoff configuration (max attempts, delays, jitter).
-
operation(str) –Check name for log messages (e.g.
"nip11_info"). -
wait(Callable[[float], Coroutine[Any, Any, bool]] | None, default:None) –Optional shutdown-aware sleep. Receives delay in seconds, returns
Trueif shutdown was requested. WhenNone, falls back toasyncio.sleep.
Note
The coro_factory pattern (a callable returning a coroutine)
is required because Python coroutines are single-use: once
awaited, they cannot be re-awaited. The factory creates a fresh
coroutine for each retry attempt.
Warning
Jitter is computed via random.uniform() (PRNG, # noqa: S311).
This is intentional -- jitter only needs to decorrelate
concurrent retries, not provide cryptographic randomness.
Source code in src/bigbrotr/services/monitor/utils.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 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 | |