Skip to content
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

Add hypertrace #158

Closed
wants to merge 1 commit into from
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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { hash, createKeyPair } = require('./lib/crypto')
const RawStreamSet = require('./lib/raw-stream-set')
const ConnectionPool = require('./lib/connection-pool')
const { STREAM_NOT_CONNECTED } = require('./lib/errors')
const { createTracer } = require('hypertrace')

class HyperDHT extends DHT {
constructor (opts = {}) {
Expand All @@ -24,6 +25,7 @@ class HyperDHT extends DHT {

const { router, persistent } = defaultCacheOpts(opts)

this.tracer = createTracer(this)
this.defaultKeyPair = opts.keyPair || createKeyPair(opts.seed)
this.listening = new Set()

Expand Down Expand Up @@ -51,10 +53,12 @@ class HyperDHT extends DHT {
}

connect (remotePublicKey, opts) {
this.tracer.trace('connect')
return connect(this, remotePublicKey, opts)
}

createServer (opts, onconnection) {
this.tracer.trace('createServer')
if (typeof opts === 'function') return this.createServer({}, opts)
if (opts && opts.onconnection) onconnection = opts.onconnection
const s = new Server(this, opts)
Expand All @@ -67,13 +71,15 @@ class HyperDHT extends DHT {
}

async resume () {
this.tracer.trace('resume')
await super.resume()
const resuming = []
for (const server of this.listening) resuming.push(server.resume())
await Promise.allSettled(resuming)
}

async suspend () {
this.tracer.trace('suspend')
const suspending = []
for (const server of this.listening) suspending.push(server.suspend())
await Promise.allSettled(suspending)
Expand Down Expand Up @@ -146,6 +152,7 @@ class HyperDHT extends DHT {
}

findPeer (publicKey, opts = {}) {
this.tracer.trace('findPeer')
const target = opts.hash === false ? publicKey : hash(publicKey)
opts = { ...opts, map: mapFindPeer }
return this.query({ target, command: COMMANDS.FIND_PEER, value: null }, opts)
Expand Down
4 changes: 4 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const b4a = require('b4a')
const { handshake, holepunch } = require('./messages')
const { COMMANDS } = require('./constants')
const { BAD_HANDSHAKE_REPLY, BAD_HOLEPUNCH_REPLY } = require('./errors')
const { createTracer } = require('hypertrace')

const FROM_CLIENT = 0
const FROM_SERVER = 1
Expand All @@ -19,6 +20,7 @@ const REPLY = 4

module.exports = class Router {
constructor (dht, opts) {
this.tracer = createTracer(this, { parent: dht.tracer })
this.dht = dht
this.forwards = new Cache(opts.forwards)
}
Expand All @@ -44,6 +46,7 @@ module.exports = class Router {
}

async peerHandshake (target, { noise, peerAddress, relayAddress, socket, session }, to) {
this.tracer.trace('peerHandshake', { peerAddress, relayAddress, socket, session, to })
const dht = this.dht

const requestValue = c.encode(handshake, {
Expand Down Expand Up @@ -131,6 +134,7 @@ module.exports = class Router {
}

async peerHolepunch (target, { id, payload, peerAddress, socket, session }, to) {
this.tracer.trace('peerHolepunch', { id, peerAddress, socket, session, to })
const dht = this.dht
const requestValue = c.encode(holepunch, {
mode: FROM_CLIENT,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dht-rpc": "^6.9.0",
"events": "^3.3.0",
"hypercore-crypto": "^3.3.0",
"hypertrace": "^1.2.1",
"noise-curve-ed": "^2.0.0",
"noise-handshake": "^3.0.0",
"record-cache": "^1.1.1",
Expand Down