From 25592ec71fd51e80018302b67ae700572e323e54 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sat, 5 Jan 2019 16:42:24 +0100
Subject: [PATCH] [enh] #1325 Allow to have up to 10 chunks of advance compared
 to apply during sync

---
 app/modules/crawler/lib/constants.ts              |  1 +
 app/modules/crawler/lib/sync/v2/DownloadStream.ts | 11 ++++-------
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/app/modules/crawler/lib/constants.ts b/app/modules/crawler/lib/constants.ts
index f53e6f607..da99667c9 100644
--- a/app/modules/crawler/lib/constants.ts
+++ b/app/modules/crawler/lib/constants.ts
@@ -32,6 +32,7 @@ export const CrawlerConstants = {
   SANDBOX_CHECK_INTERVAL: 48, // Every 4 hours (288 blocks a day / 24 * 4)
   TEST_PEERS_INTERVAL: 10, // In seconds
   SYNC_PEERS_INTERVAL: 3, // Every 3 block average generation time
+  SYNC_CHUNKS_IN_ADVANCE: 10, // We want to have that much chunks in advance when syncing
 
   DURATIONS: {
     TEN_SECONDS: 10,
diff --git a/app/modules/crawler/lib/sync/v2/DownloadStream.ts b/app/modules/crawler/lib/sync/v2/DownloadStream.ts
index cae155f5a..7c392d2d6 100644
--- a/app/modules/crawler/lib/sync/v2/DownloadStream.ts
+++ b/app/modules/crawler/lib/sync/v2/DownloadStream.ts
@@ -132,13 +132,10 @@ export class DownloadStream extends Duplex {
         delete this.chunks[this.currentChunkNumber]
         // Let's start the download of next chunk
         this.currentChunkNumber++
-        this.downloadChunk(this.currentChunkNumber)
-          .then(() => this.downloadChunk(this.currentChunkNumber + 1))
-          .then(() => this.downloadChunk(this.currentChunkNumber + 2))
-          .then(() => this.downloadChunk(this.currentChunkNumber + 3))
-          .then(() => this.downloadChunk(this.currentChunkNumber + 4))
-          .then(() => this.downloadChunk(this.currentChunkNumber + 5))
-          .then(() => this.downloadChunk(this.currentChunkNumber + 6))
+        let p = this.downloadChunk(this.currentChunkNumber)
+        for (let i = 1; i <= CrawlerConstants.SYNC_CHUNKS_IN_ADVANCE; i++) {
+          p = p.then(() => this.downloadChunk(this.currentChunkNumber + i))
+        }
       }
       else {
         // We don't have it yet
-- 
GitLab