Skip to content

Commit faeab9a

Browse files
committed
fix: cancel message check
1 parent 2d4638e commit faeab9a

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/handler.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ async function batchResponse ({ blocks, context, logger }) {
187187
for (let i = 0; i < blocks.length; i++) {
188188
const block = blocks[i]
189189
const canceledItem = context.canceled.get(block.key)
190-
if (!canceledItem || canceledItem !== block.type) {
190+
191+
if (canceledItem === block.wantType) {
192+
const size = messageSize[block.type](block)
193+
telemetry.increaseLabelCount('bitswap-block-success-cancel', [block.type])
194+
telemetry.increaseLabelCount('bitswap-cancel-size', [block.type], size)
195+
196+
context.canceled.delete(block.key)
197+
} else {
191198
const size = messageSize[block.type](block)
192199

193200
// maxMessageSize MUST BE larger than a single block info/data
@@ -198,12 +205,6 @@ async function batchResponse ({ blocks, context, logger }) {
198205

199206
message.push(block, size, context.protocol)
200207
sentMetrics[block.type](block, size)
201-
} else {
202-
const size = messageSize[block.type](block)
203-
telemetry.increaseLabelCount('bitswap-block-success-cancel', [block.type])
204-
telemetry.increaseLabelCount('bitswap-cancel-size', [block.type], size)
205-
206-
context.canceled.delete(block.key)
207208
}
208209
}
209210

src/service.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ async function startService ({ peerId, port, peerAnnounceAddr, awsClient, connec
145145
// Note: we never write to this stream - responses are always sent on
146146
// another multiplexed stream.
147147
connection.on('end:receive', () => {
148-
// GC canceled LRU on finish
149-
canceled.clear()
150148
connection.close()
151149
})
152150

test/handler.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ t.test('handle', async t => {
197197

198198
await handle({ context: contextSpy, logger: loggerSpy, batchSize: 1 })
199199

200-
t.equal(connectionSpy.send.callCount, 2) // Only two sends in the 4 block messages
200+
t.equal(connectionSpy.send.callCount, 1) // Only one sends in the 4 block messages given other gets canceled
201201
})
202202

203203
t.test('should handle a request with single batch canceling requested items', async t => {
@@ -356,7 +356,7 @@ t.test('handle', async t => {
356356
])
357357

358358
t.equal(response.blocksInfo.length, 2)
359-
t.equal(response.blocksData.length, 2)
359+
t.equal(response.blocksData.length, 1)
360360

361361
t.ok(responseContainsInfo(response, cid7, BlockPresence.Type.Have))
362362
t.ok(responseContainsInfo(response, cid9, BlockPresence.Type.Have))

0 commit comments

Comments
 (0)