Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove max Content-Length middleware #16

Merged
merged 4 commits into from
Oct 21, 2022

Conversation

alanshaw
Copy link
Member

Also removes the memory budget code (which has no bearing on actual memory usage!).

@@ -13,7 +13,8 @@ import { BlockBatch } from './block-batch.js'
* @typedef {import('../bindings').R2Bucket} R2Bucket
*/

const MAX_BLOCK_LENGTH = 1024 * 1024 * 4
// 2MB (max safe libp2p block size) + typical block header length + some leeway
const MAX_ENCODED_BLOCK_LENGTH = (1024 * 1024 * 2) + 39 + 61
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know how much to read from the CAR, but in theory no single block should be bigger than this.

Copy link
Contributor

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is good! Minor notes inline!

const batch = []
let last = offsets[0]
let last = queue[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last is first item in queue? i guess it's the last from the previous batch, but it's a little confusing that we take the head of the queue and call it last.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also why not shift this guy off the queue and add him to the batch immediately, otherwise our first comparison is always with itself!

Comment on lines 30 to 33
const item = queue.shift()
if (!item) break
if (item.carCid.toString() !== last.carCid.toString() || item.offset - last.offset >= MAX_BYTES_BETWEEN) {
queue.unshift(item) // not in this batch, return to pile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but could skip a gozzy shift/unshift operation and invert the logic to make it more positive... like test for the condition we want to be true

Suggested change
const item = queue.shift()
if (!item) break
if (item.carCid.toString() !== last.carCid.toString() || item.offset - last.offset >= MAX_BYTES_BETWEEN) {
queue.unshift(item) // not in this batch, return to pile
```suggestion
const item = queue.at(0)
if (!item) break
if (item.carCid.toString() === last.carCid.toString() && item.offset - last.offset < MAX_BYTES_BETWEEN) {
batch.push(queue.shift(item)) // winner! remove from queue and add to item

const carPath = `${carCid}/${carCid}.car`
const range = {
offset: batch[0].offset,
length: batch[batch.length - 1].offset - batch[0].offset + MAX_ENCODED_BLOCK_LENGTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
length: batch[batch.length - 1].offset - batch[0].offset + MAX_ENCODED_BLOCK_LENGTH
length: batch.at(-1).offset - batch[0].offset + MAX_ENCODED_BLOCK_LENGTH

Comment on lines 131 to 135
if (!res) {
for (const blocks of pendingBlocks.values()) {
blocks.forEach(b => b.resolve())
}
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz comment to explain this! From convo this is to furfil the contract of returning undefined if we dont have the block

src/lib/blockstore.js Show resolved Hide resolved
@alanshaw alanshaw merged commit bf11d0c into main Oct 21, 2022
@alanshaw alanshaw deleted the fix/remove-max-length-middleware branch October 21, 2022 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants