Skip to content

Commit ccb9641

Browse files
committed
pool: maxWaitForConnection parameter.
this was so obvious.
1 parent b624ad4 commit ccb9641

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

abstract-pool.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
2525
// allowConnectingToRelay takes a relay URL and the operation being performed
2626
// return false to skip connecting to that relay
2727
allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
28+
// maxWaitForConnection takes a number in milliseconds that will be given to ensureRelay such that we
29+
// don't get stuck forever when attempting to connect to a relay, it is 3000 (3 seconds) by default
30+
maxWaitForConnection: number
2831
}
2932

3033
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
@@ -48,6 +51,7 @@ export class AbstractSimplePool {
4851
public trustedRelayURLs: Set<string> = new Set()
4952
public onRelayConnectionFailure?: (url: string) => void
5053
public allowConnectingToRelay?: (url: string, operation: ['read', Filter[]] | ['write', Event]) => boolean
54+
public maxWaitForConnection: number
5155

5256
private _WebSocket?: typeof WebSocket
5357

@@ -59,6 +63,7 @@ export class AbstractSimplePool {
5963
this.automaticallyAuth = opts.automaticallyAuth
6064
this.onRelayConnectionFailure = opts.onRelayConnectionFailure
6165
this.allowConnectingToRelay = opts.allowConnectingToRelay
66+
this.maxWaitForConnection = opts.maxWaitForConnection || 3000
6267
}
6368

6469
async ensureRelay(
@@ -199,7 +204,10 @@ export class AbstractSimplePool {
199204
let relay: AbstractRelay
200205
try {
201206
relay = await this.ensureRelay(url, {
202-
connectionTimeout: params.maxWait ? Math.max(params.maxWait * 0.8, params.maxWait - 1000) : undefined,
207+
connectionTimeout:
208+
this.maxWaitForConnection < (params.maxWait || 0)
209+
? Math.max(params.maxWait! * 0.8, params.maxWait! - 1000)
210+
: this.maxWaitForConnection,
203211
abort: params.abort,
204212
})
205213
} catch (err) {
@@ -314,7 +322,11 @@ export class AbstractSimplePool {
314322
publish(
315323
relays: string[],
316324
event: Event,
317-
options?: { onauth?: (evt: EventTemplate) => Promise<VerifiedEvent> },
325+
params?: {
326+
onauth?: (evt: EventTemplate) => Promise<VerifiedEvent>
327+
maxWait?: number
328+
abort?: AbortSignal
329+
},
318330
): Promise<string>[] {
319331
return relays.map(normalizeURL).map(async (url, i, arr) => {
320332
if (arr.indexOf(url) !== i) {
@@ -328,7 +340,13 @@ export class AbstractSimplePool {
328340

329341
let r: Relay
330342
try {
331-
r = await this.ensureRelay(url)
343+
r = await this.ensureRelay(url, {
344+
connectionTimeout:
345+
this.maxWaitForConnection < (params?.maxWait || 0)
346+
? Math.max(params!.maxWait! * 0.8, params!.maxWait! - 1000)
347+
: this.maxWaitForConnection,
348+
abort: params?.abort,
349+
})
332350
} catch (err) {
333351
this.onRelayConnectionFailure?.(url)
334352
return String('connection failure: ' + String(err))
@@ -337,8 +355,8 @@ export class AbstractSimplePool {
337355
return r
338356
.publish(event)
339357
.catch(async err => {
340-
if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) {
341-
await r.auth(options.onauth)
358+
if (err instanceof Error && err.message.startsWith('auth-required: ') && params?.onauth) {
359+
await r.auth(params.onauth)
342360
return r.publish(event) // retry
343361
}
344362
throw err

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nostr/tools",
3-
"version": "2.22.1",
3+
"version": "2.22.2",
44
"exports": {
55
".": "./index.ts",
66
"./core": "./core.ts",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "module",
33
"name": "nostr-tools",
4-
"version": "2.22.1",
4+
"version": "2.22.2",
55
"description": "Tools for making a Nostr client.",
66
"repository": {
77
"type": "git",

pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function useWebSocketImplementation(websocketImplementation: any) {
1515

1616
export class SimplePool extends AbstractSimplePool {
1717
constructor(options?: Pick<AbstractPoolConstructorOptions, 'enablePing' | 'enableReconnect'>) {
18-
super({ verifyEvent, websocketImplementation: _WebSocket, ...options })
18+
super({ verifyEvent, websocketImplementation: _WebSocket, maxWaitForConnection: 3000, ...options })
1919
}
2020
}
2121

0 commit comments

Comments
 (0)