git ssb

4+

Dominic / scuttlebot



Commit 5d16858fd6638d5328345f8b8429f0d33c2a5192

replicate: better handling of various sync detection cases and timeout

Matt McKegg committed on 4/11/2017, 10:30:42 PM
Parent: d9bd37aa74a681314de63566c5576420e75b6278

Files changed

plugins/replicate.jschanged
plugins/replicate.jsView
@@ -272,9 +272,9 @@
272272 keys: false
273273 }),
274274
275275 // track sync completed progress
276- pull.through(detectSync(rpc.id, upto, peerHas, function () {
276 + pull.through(detectSync(rpc.id, upto, toSend, peerHas, function () {
277277 if (pendingFeedsForPeer[rpc.id]) {
278278 pendingFeedsForPeer[rpc.id].delete(upto.id)
279279 debounce.set()
280280 }
@@ -321,19 +321,26 @@
321321 }
322322 }
323323 }
324324
325-function detectSync (peerId, upto, peerHas, onSync) {
325 +function detectSync (peerId, upto, toSend, peerHas, onSync) {
326326 // HACK: createHistoryStream does not emit sync event, so we don't
327327 // know when it switches to live. Do it manually!
328328
329329 var sync = false
330- var timeout = null
331330 var last = (upto.sequence || upto.seq || 0)
332331
333- // check sync after 100ms, hopefully we have the info from the peer by then
334- // if not, falls back to the 3 second timeout
335- setTimeout(checkSync, 100)
332 + // check sync after 500ms, hopefully we have the info from the peer by then
333 + setTimeout(function () {
334 + if (peerHas[peerId] && peerHas[peerId][upto.id] != null) {
335 + checkSync()
336 + } else {
337 + // if we get here, the peer hasn't yet asked for this feed, or is not responding
338 + // we can assume it doesn't have the feed, so lets call sync
339 + console.log('timeout')
340 + broadcastSync()
341 + }
342 + }, 500)
336343
337344 return function (msg) {
338345 if (msg.sync) {
339346 // surprise! This peer actually has a sync event!
@@ -347,27 +354,20 @@
347354 }
348355
349356 function checkSync () {
350357 if (!sync) {
351- resetSyncTimeout()
352358 var availableSeq = peerHas[peerId] && peerHas[peerId][upto.id]
353- if (availableSeq && availableSeq === last) {
359 + if (availableSeq === last || availableSeq < toSend[upto.id]) {
354360 // we've reached the maximum sequence this server has told us it knows about
361 + // or we don't need anything from this server
355362 broadcastSync()
356363 }
357364 }
358365 }
359366
360- function resetSyncTimeout () {
361- // assume that if we haven't received a message for 3 seconds that we're sync
362- clearTimeout(timeout)
363- timeout = setTimeout(broadcastSync, 3000)
364- }
365-
366367 function broadcastSync () {
367368 if (!sync) {
368369 sync = true
369- clearTimeout(timeout)
370370 onSync && onSync()
371371 }
372372 }
373373 }

Built with git-ssb-web