-
Notifications
You must be signed in to change notification settings - Fork 236
feat(nip66): add RelayMonitorWorker cluster worker and probe scheduler #724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ferryx349
wants to merge
13
commits into
main
Choose a base branch
from
nip66_worker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bc34f89
feat(nip66): add shared relay probe engine (DNS, TLS, WS RTT, NIP-11)
Ferryx349 41d2a2c
fix(nip66): address relay probe review feedback
Ferryx349 a812bfc
Merge branch 'main' into NIP-66
Ferryx349 3183ef1
Merge branch 'main' into NIP-66
cameri 7fd2cc3
feat(nip66): add relay monitor settings schema and defaults
Ferryx349 d23f7fd
chore(nip66): merge upstream main and keep nip50 settings alongside n…
Ferryx349 4c4032d
docs(nip66): clarify settings are config-only until monitor worker lands
Ferryx349 5257486
Merge branch 'main' into NIP-66
Ferryx349 578dea0
Merge branch 'main' into nip-66-settings
Ferryx349 fe16a55
Merge branch 'nip-66-settings' into nip66_worker
Ferryx349 a26c06b
feat(nip66): add RelayMonitorWorker cluster worker and probe scheduler
Ferryx349 5474393
fix(nip66): block IPv6 NIP-11 targets and type stored probe snapshots
Ferryx349 e169ddf
Merge origin/main into nip66_worker
Ferryx349 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat(nip66): add RelayMonitorWorker cluster worker and probe scheduler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| Add NIP-66 relay monitor settings foundation with defaults for probe interval, timeouts, targets, monitor identity, and DNS cache TTL. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { | ||
| DnsRecord, | ||
| Nip11Result, | ||
| ProbeCheckResult, | ||
| ProbeResult, | ||
| ProbeTarget, | ||
| WsRttResult, | ||
| } from '../utils/relay-probe/types' | ||
|
|
||
| export type RelayProbeRunStatus = 'ok' | 'partial' | 'failed' | ||
|
|
||
| export interface StoredDnsResult { | ||
| hostname: string | ||
| records: DnsRecord[] | ||
| fromCache: boolean | ||
| cacheExpiresAt?: string | ||
| } | ||
|
|
||
| export interface StoredTlsResult { | ||
| valid: boolean | ||
| issuer?: string | ||
| subject?: string | ||
| expiresAt?: string | ||
| daysUntilExpiry?: number | ||
| } | ||
|
|
||
| export interface StoredProbeResult { | ||
| target: ProbeTarget | ||
| checkedAt: string | ||
| dns: ProbeCheckResult<StoredDnsResult> | ||
| tls: ProbeCheckResult<StoredTlsResult> | ||
| wsRtt: ProbeCheckResult<WsRttResult> | ||
| nip11: ProbeCheckResult<Nip11Result> | ||
| } | ||
|
|
||
| export interface RelayProbeRunSnapshot { | ||
| runAt: string | ||
| targets: string[] | ||
| results: StoredProbeResult[] | ||
| status: RelayProbeRunStatus | ||
| } | ||
|
|
||
| export interface IRelayProbeSnapshotStore { | ||
| saveLatest(snapshot: RelayProbeRunSnapshot, expirySeconds?: number): Promise<void> | ||
| getLatest(): Promise<RelayProbeRunSnapshot | null> | ||
| } | ||
|
|
||
| export type ProbeRunStatusInput = Pick<StoredProbeResult, 'wsRtt'> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { IRunnable } from '../@types/base' | ||
| import { IRelayProbeSnapshotStore, RelayProbeRunSnapshot } from '../@types/relay-probe-snapshot' | ||
| import { Settings } from '../@types/settings' | ||
| import { createLogger } from '../factories/logger-factory' | ||
| import { shutdownMetricsTelemetry } from '../telemetry/metrics' | ||
| import { filterValidProbeTargets, resolveProbeTargets } from '../utils/relay-probe-targets' | ||
| import { deriveRelayProbeRunStatus, serializeProbeResults } from '../utils/relay-probe-snapshot' | ||
| import { runProbe } from '../utils/relay-probe' | ||
| import { ProbeOptions, ProbeResult } from '../utils/relay-probe/types' | ||
|
|
||
| const logger = createLogger('relay-monitor-worker') | ||
|
|
||
| const DEFAULT_PROBE_INTERVAL_SECONDS = 3600 | ||
| const MIN_PROBE_INTERVAL_SECONDS = 60 | ||
|
|
||
| export type RunProbeFn = (relayUrl: string, options?: ProbeOptions) => Promise<ProbeResult> | ||
|
|
||
| export const buildProbeOptions = (settings: Settings): ProbeOptions => { | ||
| const nip66 = settings.nip66 | ||
|
|
||
| return { | ||
| timeouts: nip66?.timeouts, | ||
| dnsCacheTtlSeconds: nip66?.dnsCacheTtlSeconds, | ||
| } | ||
| } | ||
|
|
||
| export const getProbeIntervalMs = (settings: Settings): number => { | ||
| const configured = settings.nip66?.probeIntervalSeconds ?? DEFAULT_PROBE_INTERVAL_SECONDS | ||
| const intervalSeconds = Math.max(configured, MIN_PROBE_INTERVAL_SECONDS) | ||
|
|
||
| return intervalSeconds * 1000 | ||
| } | ||
|
|
||
| export class RelayMonitorWorker implements IRunnable { | ||
| private interval: NodeJS.Timeout | undefined | ||
| private isRunning = false | ||
|
|
||
| public constructor( | ||
| private readonly process: NodeJS.Process, | ||
| private readonly settings: () => Settings, | ||
| private readonly snapshotStore: IRelayProbeSnapshotStore, | ||
| private readonly probeRunner: RunProbeFn = runProbe, | ||
| ) { | ||
| this.process | ||
| .on('SIGINT', this.onExit.bind(this)) | ||
| .on('SIGHUP', this.onExit.bind(this)) | ||
| .on('SIGTERM', this.onExit.bind(this)) | ||
| .on('uncaughtException', this.onError.bind(this)) | ||
| .on('unhandledRejection', this.onError.bind(this)) | ||
| } | ||
|
|
||
| public run(): void { | ||
| const currentSettings = this.settings() | ||
|
|
||
| if (!currentSettings.nip66?.enabled) { | ||
| logger('NIP-66 relay monitoring is disabled; worker exiting') | ||
| return | ||
| } | ||
|
|
||
| const intervalMs = getProbeIntervalMs(currentSettings) | ||
| logger('starting probe scheduler with interval %d ms', intervalMs) | ||
|
|
||
| void this.runScheduledProbes() | ||
|
|
||
| this.interval = setInterval(() => { | ||
| void this.runScheduledProbes() | ||
| }, intervalMs) | ||
| } | ||
|
|
||
| private async runScheduledProbes(): Promise<void> { | ||
| if (this.isRunning) { | ||
| logger('skipping scheduled probe run because previous run is still in progress') | ||
| return | ||
| } | ||
|
|
||
| this.isRunning = true | ||
|
|
||
| try { | ||
| await this.onSchedule() | ||
| } catch (error) { | ||
| this.onError(error as Error) | ||
| } finally { | ||
| this.isRunning = false | ||
| } | ||
| } | ||
|
|
||
| private async onSchedule(): Promise<void> { | ||
| const currentSettings = this.settings() | ||
|
|
||
| if (!currentSettings.nip66?.enabled) { | ||
| logger('NIP-66 relay monitoring disabled during scheduled run; stopping scheduler') | ||
| this.close() | ||
| return | ||
| } | ||
|
|
||
| const configuredTargets = resolveProbeTargets(currentSettings) | ||
| const { valid, invalid } = filterValidProbeTargets(configuredTargets) | ||
|
|
||
| for (const target of invalid) { | ||
| logger.warn('skipping invalid probe target: %s', target) | ||
| } | ||
|
|
||
| if (valid.length === 0) { | ||
| logger.warn('no valid probe targets configured; skipping probe run') | ||
| return | ||
| } | ||
|
|
||
| const probeOptions = buildProbeOptions(currentSettings) | ||
| const results: ProbeResult[] = [] | ||
|
|
||
| for (const target of valid) { | ||
| try { | ||
| results.push(await this.probeRunner(target, probeOptions)) | ||
| } catch (error) { | ||
| logger.error('probe run failed for %s: %o', target, error) | ||
| } | ||
| } | ||
|
|
||
| if (results.length === 0) { | ||
| logger.warn('probe run produced no results') | ||
| return | ||
| } | ||
|
|
||
| const snapshot: RelayProbeRunSnapshot = { | ||
| runAt: new Date().toISOString(), | ||
| targets: valid, | ||
| results: serializeProbeResults(results), | ||
| status: deriveRelayProbeRunStatus(results), | ||
| } | ||
|
|
||
| const expirySeconds = Math.max( | ||
| (currentSettings.nip66?.probeIntervalSeconds ?? DEFAULT_PROBE_INTERVAL_SECONDS) * 2, | ||
| MIN_PROBE_INTERVAL_SECONDS * 2, | ||
| ) | ||
|
|
||
| await this.snapshotStore.saveLatest(snapshot, expirySeconds) | ||
| logger('saved probe snapshot for %d target(s) with status %s', valid.length, snapshot.status) | ||
| } | ||
|
|
||
| private onError(error: Error) { | ||
| logger('error: %o', error) | ||
| throw error | ||
| } | ||
|
|
||
| private onExit() { | ||
| logger('exiting') | ||
| void shutdownMetricsTelemetry().finally(() => { | ||
| this.close(() => { | ||
| this.process.exit(0) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| public close(callback?: () => void) { | ||
| logger('closing') | ||
| clearInterval(this.interval) | ||
| if (typeof callback === 'function') { | ||
| callback() | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { RedisAdapter } from '../adapters/redis-adapter' | ||
| import { RelayMonitorWorker } from '../app/relay-monitor-worker' | ||
| import { getCacheClient } from '../cache/client' | ||
| import { createSettings } from './settings-factory' | ||
| import { RelayProbeSnapshotStore } from '../utils/relay-probe-snapshot' | ||
|
|
||
| export const relayMonitorWorkerFactory = () => { | ||
| const snapshotStore = new RelayProbeSnapshotStore(new RedisAdapter(getCacheClient())) | ||
|
|
||
| return new RelayMonitorWorker(process, createSettings, snapshotStore) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.