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

un-deprecate stream-like objects #1520

Merged
merged 1 commit into from
Jun 9, 2020

Conversation

urkle
Copy link
Contributor

@urkle urkle commented Nov 14, 2016

  • this splits the "stream" and "file" inside_route methods
  • file is purely for specifying a file (and utilizing sendfile when it can)
  • stream is purely for stream-like objects.. This allows you to have streaming responses of generated data

This PR is in reference to issue #1392

Pending Questions

  • why are the headers only set for the "stream" method and not the "file" method in inside_routes ?
  • if the behavior is different because of those headers then I should update this to NOT deprecate passing a filename to "stream"
  • what additional tests are needed for this change?

@dblock
Copy link
Member

dblock commented Nov 15, 2016

So the new code is handling in stream? I think there needs to be tests for any new paths. Maybe explicit deprecation tests that warn is called?

Needs an UPGRADING/README too. Thanks.

@milgner
Copy link
Contributor

milgner commented Aug 9, 2017

This looks very promising! @urkle any chance you can adapt this to the current master? Otherwise if anyone is interested, I'd be willing to take this on, too.

@urkle
Copy link
Contributor Author

urkle commented Aug 10, 2017

@milgner it maybe a week or so before I get back around to this. It's been on my TODO list to rebase, add tests and re-submit this

@linchus
Copy link

linchus commented Apr 16, 2018

@urkle Any news in this PR?
Do you need any help?

@urkle
Copy link
Contributor Author

urkle commented Apr 16, 2018

@linchus this needs to be rebased and tests added (as requested). Right now it has been a matter of finding the time to work on it.

@crondaemon
Copy link

Can we help in some way this PR to be merged? The deprecation warning is really noisy when used in regression tests and clutters the output. Since we're using a non-deprecated object, it would be really useful to remove the warning.

@dblock
Copy link
Member

dblock commented May 20, 2020

Can we help in some way this PR to be merged? The deprecation warning is really noisy when used in regression tests and clutters the output. Since we're using a non-deprecated object, it would be really useful to remove the warning.

Reopen the PR, fix per comments above, make it green?

@urkle
Copy link
Contributor Author

urkle commented May 20, 2020

Right now I'm finishing a project in the next week or so upgrading our apps to using Grape 1.3.x. Once that is done I'll be able to revisit this against grape master (as we are still using this functionality)

And this means I'm actually logging a task in our system for me to work on this :)

@crondaemon
Copy link

@urkle thanks for your effort!

@urkle urkle force-pushed the feat-allow-stream-objects branch 2 times, most recently from 9e0329e to fdf5398 Compare June 2, 2020 17:56
@dblock
Copy link
Member

dblock commented Jun 2, 2020

This looks ready code-wise, thanks for following up! We need to document the new behavior in README? Anything to add to UPGRADING?

@urkle
Copy link
Contributor Author

urkle commented Jun 2, 2020

This looks ready code-wise, thanks for following up! We need to document the new behavior in README? Anything to add to UPGRADING?

I just rebased it right now. Going through my changes and refreshing my memory and going to be adding more tests and updating README still. I'll let you know when it's all ready.

@dblock also did you have any thoughts on my first question in the PR? Why the "stream' method sets headers but not the 'file' method?

@urkle urkle force-pushed the feat-allow-stream-objects branch from 9b12fe6 to c7da23b Compare June 2, 2020 21:31
@urkle
Copy link
Contributor Author

urkle commented Jun 2, 2020

@dblock ok.. I pushed up new tests and updates to UPGRADING and README. Let me know what else you want me to adjust/tweak.

@dblock
Copy link
Member

dblock commented Jun 2, 2020

  • why are the headers only set for the "stream" method and not the "file" method in inside_routes ?

I am guessing because for a file you want to let Rack do its thing for content-length and transfer-encoding and generate an e-tag, but for a stream none of these make sense?

  • if the behavior is different because of those headers then I should update this to NOT deprecate passing a filename to "stream"

I think what you did was right. A file is a file, a stream is a stream. Until someone points out something broken I say leave it. But I would also agree to not deprecate it. Possibly simpler?

  • what additional tests are needed for this change?

I would ensure tests for those headers, if we made a mistake at least we will have tests to say that it was thought through behavior.

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

I'm good with this on a green build. The header specs need to be adjusted to what is going on.

Would like another pair of eyes ideally, maybe @ioquatix or @dm1try ?

README.md Outdated
@@ -3168,7 +3168,7 @@ end

Use `body false` to return `204 No Content` without any data or content-type.

You can also set the response to a file with `file`.
You can also set the response to a file with `file` and it will be streamed using Rack::Chunked.
Copy link
Member

Choose a reason for hiding this comment

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

Quote Rack::Chunked

README.md Outdated
@@ -3178,12 +3178,22 @@ class API < Grape::API
end
```

If you want a file to be streamed using Rack::Chunked, use `stream`.
If you want to stream non-file data you use the stream method and a Stream object.
Copy link
Member

Choose a reason for hiding this comment

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

Quote classes

README.md Outdated
@@ -3178,12 +3178,22 @@ class API < Grape::API
end
```

If you want a file to be streamed using Rack::Chunked, use `stream`.
If you want to stream non-file data you use the stream method and a Stream object.
This is simply an object that responds to each and yields for each chunk to send to the client.
Copy link
Member

Choose a reason for hiding this comment

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

Remove "simply", weasel word ;)

Quote each

UPGRADING.md Outdated

Previously in 0.16 stream-like objects were deprecated. This release restores their functionality for use-cases other than file streaming.

For streaming files, simply use file always.
Copy link
Member

Choose a reason for hiding this comment

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

Remove simply, quote file

UPGRADING.md Outdated
end
```

If you want to stream other kinds of content from a streamer object you may.
Copy link
Member

Choose a reason for hiding this comment

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

Or shorter ...

Use stream to stream other kinds of content. In the following example a streamer class ...

UPGRADING.md Outdated
class MyObject
def each
yield '['
# maybe do some paginated DB fetches and return each page
Copy link
Member

Choose a reason for hiding this comment

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

Remove maybe, another weasel word

Copy link
Member

@dm1try dm1try left a comment

Choose a reason for hiding this comment

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

file is purely for specifying a file (and utilizing sendfile when it can)

from the user's point of view, it still sounds ambiguous. at least it should be documented "when it can".

though I would use file only for utilizing sendfile(or even rename it to sendfile) and use stream to produce a chunked response(this could be a file too): those different delivery mechanisms require different changes to the stack, so I would use different interfaces for them to force a user to make an explicit choice.
moreover sendfile is not about streaming, it's about an optimization of static file delivering using web server facilities.

@urkle
Copy link
Contributor Author

urkle commented Jun 9, 2020

file is purely for specifying a file (and utilizing sendfile when it can)

from the user's point of view, it still sounds ambiguous. at least it should be documented "when it can".

though I would use file only for utilizing sendfile(or even rename it to sendfile) and use stream to produce a chunked response(this could be a file too): those different delivery mechanisms require different changes to the stack, so I would use different interfaces for them to force a user to make an explicit choice.
moreover sendfile is not about streaming, it's about an optimization of static file delivering using web server facilities.

@dm1try @dblock
OK.. so how about this.

  1. deprecate fully "file"
  2. add sendfile that does what "file" does today in master
  3. allow "stream" to take a filename (string) or a stream-like object

@dblock
Copy link
Member

dblock commented Jun 9, 2020

I think what is in this PR is good (enough) as it does what it advertises. Before we go into further renaming - @dm1try what do you think about merging this on 🍏 as here and then we can try the suggested change?

@urkle
Copy link
Contributor Author

urkle commented Jun 9, 2020

@dblock I pushed a fixup that does that rework.. but I can extract to a separate PR if you wish.. Up to you.. just let me know.

@urkle
Copy link
Contributor Author

urkle commented Jun 9, 2020

@dblock I think the rework is definitely cleaner and more obvious.

@dm1try
Copy link
Member

dm1try commented Jun 9, 2020

I think what is in this PR is good (enough) as it does what it advertises. Before we go into further renaming - @dm1try what do you think about merging this on 🍏 as here and then we can try the suggested change?

I’m fine with it)

EDITED: oops, I was late with my comment

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Ok, I'm cool with this update, it's definitely better.

Let's bump version to 1.4 as part of this PR?

Fix the build.

UPGRADING.md Outdated

Previously in 0.16 stream-like objects were deprecated. This release restores their functionality for use-cases other than file streaming.

This release also renamed `file` to `sendfile` to better document it's purpose.
Copy link
Member

Choose a reason for hiding this comment

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

it's -> its

We're deprecating file, so I would say "this release deprecated file in favor of sendfile"

UPGRADING.md Outdated

This release also renamed `file` to `sendfile` to better document it's purpose.

To deliver a file via `sendfile` if available do this.
Copy link
Member

Choose a reason for hiding this comment

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

"if available" doesn't make sense to me, I would just remove

to deliver "in chunks" or something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dblock look at how I re-worded it.. as it is not really "in chunks" as if the web server you are running through supports sendfile then it hands it off to that functionality.

UPGRADING.md Outdated
end
```

Use `stream` to stream files
Copy link
Member

Choose a reason for hiding this comment

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

"stream file conntent"?

@urkle urkle requested a review from dblock June 9, 2020 18:02
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Fix the typo below, squash and I'll merge. Thanks for hanging in here!

@@ -4,12 +4,16 @@

describe Rack::Sendfile do
subject do
send_file = file_streamer
contet_object = file_object
Copy link
Member

Choose a reason for hiding this comment

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

content_object, typo, missing n

UPGRADING.md Outdated

To deliver a file via `sendfile` if available do this.
To deliver a file via the Sendfile support in your web server and have the Rack::Sendfile middleware enabled. See [`Rack::Sendfile`](https://www.rubydoc.info/gems/rack/Rack/Sendfile)
Copy link
Member

Choose a reason for hiding this comment

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

Add a period in the end.

UPGRADING.md Outdated
@@ -18,7 +18,7 @@ class API < Grape::API
end
```

Use `stream` to stream files
Use `stream` to stream file content in chunks
Copy link
Member

Choose a reason for hiding this comment

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

Add a period.

@dblock
Copy link
Member

dblock commented Jun 9, 2020

Since we're bumping to 1.4, change version.rb and the version in CHANGELOG too please.

- deprecate file and replace with sendfile
- update stream to not deprecate stream-like objects
@urkle urkle force-pushed the feat-allow-stream-objects branch from 8f8609c to 3db3b0a Compare June 9, 2020 18:39
@urkle urkle requested a review from dblock June 9, 2020 18:41
@dblock dblock merged commit 9198f7e into ruby-grape:master Jun 9, 2020
@dblock
Copy link
Member

dblock commented Jun 9, 2020

Merged, thank you!

@dblock
Copy link
Member

dblock commented Jun 9, 2020

I suggest sending an email to the Grape mailing list describing the change (mostly copy from upgrading) and asking people to comment/test/etc. early.

@urkle urkle deleted the feat-allow-stream-objects branch June 9, 2020 20:08
stanhu added a commit to stanhu/grape that referenced this pull request Jun 12, 2020
ServeFile was renamed to ServeStream in ruby-grape#1520. Calling Grape.eager_load!
would fail with:

```
uninitialized constant Grape::ServeFile (NameError)
```
stanhu added a commit to stanhu/grape that referenced this pull request Jun 28, 2020
ServeFile was renamed to ServeStream in ruby-grape#1520. Calling Grape.eager_load!
would fail with:

```
uninitialized constant Grape::ServeFile (NameError)
```
stanhu added a commit to stanhu/grape that referenced this pull request Jun 28, 2020
ServeFile was renamed to ServeStream in ruby-grape#1520. Calling Grape.eager_load!
would fail with:

```
uninitialized constant Grape::ServeFile (NameError)
```
dblock pushed a commit that referenced this pull request Jun 28, 2020
ServeFile was renamed to ServeStream in #1520. Calling Grape.eager_load!
would fail with:

```
uninitialized constant Grape::ServeFile (NameError)
```
stanhu added a commit to stanhu/grape that referenced this pull request Jul 10, 2020
The pull request ruby-grape#1520 introduced a regression that always caused the
`Cache-Control` HTTP header to be set to `no-cache`, even if the
response wasn't a stream. To fix this, we only set HTTP headers if there
is an actual stream.

Closes ruby-grape#2087
stanhu added a commit to stanhu/grape that referenced this pull request Jul 11, 2020
The pull request ruby-grape#1520 introduced a regression that always caused the
`Cache-Control` HTTP header to be set to `no-cache`, even if the
response wasn't a stream. To fix this, we only set HTTP headers if there
is an actual stream.

Closes ruby-grape#2087
stanhu added a commit to stanhu/grape that referenced this pull request Jul 13, 2020
The pull request ruby-grape#1520 introduced a regression that always caused the
`Cache-Control` HTTP header to be set to `no-cache`, even if the
response wasn't a stream. To fix this, we only set HTTP headers if there
is an actual stream.

Closes ruby-grape#2087
stanhu added a commit to stanhu/grape that referenced this pull request Jul 13, 2020
The pull request ruby-grape#1520 introduced a regression that always caused the
`Cache-Control` HTTP header to be set to `no-cache`, even if the
response wasn't a stream. To fix this, we only set HTTP headers if there
is an actual stream.

Closes ruby-grape#2087
stanhu added a commit to stanhu/grape that referenced this pull request Jul 13, 2020
The pull request ruby-grape#1520 introduced a regression that always caused the
`Cache-Control` HTTP header to be set to `no-cache`, even if the
response wasn't a stream. To fix this, we only set HTTP headers if there
is an actual stream.

Closes ruby-grape#2087
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.

6 participants