Skip to content

Commit

Permalink
Avoid recursive calls to _fetchRecData
Browse files Browse the repository at this point in the history
  • Loading branch information
animetosho committed Oct 18, 2023
1 parent 7cda3ff commit c6cc87b
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions lib/par2.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,14 @@ var GFWrapper = {
// buffer can be used for fetching
var nextIdx = idx + this.recData.length;
if(nextIdx < this.recoverySlices.length)
this._fetchRecData(nextIdx);
// defer callback to avoid recursive nesting (since _fetchRecData can call _markRecDataConsumed)
setImmediate(this._fetchRecData.bind(this, nextIdx));
else {
// if we've consumed everything, free up recovery data memory
this.recData[groupIdx] = null;
if(this._isRecoveryProcessed() && this.recCompleteCb)
this.recCompleteCb();
// ensure this callback is called *after* any other pending callbacks
process.nextTick(this.recCompleteCb.bind(this));
}
}
},
Expand Down Expand Up @@ -314,30 +316,15 @@ var GFWrapper = {
bufs[i] = bufferSlice.call(self.recData[baseBufIdx + i], 0, self.chunkSize);
}

var updateCallbackCount = 0;
self.recDataHashers[hasherIdx].update(bufs, function() {
updateCallbackCount++;
// remove refs on buffers
for(var i=0; i<numBufs; i++) {
var _i = i+hashBaseIdx;
self._markRecDataConsumed(_i);

// notify that hashing is complete
if(self.recDataHashCb[_i]) {
// debug weird failure
if(self.recDataHashCb[_i] === true)
throw new Error('Trying to call rec-data hash callback more than once! [Debug] ' + JSON.stringify({
hashBaseIdx: hashBaseIdx,
hashIdx: i,
invokeIdx: idx,
numBufs: numBufs,
recSlices: self.recoverySlices.length,
callbackCount: updateCallbackCount,
batchSize: self._gfOpts.hashBatchSize,
batchCbTypes: self.recDataHashCb.slice(hashBaseIdx, hashBaseIdx+numBufs).map(function(c){return typeof c}).join(',')
}));
if(self.recDataHashCb[_i])
self.recDataHashCb[_i](_i);
}
self.recDataHashCb[_i] = true;
}
});
Expand Down

0 comments on commit c6cc87b

Please sign in to comment.