Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix: expose extra options from net.connect for dial and listen (#211)
Browse files Browse the repository at this point in the history
To allow more flexibility in use of sockets, expose options from node's net.connect method.
  • Loading branch information
achingbrain authored Sep 24, 2022
1 parent 637bd31 commit 6401a87
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { multiaddrToNetConfig } from './utils.js'
import { AbortError } from '@libp2p/interfaces/errors'
import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'
import { CreateListenerOptions, DialOptions, symbol, Transport } from '@libp2p/interface-transport'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
import type { Socket, IpcSocketConnectOpts, TcpSocketConnectOpts } from 'net'
import type { AbortOptions } from '@libp2p/interfaces'
import type { Connection } from '@libp2p/interface-connection'

const log = logger('libp2p:tcp')
Expand All @@ -32,6 +31,24 @@ export interface TCPOptions {
socketCloseTimeout?: number
}

/**
* Expose a subset of net.connect options
*/
export interface TCPSocketOptions extends AbortOptions {
noDelay?: boolean
keepAlive?: boolean
keepAliveInitialDelay?: number
allowHalfOpen?: boolean
}

export interface TCPDialOptions extends DialOptions, TCPSocketOptions {

}

export interface TCPCreateListenerOptions extends CreateListenerOptions, TCPSocketOptions {

}

export class TCP implements Transport {
private readonly opts: TCPOptions

Expand All @@ -47,9 +64,10 @@ export class TCP implements Transport {
return '@libp2p/tcp'
}

async dial (ma: Multiaddr, options: DialOptions): Promise<Connection> {
async dial (ma: Multiaddr, options: TCPDialOptions): Promise<Connection> {
options.keepAlive = options.keepAlive ?? true

const socket = await this._connect(ma, options)
socket.setKeepAlive(true)

// Avoid uncaught errors caused by unstable connections
socket.on('error', err => {
Expand All @@ -68,7 +86,7 @@ export class TCP implements Transport {
return conn
}

async _connect (ma: Multiaddr, options: AbortOptions = {}) {
async _connect (ma: Multiaddr, options: TCPDialOptions) {
if (options.signal?.aborted === true) {
throw new AbortError()
}
Expand Down Expand Up @@ -137,7 +155,7 @@ export class TCP implements Transport {
* anytime a new incoming Connection has been successfully upgraded via
* `upgrader.upgradeInbound`.
*/
createListener (options: CreateListenerOptions) {
createListener (options: TCPCreateListenerOptions) {
return createListener({
...options,
socketInactivityTimeout: this.opts.inboundSocketInactivityTimeout,
Expand Down
9 changes: 5 additions & 4 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { MultiaddrConnection, Connection } from '@libp2p/interface-connecti
import type { Upgrader, Listener } from '@libp2p/interface-transport'
import type { Server } from 'net'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { TCPCreateListenerOptions } from './index.js'

const log = logger('libp2p:tcp:listener')

Expand All @@ -29,7 +30,7 @@ async function attemptClose (maConn: MultiaddrConnection) {
}
}

interface Context {
interface Context extends TCPCreateListenerOptions {
handler?: (conn: Connection) => void
upgrader: Upgrader
socketInactivityTimeout?: number
Expand All @@ -44,12 +45,12 @@ export function createListener (context: Context) {
handler, upgrader, socketInactivityTimeout, socketCloseTimeout
} = context

context.keepAlive = context.keepAlive ?? true

let peerId: string | null
let listeningAddr: Multiaddr

const server: ServerWithMultiaddrConnections = Object.assign(net.createServer(socket => {
socket.setKeepAlive(true)

const server: ServerWithMultiaddrConnections = Object.assign(net.createServer(context, socket => {
// Avoid uncaught errors caused by unstable connections
socket.on('error', err => {
log('socket error', err)
Expand Down

0 comments on commit 6401a87

Please sign in to comment.