Skip to content

Commit

Permalink
add disconnectTimeout option to rx-nostr config
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Jan 13, 2025
1 parent f324ce9 commit 9a52a61
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions packages/core/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const makeRxNostrConfig = (config: RxNostrConfig) =>
maxCount: 5,
initialDelay: 1000,
},
disconnectTimeout: 10_000,
eoseTimeout: 30 * 1000,
okTimeout: 30 * 1000,
authTimeout: 30 * 1000,
Expand Down Expand Up @@ -48,6 +49,13 @@ export interface RxNostrConfig {
* Auto reconnection strategy.
*/
retry?: RetryConfig;

/**
* How long a relay connection should be held open when no longer used
* @default 5000
*/
disconnectTimeout?: number,

/**
* Specify how long rx-nostr waits for EOSE messages in `use()` following backward strategy (milliseconds).
*
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class NostrConnection {
private defaultSubscriptionIds: Set<string> = new Set();
private communicating = false;
private strategy: ConnectionStrategy = "lazy";
private disconnectTimeout: number
private disconnectTimer?: ReturnType<typeof setTimeout>
private isDefaultRelay = false;
private disposed = false;
private _url: string;
Expand All @@ -65,6 +67,8 @@ export class NostrConnection {
this.pubProxy = pubProxy;
this.subProxy = subProxy;

this.disconnectTimeout = config.disconnectTimeout

// Idling cold sockets
combineLatest([
this.pubProxy.getLogicalConnectionSizeObservable(),
Expand Down Expand Up @@ -94,9 +98,18 @@ export class NostrConnection {

switch (strategy) {
case "lazy": {
if (!this.communicating) {
this.relay.disconnect(WebSocketCloseCode.RX_NOSTR_IDLE);
// clear existing timer
if(this.disconnectTimeout) {
clearTimeout(this.disconnectTimer)
this.disconnectTimer=undefined
}

// create a new timer
this.disconnectTimer = setTimeout(() => {
if (!this.communicating) {
this.relay.disconnect(WebSocketCloseCode.RX_NOSTR_IDLE);
}
}, this.disconnectTimeout);
break;
}
case "lazy-keep": {
Expand Down Expand Up @@ -254,6 +267,9 @@ export class NostrConnection {

this.disposed = true;

if(this.disconnectTimer) clearTimeout(this.disconnectTimer)
this.disconnectTimer=undefined

this.relay.dispose();
this.pubProxy.dispose();
this.subProxy.dispose();
Expand Down

0 comments on commit 9a52a61

Please sign in to comment.