mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2026-02-04 07:44:32 +00:00
automatic prune broken relay objects and keep track of relay idleness so they can be pruned.
This commit is contained in:
@@ -88,10 +88,8 @@ export class AbstractSimplePool {
|
|||||||
enableReconnect: this.enableReconnect,
|
enableReconnect: this.enableReconnect,
|
||||||
})
|
})
|
||||||
relay.onclose = () => {
|
relay.onclose = () => {
|
||||||
if (relay && !relay.enableReconnect) {
|
|
||||||
this.relays.delete(url)
|
this.relays.delete(url)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
this.relays.set(url, relay)
|
this.relays.set(url, relay)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,10 +100,15 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
await relay.connect({
|
await relay.connect({
|
||||||
timeout: params?.connectionTimeout,
|
timeout: params?.connectionTimeout,
|
||||||
abort: params?.abort,
|
abort: params?.abort,
|
||||||
})
|
})
|
||||||
|
} catch (err) {
|
||||||
|
this.relays.delete(url)
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
return relay
|
return relay
|
||||||
}
|
}
|
||||||
@@ -380,4 +383,19 @@ export class AbstractSimplePool {
|
|||||||
this.relays.forEach(conn => conn.close())
|
this.relays.forEach(conn => conn.close())
|
||||||
this.relays = new Map()
|
this.relays = new Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pruneIdleRelays(idleThresholdMs: number = 10000): string[] {
|
||||||
|
const prunedUrls: string[] = []
|
||||||
|
|
||||||
|
// check each relay's idle status and prune if over threshold
|
||||||
|
for (const [url, relay] of this.relays) {
|
||||||
|
if (relay.idleSince && Date.now() - relay.idleSince >= idleThresholdMs) {
|
||||||
|
this.relays.delete(url)
|
||||||
|
prunedUrls.push(url)
|
||||||
|
relay.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return prunedUrls
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ export class AbstractRelay {
|
|||||||
public openSubs: Map<string, Subscription> = new Map()
|
public openSubs: Map<string, Subscription> = new Map()
|
||||||
public enablePing: boolean | undefined
|
public enablePing: boolean | undefined
|
||||||
public enableReconnect: boolean
|
public enableReconnect: boolean
|
||||||
|
public idleSince: number | undefined = Date.now() // when undefined that means it isn't idle
|
||||||
|
public ongoingOperations: number = 0 // used to compute idleness
|
||||||
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
||||||
private reconnectAttempts: number = 0
|
private reconnectAttempts: number = 0
|
||||||
@@ -116,12 +118,12 @@ export class AbstractRelay {
|
|||||||
|
|
||||||
this._connected = false
|
this._connected = false
|
||||||
this.connectionPromise = undefined
|
this.connectionPromise = undefined
|
||||||
|
this.idleSince = undefined
|
||||||
this.onclose?.()
|
|
||||||
|
|
||||||
if (this.enableReconnect && !this.skipReconnection) {
|
if (this.enableReconnect && !this.skipReconnection) {
|
||||||
this.reconnect()
|
this.reconnect()
|
||||||
} else {
|
} else {
|
||||||
|
this.onclose?.()
|
||||||
this.closeAllSubscriptions(reason)
|
this.closeAllSubscriptions(reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,7 +229,7 @@ export class AbstractRelay {
|
|||||||
const sub = this.subscribe(
|
const sub = this.subscribe(
|
||||||
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
||||||
{
|
{
|
||||||
label: 'forced-ping',
|
label: '<forced-ping>',
|
||||||
oneose: () => {
|
oneose: () => {
|
||||||
resolve(true)
|
resolve(true)
|
||||||
sub.close()
|
sub.close()
|
||||||
@@ -299,6 +301,9 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async publish(event: Event): Promise<string> {
|
public async publish(event: Event): Promise<string> {
|
||||||
|
this.idleSince = undefined
|
||||||
|
this.ongoingOperations++
|
||||||
|
|
||||||
const ret = new Promise<string>((resolve, reject) => {
|
const ret = new Promise<string>((resolve, reject) => {
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
const ep = this.openEventPublishes.get(event.id) as EventPublishResolver
|
const ep = this.openEventPublishes.get(event.id) as EventPublishResolver
|
||||||
@@ -310,6 +315,11 @@ export class AbstractRelay {
|
|||||||
this.openEventPublishes.set(event.id, { resolve, reject, timeout })
|
this.openEventPublishes.set(event.id, { resolve, reject, timeout })
|
||||||
})
|
})
|
||||||
this.send('["EVENT",' + JSON.stringify(event) + ']')
|
this.send('["EVENT",' + JSON.stringify(event) + ']')
|
||||||
|
|
||||||
|
// compute idleness state
|
||||||
|
this.ongoingOperations--
|
||||||
|
if (this.ongoingOperations === 0) this.idleSince = Date.now()
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,7 +337,15 @@ export class AbstractRelay {
|
|||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
||||||
): Subscription {
|
): Subscription {
|
||||||
const sub = this.prepareSubscription(filters, params)
|
if (params.label !== '<forced-ping>') {
|
||||||
|
this.idleSince = undefined
|
||||||
|
this.ongoingOperations++
|
||||||
|
}
|
||||||
|
|
||||||
|
this.serial++
|
||||||
|
const id = params.id || (params.label ? params.label + ':' : 'sub:') + this.serial
|
||||||
|
const sub = new Subscription(this, id, filters, params)
|
||||||
|
this.openSubs.set(id, sub)
|
||||||
sub.fire()
|
sub.fire()
|
||||||
|
|
||||||
if (params.abort) {
|
if (params.abort) {
|
||||||
@@ -337,17 +355,6 @@ export class AbstractRelay {
|
|||||||
return sub
|
return sub
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepareSubscription(
|
|
||||||
filters: Filter[],
|
|
||||||
params: Partial<SubscriptionParams> & { label?: string; id?: string },
|
|
||||||
): Subscription {
|
|
||||||
this.serial++
|
|
||||||
const id = params.id || (params.label ? params.label + ':' : 'sub:') + this.serial
|
|
||||||
const subscription = new Subscription(this, id, filters, params)
|
|
||||||
this.openSubs.set(id, subscription)
|
|
||||||
return subscription
|
|
||||||
}
|
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
this.skipReconnection = true
|
this.skipReconnection = true
|
||||||
if (this.reconnectTimeoutHandle) {
|
if (this.reconnectTimeoutHandle) {
|
||||||
@@ -360,6 +367,7 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
this.closeAllSubscriptions('relay connection closed by us')
|
this.closeAllSubscriptions('relay connection closed by us')
|
||||||
this._connected = false
|
this._connected = false
|
||||||
|
this.idleSince = undefined
|
||||||
this.onclose?.()
|
this.onclose?.()
|
||||||
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
||||||
this.ws?.close()
|
this.ws?.close()
|
||||||
@@ -549,6 +557,11 @@ export class Subscription {
|
|||||||
this.closed = true
|
this.closed = true
|
||||||
}
|
}
|
||||||
this.relay.openSubs.delete(this.id)
|
this.relay.openSubs.delete(this.id)
|
||||||
|
|
||||||
|
// compute idleness state
|
||||||
|
this.relay.ongoingOperations--
|
||||||
|
if (this.relay.ongoingOperations === 0) this.relay.idleSince = Date.now()
|
||||||
|
|
||||||
this.onclose?.(reason)
|
this.onclose?.(reason)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.22.2",
|
"version": "2.23.0",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.22.2",
|
"version": "2.23.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user