git ssb

4+

Dominic / scuttlebot



Commit c5a7c1d8b9fae990ed1c54fd2de267eccdbdc14a

Add pingPeers

cel committed on 5/9/2018, 3:58:03 AM
Parent: 0487bdcc9476f84c5edb8eff3d45ea0e479f7e0d

Files changed

plugins/gossip.mdchanged
plugins/gossip/index.jschanged
plugins/gossip.mdView
@@ -54,8 +54,14 @@
5454 ## ping: duplex
5555
5656 used internally by the gossip plugin to measure latency and clock skew
5757
58 +## pingPeers: source
59 +
60 +Ping all currently connected peers once.
61 +Returns an object `{"peer": <peerId>, "value": ping}` for each peer's response.
62 +Errors are ignored.
63 +
5864 ## connect: async
5965
6066 Add an address to the peer table, and connect immediately.
6167
plugins/gossip/index.jsView
@@ -254,8 +254,61 @@
254254 //between 10 seconds and 30 minutes, default 5 min
255255 timeout = Math.max(10e3, Math.min(timeout, 30*60e3))
256256 return ping({timeout: timeout})
257257 },
258 + pingPeers: function () {
259 + var drains = {}
260 + var queue = []
261 + var waitingCb
262 + for(var id in server.peers) {
263 + if(id !== server.id) server.peers[id].forEach(function (peer) {
264 + var pings
265 + try {
266 + pings = peer.gossip.ping()
267 + } catch(e) {
268 + return
269 + }
270 + pull(
271 + pull.once(true),
272 + pings,
273 + pull.take(1),
274 + drains[peer.id] = pull.collect(function (err, values) {
275 + delete drains[peer.id]
276 + var cb = waitingCb
277 + if (err) {
278 + if (cb && Object.keys(drains).length === 0) {
279 + waitingCb = null
280 + cb(true)
281 + }
282 + return
283 + }
284 + var val = {peer: peer.id, value: values[0]}
285 + if (cb) {
286 + waitingCb = null
287 + cb(null, val)
288 + } else {
289 + queue.push(val)
290 + }
291 + })
292 + )
293 + })
294 + }
295 + return function (abort, cb) {
296 + if(abort) {
297 + for(var id in drains) {
298 + drains[id].abort(abort)
299 + }
300 + return cb(abort)
301 + }
302 + if (queue.length > 0) {
303 + cb(null, queue.shift())
304 + } else if (Object.keys(drains).length === 0) {
305 + cb(true)
306 + } else {
307 + waitingCb = cb
308 + }
309 + }
310 + },
258311 reconnect: function () {
259312 for(var id in server.peers)
260313 if(id !== server.id) //don't disconnect local client
261314 server.peers[id].forEach(function (peer) {

Built with git-ssb-web