Skip to content

fix: optimize TCP connection establishment time #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/transport-tcp/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const CODE_CIRCUIT = 290
export const CODE_UNIX = 400

// Time to wait for a connection to close gracefully before destroying it manually
export const CLOSE_TIMEOUT = 500
export const CLOSE_TIMEOUT = 250
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?


// Close the socket if there is no activity after this long in ms
export const SOCKET_TIMEOUT = 2 * 60000 // 2 mins
2 changes: 1 addition & 1 deletion packages/transport-tcp/src/socket-to-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const toMultiaddrConnection = (socket: Socket, options: ToConnectionOptio
// by default there is no timeout
// https://nodejs.org/dist/latest-v16.x/docs/api/net.html#socketsettimeouttimeout-callback
socket.setTimeout(inactivityTimeout)

socket.once('timeout', () => {
timedOut = true
log('%s %s socket read timeout', direction, lOptsStr)
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-tcp/src/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class TCP implements Transport<TCPDialEvents> {
}

async _connect (ma: Multiaddr, options: TCPDialOptions): Promise<Socket> {
options.signal.throwIfAborted()
options.signal?.throwIfAborted()
options.onProgress?.(new CustomProgressEvent('tcp:open-connection'))

let rawSocket: Socket
Expand Down
43 changes: 43 additions & 0 deletions packages/transport-tcp/test/connection-time.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import net from 'node:net'
import { tcp } from '../src/index.js'

describe('TCP connection time', () => {
it('should establish a raw TCP connection quickly', async () => {
// Create a simple TCP server
const server = net.createServer()
const port = 8080 + Math.floor(Math.random() * 1000)

await new Promise<void>(resolve => {
server.listen(port, '127.0.0.1', () => { resolve() })
})

try {
// Measure connection time
const start = Date.now()

const socket = await new Promise<net.Socket>((resolve, reject) => {
const socket = net.connect(port, '127.0.0.1')
socket.on('connect', () => resolve(socket))
socket.on('error', reject)
})

const connectionTime = Date.now() - start
console.log(`Raw TCP connection established in ${connectionTime}ms`)

// Close the socket
socket.end()

// Note: This test only verifies local connection speed
// The actual issue (#3029) involves connections between data centers
// This test serves as a baseline for local development
} finally {
// Close the server
await new Promise<void>(resolve => {
server.close(() => { resolve() })
})
}
})
})