Skip to content

Commit

Permalink
feat(rosenet-node): add signal to dial and newStream calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mkermani144 committed Jun 27, 2024
1 parent 2aa4234 commit 7eb1ddd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-hotels-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosenet-node': minor
---

Abort connection establishment or stream creation after a predefined duration
2 changes: 2 additions & 0 deletions packages/rosenet-node/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const RELAYS_COUNT_TO_CONNECT = 3;
export const ROSENET_DIRECT_PROTOCOL_V1 = '/rosenet/direct/1';
export const DEFAULT_NODE_PORT = 55123;
export const STREAM_CREATION_TIMEOUT = 500;
export const CONNECTION_CREATION_TIMEOUT = 1000;
13 changes: 11 additions & 2 deletions packages/rosenet-node/lib/stream/stream-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { Libp2p } from 'libp2p';
import RoseNetNodeContext from '../context/RoseNetNodeContext';
import createPushable, { Pushable } from './createPushable';

import { ROSENET_DIRECT_PROTOCOL_V1 } from '../constants';
import {
CONNECTION_CREATION_TIMEOUT,
ROSENET_DIRECT_PROTOCOL_V1,
STREAM_CREATION_TIMEOUT,
} from '../constants';

const cache = new Map<
string,
Expand Down Expand Up @@ -53,7 +57,11 @@ async function getStreamAndPushable(to: string, node: Libp2p) {
const possibleOpenConnectionToPeer = allConnectionsToPeer.find(
(connection) => connection.status === 'open',
);
const connection = possibleOpenConnectionToPeer ?? (await node.dial(peerId));
const connection =
possibleOpenConnectionToPeer ??
(await node.dial(peerId, {
signal: AbortSignal.timeout(CONNECTION_CREATION_TIMEOUT),
}));

RoseNetNodeContext.logger.debug(
possibleOpenConnectionToPeer
Expand All @@ -78,6 +86,7 @@ async function getStreamAndPushable(to: string, node: Libp2p) {
possibleWritableStream ??
(await connection.newStream(ROSENET_DIRECT_PROTOCOL_V1, {
runOnTransientConnection: true,
signal: AbortSignal.timeout(STREAM_CREATION_TIMEOUT),
}));

RoseNetNodeContext.logger.debug(
Expand Down

0 comments on commit 7eb1ddd

Please sign in to comment.