Skip to content

Commit

Permalink
add connectionKeepAlive opt (#177)
Browse files Browse the repository at this point in the history
* Add connectionKeepAlive opt

* Simplify: make server use dht.connectionKeepAlive

* Clean up test (close socket + server, and set long keepALive to avoid it being sent during the test)

* Add error handlers to the sockets

* Use secret-stream 6.4.0

* Add connectionKeepAlive to readme
  • Loading branch information
HDegroote committed Apr 3, 2024
1 parent 79eea93 commit 880a935
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Options include:
// Optionally overwrite the default bootstrap servers, just need to be an array of any known dht node(s)
// 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
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).
}
```

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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._router = new Router(this, router)
this._socketPool = new SocketPool(this, opts.host || '0.0.0.0')
Expand Down
3 changes: 2 additions & 1 deletion lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ module.exports = function connect (dht, publicKey, opts = {}) {
const encryptedSocket = (opts.createSecretStream || defaultCreateSecretStream)(true, null, {
publicKey: keyPair.publicKey,
remotePublicKey: publicKey,
autoStart: false
autoStart: false,
keepAlive: dht.connectionKeepAlive
})

if (pool) pool._attachStream(encryptedSocket, false)
Expand Down
5 changes: 4 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ module.exports = class Server extends EventEmitter {
? new DebuggingStream(hs.rawStream, this.dht._debugStream)
: hs.rawStream

hs.encryptedSocket = this.createSecretStream(false, rawStream, { handshake: h })
hs.encryptedSocket = this.createSecretStream(false, rawStream, {
handshake: h,
keepAlive: this.dht.connectionKeepAlive
})

this.onconnection(hs.encryptedSocket)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
},
"dependencies": {
"@hyperswarm/secret-stream": "^6.0.0",
"@hyperswarm/secret-stream": "^6.4.0",
"b4a": "^1.3.1",
"bare-events": "^2.2.0",
"blind-relay": "^1.3.0",
Expand Down
34 changes: 34 additions & 0 deletions test/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,37 @@ test('exception if null id is used', async function (t) {
await a.destroy()
}
})

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

test('connectionKeepAlive passed to server and connection', async function (t) {
const allChecks = t.test('all') // hack so we can await plan
allChecks.plan(2)

const { bootstrap } = await swarm(t)

const a = new DHT({ bootstrap, connectionKeepAlive: 10000 })
const b = new DHT({ bootstrap, connectionKeepAlive: 20000 })

const server = a.createServer(async function (socket) {
socket.on('error', () => {})
allChecks.is(socket.keepAlive, 10000, 'keepAlive set for server')
})

await server.listen()

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

allChecks.is(socket.keepAlive, 20000, 'keepAlive set for connection')

await allChecks
await socket.end()
await server.close()

await a.destroy()
await b.destroy()
})

0 comments on commit 880a935

Please sign in to comment.