Skip to content

Commit

Permalink
chore: add comment on cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Mar 2, 2023
1 parent 2d4638e commit 0a8c79c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function handle ({ context, logger, batchSize = config.blocksBatchSize }) {
const normalizedWantlist = getNormalizedWantlist(wantlist, context, logger)

// Set state of processing blocks in canceled state
updateCanceledState(normalizedWantlist, context)
updateCanceledState(normalizedWantlist, context, logger)

process.nextTick(async () => {
// catch async error in libp2p connection
Expand Down Expand Up @@ -186,7 +186,11 @@ async function batchResponse ({ blocks, context, logger }) {
let message = new Message()
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]
// console.log('block key', block.key)
const canceledItem = context.canceled.get(block.key)
logger.info({ keyList: context.canceled.keyList }, 'check keyList')
logger.info({ key: block.key }, 'check')
logger.info({ canceled: canceledItem }, 'canceled')
if (!canceledItem || canceledItem !== block.type) {
const size = messageSize[block.type](block)

Expand All @@ -203,6 +207,7 @@ async function batchResponse ({ blocks, context, logger }) {
telemetry.increaseLabelCount('bitswap-block-success-cancel', [block.type])
telemetry.increaseLabelCount('bitswap-cancel-size', [block.type], size)

logger.info({ key: block.key }, 'delete')
context.canceled.delete(block.key)
}
}
Expand All @@ -217,20 +222,23 @@ async function batchResponse ({ blocks, context, logger }) {
}
}

function updateCanceledState (wantList, context) {
function updateCanceledState (wantList, context, logger) {
const { wantedBlocks, wantedHave, canceled } = wantList

// Removed previous canceled blocks
wantedBlocks.forEach(block => {
logger.info({ key: block.key }, 'remove block from canceled')
context.canceled.delete(block.key)
})

wantedHave.forEach(block => {
logger.info({ key: block.key }, 'remove wanted block from canceled')
context.canceled.delete(block.key)
})

// Add new canceled blocks
canceled.forEach(block => {
logger.info({ key: block.key }, 'add block to canceled')
context.canceled.set(block.key, block.wantType)
})
}
Expand Down

0 comments on commit 0a8c79c

Please sign in to comment.