-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: ping * chore: ping is now a function * chore: address review
- Loading branch information
1 parent
1cf3198
commit 88e5698
Showing
10 changed files
with
142 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,62 @@ | ||
'use strict' | ||
|
||
const handler = require('./handler') | ||
const debug = require('debug') | ||
const log = debug('libp2p-ping') | ||
log.error = debug('libp2p-ping:error') | ||
const errCode = require('err-code') | ||
|
||
exports = module.exports = require('./ping') | ||
exports.mount = handler.mount | ||
exports.unmount = handler.unmount | ||
const crypto = require('libp2p-crypto') | ||
const pipe = require('it-pipe') | ||
const { toBuffer } = require('it-buffer') | ||
const { collect } = require('streaming-iterables') | ||
|
||
const { PROTOCOL, PING_LENGTH } = require('./constants') | ||
|
||
/** | ||
* Ping a given peer and wait for its response, getting the operation latency. | ||
* @param {Libp2p} node | ||
* @param {PeerInfo} peer | ||
* @returns {Promise<Number>} | ||
*/ | ||
async function ping (node, peer) { | ||
log('dialing %s to %s', PROTOCOL, peer.id.toB58String()) | ||
|
||
const { stream } = await node.dialProtocol(peer, PROTOCOL) | ||
|
||
const start = new Date() | ||
const data = crypto.randomBytes(PING_LENGTH) | ||
|
||
const [result] = await pipe( | ||
[data], | ||
stream, | ||
toBuffer, | ||
collect | ||
) | ||
const end = Date.now() | ||
|
||
if (!data.equals(result)) { | ||
throw errCode(new Error('Received wrong ping ack'), 'ERR_WRONG_PING_ACK') | ||
} | ||
|
||
return end - start | ||
} | ||
|
||
/** | ||
* Subscribe ping protocol handler. | ||
* @param {Libp2p} node | ||
*/ | ||
function mount (node) { | ||
node.handle(PROTOCOL, ({ stream }) => pipe(stream, stream)) | ||
} | ||
|
||
/** | ||
* Unsubscribe ping protocol handler. | ||
* @param {Libp2p} node | ||
*/ | ||
function unmount (node) { | ||
node.unhandle(PROTOCOL) | ||
} | ||
|
||
exports = module.exports = ping | ||
exports.mount = mount | ||
exports.unmount = unmount |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict' | ||
/* eslint-env mocha */ | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
const { expect } = chai | ||
|
||
const pTimes = require('p-times') | ||
|
||
const peerUtils = require('../utils/creators/peer') | ||
const baseOptions = require('../utils/base-options') | ||
|
||
describe('ping', () => { | ||
let nodes | ||
|
||
beforeEach(async () => { | ||
nodes = await peerUtils.createPeer({ | ||
number: 2, | ||
config: baseOptions | ||
}) | ||
}) | ||
|
||
it('ping once from peer0 to peer1', async () => { | ||
const latency = await nodes[0].ping(nodes[1].peerInfo) | ||
|
||
expect(latency).to.be.a('Number') | ||
}) | ||
|
||
it('ping several times for getting an average', async () => { | ||
const latencies = await pTimes(5, () => nodes[1].ping(nodes[0].peerInfo)) | ||
|
||
const averageLatency = latencies.reduce((p, c) => p + c, 0) / latencies.length | ||
expect(averageLatency).to.be.a('Number') | ||
}) | ||
}) |