Skip to content

Commit 1a5ed4a

Browse files
authored
fix: re-sort queue after adding tasks (#221)
When tasks are added to an existing list of tasks for a given peer, we need to sort the queue to ensure the order is correct, otherwise we never process pending tasks as task lists with pending tasks need to be moved up the queue. Fixes the build problems exposed in ipfs/js-ipfs#2992 Also upgrade aegir to a safe version.
1 parent 7872a19 commit 1a5ed4a

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
4444
"devDependencies": {
4545
"@nodeutils/defaults-deep": "^1.1.0",
46-
"aegir": "^21.9.0",
46+
"aegir": "^21.10.1",
4747
"async-iterator-all": "^1.0.0",
4848
"benchmark": "^2.1.4",
4949
"buffer": "^5.6.0",

src/decision-engine/req-queue.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ class RequestQueue {
5454
*/
5555
pushTasks (peerId, tasks) {
5656
let peerTasks = this._byPeer.get(peerId.toB58String())
57-
if (peerTasks) {
58-
peerTasks.pushTasks(tasks)
59-
return
57+
58+
if (!peerTasks) {
59+
peerTasks = new PeerTasks(peerId, this._taskMerger)
6060
}
6161

62-
peerTasks = new PeerTasks(peerId, this._taskMerger)
6362
peerTasks.pushTasks(tasks)
6463
this._byPeer.set(peerId.toB58String(), peerTasks)
6564
}

test/decision-engine/req-queue.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,31 @@ describe('Request Queue', () => {
237237
})
238238
})
239239

240+
it('resorts queue when new peer tasks are added where peer tasks already exist', () => {
241+
const rq = new RequestQueue()
242+
243+
rq.pushTasks(peerIds[0], [{
244+
topic: 'a',
245+
size: 0,
246+
priority: 1
247+
}])
248+
rq.pushTasks(peerIds[1], [{
249+
topic: 'a',
250+
size: 0,
251+
priority: 1
252+
}])
253+
rq.pushTasks(peerIds[0], [{
254+
topic: 'a',
255+
size: 1,
256+
priority: 1
257+
}])
258+
259+
// _byPeer map should have been resorted to put peer0
260+
// fist in the queue
261+
const { peerId } = rq.popTasks(16)
262+
expect(peerId).to.eql(peerIds[0])
263+
})
264+
240265
describe('remove', () => {
241266
it('removes tasks by peer and topic', () => {
242267
const rq = new RequestQueue()

0 commit comments

Comments
 (0)