Repository navigation
feat(server + client): streaming mutations and queries over HTTP - #5700
Conversation
Diagnostics ComparisonDetailsNumbers
Timings and averages
unstable timingsUnstable
|
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| if (pending.size === 0) { | ||
| controller.close(); | ||
| } |
There was a problem hiding this comment.
I'm not sure if there's a race condition in which this could be called prematurely
|
Super, super cool! Some high-level thoughts from running this in prod:
|
|
Another thing I found useful was retaining the I don't feel strongly about this, but just a thought. |
|
Hey thanks @iamnafets
The idea is not to use queries and mutations for "infinite" iterators - we expect them to stream continuously and then end. What you're describing is a subscription (I think) which is implemented in #5713
I know. I've seen some, maybe not all 😬
Again, this feels like a subscription and not a query/mutaiton |
Yeah, I’m still talking about finite queries here. We saw hangups happening as soon as 15s which was quite annoying to debug. You can of course just handle this as a user but it’s not exactly a user concern. |
Sheraff
left a comment
There was a problem hiding this comment.
Wow that's tough to review. Just a few notes
- I haven't looked at the follow up PR yet at the time of writing this review (#5713)
- maybe minor but this was initially messing with my head: to me "hydrate" and "dehydrate" would be the opposite of how they're used here. In my mind you dehydrate to send over the network, and hydrate on the client to "revive" the objects
- as always with these streaming things, I feel like we should test more things but I don't know how to / what exactly. For example
- does this work correctly in ≠ environments? (for example the
nodeJsStreamToReaderEsquewas missing an error event listener, so maybe we're missing tests for a "node client") - does this implementation leak memory? I know with node + promises + streams there are edge-cases where we can have dangling objects without references that still occupy memory, but I'm not really sure how to test for this
- does this work correctly in ≠ environments? (for example the
- most of this review is nitpicks, the implementation seems solid, and the few tests I tried to write "just to see" all seemed to pass correctly
| result.map(async (valueOrPromise, index) => { | ||
| const item = batch.items[index]!; | ||
| try { | ||
| const value = await Promise.resolve(valueOrPromise); |
There was a problem hiding this comment.
Nit: I don't think we need a Promise.resolve() here to use await. It's allowed to await regular values.
There was a problem hiding this comment.
Linter complained b/c i was lazy with the types
| const result = await Promise.resolve(json.result); | ||
| json = { | ||
| result: { | ||
| data: await Promise.resolve(result.data), |
There was a problem hiding this comment.
Same comment, though it's still a nitpick. I just don't know whether this matters or not (as in "does this add a microtask loop or not")
| const result = await Promise.resolve(json.result); | |
| json = { | |
| result: { | |
| data: await Promise.resolve(result.data), | |
| const result = await json.result; | |
| json = { | |
| result: { | |
| data: await result.data, |
| /** | ||
| * Not very pretty, but we need to wrap nested data in promises | ||
| * Our stream producer will only resolve top-level async values or async values that are directly nested in another async value | ||
| */ | ||
| return { | ||
| ...response, | ||
| result: Promise.resolve({ | ||
| ...response.result, | ||
| data: Promise.resolve(response.result.data), | ||
| }), | ||
| }; |
There was a problem hiding this comment.
Here I'm less sure. Why do we need to wrap in Promise.resolve()?
There was a problem hiding this comment.
Because the "serializer" only looks one level deep of async values
It's smelly
| if (isAsyncIterable(value)) { | ||
| if (opts.maxDepth && path.length >= opts.maxDepth) { | ||
| throw new Error('Max depth reached'); | ||
| } |
There was a problem hiding this comment.
Isn't this check already included in the hydrateAsyncIterable call?
| if (isAsyncIterable(value)) { | |
| if (opts.maxDepth && path.length >= opts.maxDepth) { | |
| throw new Error('Max depth reached'); | |
| } | |
| if (isAsyncIterable(value)) { |
And if not, is there a reason not to use the checkMaxDepth function here?
| if (isAsyncIterable(value)) { | |
| if (opts.maxDepth && path.length >= opts.maxDepth) { | |
| throw new Error('Max depth reached'); | |
| } | |
| if (isAsyncIterable(value)) { | |
| const error = checkMaxDepth(path) | |
| if (error) { | |
| throw error | |
| } |
| const reader = | ||
| 'getReader' in from | ||
| ? from.getReader() | ||
| : nodeJsStreamToReaderEsque(from).getReader(); |
There was a problem hiding this comment.
Nit: if we're always immediately calling .getReader() we could return the reader instead of an object with a method to be called.
Co-authored-by: Flo <[email protected]>
…sponse.ts Co-authored-by: Flo <[email protected]>
Co-authored-by: Flo <[email protected]>
Co-authored-by: Flo <[email protected]>
|
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |
🎯 Changes
httpBatchStreamLinkwith an implementation that also handles async generators and deferred promise@trpc/clientand@trpc/serverOther stuff / notes