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

Change attachment InputStream to ChannelProvider #682

Merged
merged 9 commits into from
Sep 10, 2022

Conversation

Tmpod
Copy link
Contributor

@Tmpod Tmpod commented Sep 2, 2022

Also remove full eager consuming of stream/channel when building request and
just pass it directly.

Closes #674 (discussed in this Discord topic as well)

@Tmpod Tmpod changed the title Change attachment InputStream to ByteReadChannel [Draft] Change attachment InputStream to ByteReadChannel Sep 2, 2022
@Tmpod Tmpod force-pushed the task/674/fix-attachment-streams branch from da6977d to dac0337 Compare September 2, 2022 19:47
@Tmpod Tmpod changed the title [Draft] Change attachment InputStream to ByteReadChannel Change attachment InputStream to ByteReadChannel Sep 2, 2022
@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 2, 2022

right, this is a binary incompatible change

@lukellmann
Copy link
Member

You need to run ./gradlew apiDump and commit the changes to make the tests pass

Also remove full eager consuming of stream/channel when building request and
just pass it directly.
Remove a test regarding closing a stream after building the request, which is
no longer significant.

Closes kordlib#674
@Tmpod Tmpod force-pushed the task/674/fix-attachment-streams branch from dac0337 to 854d49e Compare September 2, 2022 20:02
@Tmpod Tmpod requested a review from lukellmann September 3, 2022 17:28
@lukellmann
Copy link
Member

Another thought I had was to use ChannelProvider directly as inputs for the functions (instead of ByteReadChannel). This would mean the channel wouldn't even have to be opened until executing the request. What is your thought on this?

(and btw sorry for requesting so many changes)

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 4, 2022 via email

@lukellmann
Copy link
Member

However, also accepting a channel provider would make this even more versatile (which was kinda the whole point to this change). Perhaps I could change the internals once again to ChannelProvider and write overloads for those addFile methods and such that can accept both providers and channels directly (which would be wrapped in simple providers)?

Sounds good 👍

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 4, 2022 via email

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 4, 2022

So yeah, now it complains that _component2 is using the deprecated inputStream property. The compiler should see that both are deprecated and not raise a warning, no?
Anyways, what do you suggest I do? Just have _component2 mimic inputStream or use a @Suppress? I assume the latter?

Also, would like suggestions regarding that test.

@lukellmann
Copy link
Member

So yeah, now it complains that _component2 is using the deprecated inputStream property. The compiler should see that both are deprecated and not raise a warning, no?
Anyways, what do you suggest I do? Just have _component2 mimic inputStream or use a @Suppress? I assume the latter?

just slap @Suppress("DEPRECATION") on it :)

@lukellmann
Copy link
Member

Also, would like suggestions regarding that test.

I think we actually need to mock a request so that the channel gets consumed. Take a look at StackTraceRecoveryTest for an example.

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 4, 2022

Right... 🤦
That was extra silly lol

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 5, 2022

Also, would like suggestions regarding that test.

I think we actually need to mock a request so that the channel gets consumed. Take a look at StackTraceRecoveryTest for an example.

The channel never seems to get consumed in the mock request, which is odd. I'll push what I have, maybe I'm overlooking something obvious again 🙃

@lukellmann
Copy link
Member

The channel never seems to get consumed in the mock request, which is odd. I'll push what I have, maybe I'm overlooking something obvious again 🙃

Let me check this out and take a look at it.

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 9, 2022 via email

@lukellmann
Copy link
Member

Sure thing! Thanks a lot :)

I PRed the fix here: Tmpod#1

@lukellmann
Copy link
Member

I found another problem with using ByteReadChannel directly without a ChannelProvider:

Here we retry a request when we got a rate limit. This means we call handle again and will rebuild the request here by creating a new MultiPartFormDataContent from the already built request.data. Since ByteReadChannel can only be read once, the second request will also fail because of this.

This is actually also a problem with InputStream that we had before, but now that we refactor this, what should we do?

  • add the convenience functions that take ByteReadChannel (like it is rn)
  • add the convenience functions that take ByteReadChannel but add a warning in the docs
  • don't have these convenience functions (I would actually prefer this)

Of course rate limit on the exact request that uploads a file might not happen very often, but when it does, I'm pretty sure it'll fail.

Copy link
Member

@lukellmann lukellmann left a comment

Choose a reason for hiding this comment

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

Suggestions for updates to deprecation messages and replacements based on this comment: #682 (comment)

@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 10, 2022 via email

@Tmpod Tmpod force-pushed the task/674/fix-attachment-streams branch from ad66008 to d576efa Compare September 10, 2022 12:03
@Tmpod Tmpod requested a review from lukellmann September 10, 2022 13:46
Copy link
Member

@lukellmann lukellmann left a comment

Choose a reason for hiding this comment

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

Looks good overall, just one last question left

@Tmpod Tmpod force-pushed the task/674/fix-attachment-streams branch from d576efa to 7026ad9 Compare September 10, 2022 15:51
@lukellmann lukellmann self-requested a review September 10, 2022 19:11
Copy link
Member

@lukellmann lukellmann left a comment

Choose a reason for hiding this comment

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

I've committed my final touches here.

Could you update title and description of the PR (we no longer use ByteReadChannel and the test wasn't removed)? Then we would be good to go. Thanks for your efforts :)

- consistent `contentProvider` name
- optimize imports
- add @Suppress KDoc tags
@lukellmann lukellmann force-pushed the task/674/fix-attachment-streams branch from 48351b2 to 8722751 Compare September 10, 2022 19:30
@Tmpod
Copy link
Contributor Author

Tmpod commented Sep 10, 2022

I've committed my final touches here.

Perfect!

Could you update title and description of the PR

Yep, good point hehe

Thank you for your excellent reviewing! Helped me get back into Kotlin again :3

@Tmpod Tmpod changed the title Change attachment InputStream to ByteReadChannel Change attachment InputStream to ChannelProvider Sep 10, 2022
@lukellmann lukellmann merged commit 9036b36 into kordlib:0.8.x Sep 10, 2022
@Tmpod Tmpod deleted the task/674/fix-attachment-streams branch September 10, 2022 20:06
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.

Add a streamed multipart request
2 participants