Explorar el Código

storage-node: refactor if/else blocks for clarity

Mokhtar Naamani hace 4 años
padre
commit
0a6d83610d
Se han modificado 1 ficheros con 17 adiciones y 11 borrados
  1. 17 11
      storage-node/packages/colossus/lib/sync.js

+ 17 - 11
storage-node/packages/colossus/lib/sync.js

@@ -21,6 +21,8 @@
 const debug = require('debug')('joystream:sync')
 const _ = require('lodash')
 
+// TODO: refactor these two values into a new class
+// The number of concurrent sync sessions allowed. Must be greater than zero.
 const MAX_CONCURRENT_SYNC_ITEMS = 15
 const contentBeingSynced = new Map()
 
@@ -54,21 +56,25 @@ async function syncCallback(api, storage) {
 
           if (synced) {
             return contentId
-          }
-
-          if (!synced && !syncing && contentBeingSynced.size < MAX_CONCURRENT_SYNC_ITEMS) {
-            try {
-              contentBeingSynced.set(contentId, true)
-
-              await storage.synchronize(contentId, () => {
+          } else if (!syncing) {
+            if (contentBeingSynced.size < MAX_CONCURRENT_SYNC_ITEMS) {
+              try {
+                contentBeingSynced.set(contentId, true)
+
+                await storage.synchronize(contentId, () => {
+                  contentBeingSynced.delete(contentId)
+                })
+              } catch (err) {
+                debug(`Failed calling synchronize ${err}`)
                 contentBeingSynced.delete(contentId)
-              })
-            } catch (err) {
-              contentBeingSynced.delete(contentId)
+              }
+            } else {
+              // Content needs to be synced, but limit on concurrent syncs reached
+              debug('Deferring, concurrent sessions exhausted.')
             }
           }
         } catch (err) {
-          //
+          debug(`Failed getting syncStatus. contnetId: ${contentId} ${err}`)
         }
 
         return null