Commit 5d16858fd6638d5328345f8b8429f0d33c2a5192
replicate: better handling of various sync detection cases and timeout
Matt McKegg committed on 4/11/2017, 10:30:42 PMParent: d9bd37aa74a681314de63566c5576420e75b6278
Files changed
plugins/replicate.js | changed |
plugins/replicate.js | ||
---|---|---|
@@ -272,9 +272,9 @@ | ||
272 | 272 … | keys: false |
273 | 273 … | }), |
274 | 274 … | |
275 | 275 … | // track sync completed progress |
276 | - pull.through(detectSync(rpc.id, upto, peerHas, function () { | |
276 … | + pull.through(detectSync(rpc.id, upto, toSend, peerHas, function () { | |
277 | 277 … | if (pendingFeedsForPeer[rpc.id]) { |
278 | 278 … | pendingFeedsForPeer[rpc.id].delete(upto.id) |
279 | 279 … | debounce.set() |
280 | 280 … | } |
@@ -321,19 +321,26 @@ | ||
321 | 321 … | } |
322 | 322 … | } |
323 | 323 … | } |
324 | 324 … | |
325 | -function detectSync (peerId, upto, peerHas, onSync) { | |
325 … | +function detectSync (peerId, upto, toSend, peerHas, onSync) { | |
326 | 326 … | // HACK: createHistoryStream does not emit sync event, so we don't |
327 | 327 … | // know when it switches to live. Do it manually! |
328 | 328 … | |
329 | 329 … | var sync = false |
330 | - var timeout = null | |
331 | 330 … | var last = (upto.sequence || upto.seq || 0) |
332 | 331 … | |
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) | |
336 | 343 … | |
337 | 344 … | return function (msg) { |
338 | 345 … | if (msg.sync) { |
339 | 346 … | // surprise! This peer actually has a sync event! |
@@ -347,27 +354,20 @@ | ||
347 | 354 … | } |
348 | 355 … | |
349 | 356 … | function checkSync () { |
350 | 357 … | if (!sync) { |
351 | - resetSyncTimeout() | |
352 | 358 … | var availableSeq = peerHas[peerId] && peerHas[peerId][upto.id] |
353 | - if (availableSeq && availableSeq === last) { | |
359 … | + if (availableSeq === last || availableSeq < toSend[upto.id]) { | |
354 | 360 … | // we've reached the maximum sequence this server has told us it knows about |
361 … | + // or we don't need anything from this server | |
355 | 362 … | broadcastSync() |
356 | 363 … | } |
357 | 364 … | } |
358 | 365 … | } |
359 | 366 … | |
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 | - | |
366 | 367 … | function broadcastSync () { |
367 | 368 … | if (!sync) { |
368 | 369 … | sync = true |
369 | - clearTimeout(timeout) | |
370 | 370 … | onSync && onSync() |
371 | 371 … | } |
372 | 372 … | } |
373 | 373 … | } |
Built with git-ssb-web