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

stream: allow stream to stay open after take #47023

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

rluvaton
Copy link
Member

@rluvaton rluvaton commented Mar 9, 2023

fix #46980

TODO

  • add tests
  • add docs

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/streams

Comment on lines +401 to +403
if (options?.destroyStream != null) {
validateBoolean(options.destroyStream, 'options.destroyStream');
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe another name would be better? Not sure what though... @benjamingr

Comment on lines 410 to 413
for await (const val of this.iterator({ destroyOnReturn: options?.destroyStream ?? true })) {
if (options?.signal?.aborted) {
throw new AbortError();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I think that if the stream fails we should close the stream, WDYT @ronag ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mcollina any thoughts on your side?

Copy link
Member Author

Choose a reason for hiding this comment

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

after thinking I think it should be closed on an error as in the iterator helpers proposal spec the underlying iterator should be closed when it failed

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Thanks for opening a PR! Can you please add a unit test?

@mcollina
Copy link
Member

mcollina commented Mar 9, 2023

Docs are also missing

@rluvaton
Copy link
Member Author

rluvaton commented Mar 9, 2023

Hey @mcollina I've added tests and docs :)

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 10, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 10, 2023
@rluvaton
Copy link
Member Author

@mcollina is something holding this back from merging? Or we waiting for the TC39?

@mcollina
Copy link
Member

we have 48 hours grace period to allow for folks across the globe to review

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 10, 2023
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Mar 10, 2023
@github-actions
Copy link
Contributor

Failed to start CI
- Validating Jenkins credentials
✔  Jenkins credentials valid
- Starting PR CI job
✘  Failed to start PR CI: 403 Forbidden
https://github.com/nodejs/node/actions/runs/4384604326

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 10, 2023
@github-actions
Copy link
Contributor

Failed to start CI
- Validating Jenkins credentials
✔  Jenkins credentials valid
- Starting PR CI job
✘  Failed to start PR CI: 403 Forbidden
https://github.com/nodejs/node/actions/runs/4385139704

@debadree25 debadree25 added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 11, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 11, 2023
@nodejs-github-bot
Copy link
Collaborator

doc/api/stream.md Outdated Show resolved Hide resolved
Co-authored-by: Debadree Chatterjee <debadree333@gmail.com>
doc/api/stream.md Outdated Show resolved Hide resolved
…fter-take-46980' into feat/allow_stream-to-stay-open-after-take-46980
Copy link
Member

@debadree25 debadree25 left a comment

Choose a reason for hiding this comment

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

I think you would need to remove the merge commit since supposedly it can cause issues with the tooling everything else is good!!

@rluvaton
Copy link
Member Author

I think you would need to remove the merge commit since supposedly it can cause issues with the tooling everything else is good!!

aren't we using squash and merge?

@debadree25
Copy link
Member

I think you would need to remove the merge commit since supposedly it can cause issues with the tooling everything else is good!!

aren't we using squash and merge?

Ref: #46910 (comment)

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

I don't think discussion has exhausted itself in the spec issue and I don't want to deviate from it until we proved we must

@debadree25
Copy link
Member

I don't think discussion has exhausted itself in the spec issue and I don't want to deviate from it until we proved we must

Should be add the blocked label for now then?

@ronag ronag added the blocked PRs that are blocked by other issues or PRs. label Mar 12, 2023
Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

Yeah sorry I meant to explicitly block and hit the wrong button

Copy link
Contributor

@michaelficarra michaelficarra left a comment

Choose a reason for hiding this comment

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

I think what you really want is a chunk (which will be proposed for inclusion in standard iterators in the future) or a nextN helper like

function nextN(iterator, n) {
  const result = [];
  const next = iterator.next;
  for (; n > 0; --n) {
    cont { done, value } = next.call(iterator);
    if (done) break;
    result.push(value);
  }
  return result;
}

@rluvaton
Copy link
Member Author

@michaelficarra no problem but maybe could you update the proposal first so we won't have this back and forth

FYI I think nextN is better name than chunk

@michaelficarra
Copy link
Contributor

@rluvaton chunk is a different method than the nextN that I wrote. See tc39/proposal-iterator-helpers#71 (comment) or https://www.npmjs.com/package/chunk.

@rluvaton
Copy link
Member Author

rluvaton commented Mar 16, 2023

@rluvaton chunk is a different method than the nextN that I wrote. See tc39/proposal-iterator-helpers#71 (comment) or npmjs.com/package/chunk.

@michaelficarra

Oh, after looking at chunk what I really want is nextN which is basically a non-closing take

Do you think that non-closing take will be added?

chunk is less what I want as:

  1. I don't want chunks I just want the x items and the rest left unchanged (like in the headers row from CSV file - explained more in the linked issue)
  2. The data is not evenly distributed
  3. It won't let me get X items without closing the stream, I would just get another iterator that emits in chunks which I can't exit as it will close

@rluvaton
Copy link
Member Author

rluvaton commented Mar 16, 2023

Having nextN as an operator that returns iterable would enable:

  • better chaining
  • better efficiency (no need to save the values in case we don't need - without .toArray())

Example of usages:

having it as an operator:

const responses = await topVisitedUrlsIterator
	.filter(noEmptyLine)
	.drop(1) // Header
	.nextN(8)
	.map(parseLine)
	.map(value => apiCall(value), { concurrency: 4 })
	.toArray();

having it as a static function:

const values = await nextN(
  topVisitedUrlsIterator
    .filter(noEmptyLine)
    .drop(1),
  8)
  .map(parseLine);

// How would I use the concurrency that we would have in the map?
const responses = concurrentMap(values, (value) => apiCall(value), 4);

@rluvaton rluvaton mentioned this pull request May 20, 2023
@aduh95 aduh95 added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Aug 8, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 10, 2023
@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked PRs that are blocked by other issues or PRs. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

stream: Add option to Readable.take operator to not close the stream
8 participants