Skip to content

Commit

Permalink
connectionKeepAlive: 5000 default (#186)
Browse files Browse the repository at this point in the history
* connectionKeepAlive: 5000 default

* Set false to turn off + update readme

* rm now irrelevant keep-alive integration test
  • Loading branch information
HDegroote authored Jul 26, 2024
1 parent bc4f6d5 commit 8d27b7e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Options include:
// Defaults to ['node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737']
bootstrap: ['host:port'],
keyPair, // set the default key pair to use for server.listen and connect
connectionKeepAlive // set a default keep-alive (in ms) on all opened sockets. Defaults to 0 (no keep-alive).
connectionKeepAlive // set a default keep-alive (in ms) on all opened sockets. Defaults to 5000. Set false to turn off (advanced usage).
}
```

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class HyperDHT extends DHT {
this.defaultKeyPair = opts.keyPair || createKeyPair(opts.seed)
this.listening = new Set()
this.tracer = createTracer(this)
this.connectionKeepAlive = opts.connectionKeepAlive || 0
this.connectionKeepAlive = opts.connectionKeepAlive === false
? 0
: opts.connectionKeepAlive || 5000

// stats is inherited from dht-rpc so fwd the ones from there
this.stats = { punches: { consistent: 0, random: 0, open: 0 }, ...this.stats }
Expand Down
49 changes: 46 additions & 3 deletions test/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,52 @@ test('exception if null id is used', async function (t) {
}
})

test('connectionKeepAlive defaults to 0', async function (t) {
const [a] = await swarm(t)
t.is(a.connectionKeepAlive, 0)
test('connectionKeepAlive defaults to 5000', async function (t) {
t.plan(4)

const [a, b] = await swarm(t)
t.is(a.connectionKeepAlive, 5000, 'sanity check: connectionKeepAlive set to 5000')
t.is(b.connectionKeepAlive, 5000, 'sanity check: connectionKeepAlive set to 5000')

const server = a.createServer((socket) => {
socket.on('error', () => {})
t.is(socket.keepAlive, 5000, 'keepAlive set for server socket')
})

await server.listen()

const socket = b.connect(server.publicKey)
socket.on('error', () => {})

t.is(socket.keepAlive, 5000, 'keepAlive set for client socket')
})

test('connectionKeepAlive can be turned off', async function (t) {
t.plan(4)

const { bootstrap } = await swarm(t)

const a = new DHT({ bootstrap, connectionKeepAlive: false })
const b = new DHT({ bootstrap, connectionKeepAlive: false })
t.teardown(async () => {
await a.destroy()
await b.destroy()
})

t.is(a.connectionKeepAlive, 0, 'sanity check')
t.is(b.connectionKeepAlive, 0, 'sanity check')

const server = a.createServer((socket) => {
socket.on('error', () => {})
t.is(socket.keepAlive, 0, 'keepAlive not set for server socket')
})

await server.listen()

const socket = b.connect(server.publicKey)
socket.on('error', () => {})

t.is(socket.keepAlive, 0, 'keepAlive not set for client socket')
})

test('connectionKeepAlive passed to server and connection', async function (t) {
Expand Down
53 changes: 0 additions & 53 deletions test/integration/keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,56 +55,3 @@ test('Client use keepalive to detect disconnect - separated by processes', async
clientSocket.on('close', () => clientTest.pass('Discovered that the connection has been lost'))
}
})

test('Client not using keepalive does not detect disconnect - separated by processes', async t => {
t.plan(2)
t.teardown(() => node.destroy())

const clientTest = t.test('client')
const { bootstrap } = await swarm(t)
const node = new DHT({ bootstrap })
const keyPair = DHT.keyPair()
const publicKey = b4a.toString(keyPair.publicKey, 'hex')
const secretKey = b4a.toString(keyPair.secretKey, 'hex')
let timedout = false

clientTest.plan(2)

t.test('server', async serverTest => {
serverTest.plan(3)

const args = [
path.join(__dirname, 'fixtures/server.js'),
publicKey,
secretKey,
JSON.stringify(bootstrap)
]

for await (const [kill, data] of spawnFixture(serverTest, args)) {
if (data === 'started') {
serverTest.pass('Started. Now starting client')
startClient()
}

if (data === 'socket_connected') {
serverTest.pass('Client connected. Killing server process in 1 second')
kill()
}
}

serverTest.pass('Process died')

setTimeout(() => {
timedout = true
clientTest.pass('After 20 seconds the connection is still open')
}, 20000)
})

function startClient () {
const clientSocket = node.connect(publicKey)
// No .setKeepAlive() here
clientSocket.on('open', () => clientTest.pass('Connected'))
clientSocket.on('error', () => !timedout && clientTest.fail())
clientSocket.on('close', () => !timedout && clientTest.fail())
}
})

0 comments on commit 8d27b7e

Please sign in to comment.