This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert ping API to async/await (#2671)
- Loading branch information
Alan Shaw
authored
Dec 13, 2019
1 parent
21e62cb
commit 0921a82
Showing
5 changed files
with
41 additions
and
120 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 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 |
---|---|---|
@@ -1,18 +1,44 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream/pull') | ||
|
||
module.exports = function ping (self) { | ||
return promisify((peerId, opts, callback) => { | ||
if (typeof opts === 'function') { | ||
callback = opts | ||
opts = {} | ||
const PeerId = require('peer-id') | ||
const basePacket = { success: true, time: 0, text: '' } | ||
|
||
module.exports = ({ libp2p }) => { | ||
return async function * (peerId, options) { | ||
options = options || {} | ||
options.count = options.count || 10 | ||
|
||
if (!PeerId.isPeerId(peerId)) { | ||
peerId = PeerId.createFromCID(peerId) | ||
} | ||
|
||
pull( | ||
self.pingPullStream(peerId, opts), | ||
pull.collect(callback) | ||
) | ||
}) | ||
let peerInfo | ||
if (libp2p.peerStore.has(peerId)) { | ||
peerInfo = libp2p.peerStore.get(peerId) | ||
} else { | ||
yield { ...basePacket, text: `Looking up peer ${peerId}` } | ||
peerInfo = await libp2p.peerRouting.findPeer(peerId) | ||
} | ||
|
||
yield { ...basePacket, text: `PING ${peerInfo.id.toB58String()}` } | ||
|
||
let packetCount = 0 | ||
let totalTime = 0 | ||
|
||
for (let i = 0; i < options.count; i++) { | ||
try { | ||
const time = libp2p.ping(peerInfo) | ||
totalTime += time | ||
packetCount++ | ||
yield { ...basePacket, time } | ||
} catch (err) { | ||
yield { ...basePacket, success: false, text: err.toString() } | ||
} | ||
} | ||
|
||
if (packetCount) { | ||
const average = totalTime / packetCount | ||
yield { ...basePacket, text: `Average latency: ${average}ms` } | ||
} | ||
} | ||
} |
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