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

Pausable response streams #1179

Merged
merged 4 commits into from
Aug 19, 2018

Conversation

ashleysommer
Copy link
Member

@ashleysommer ashleysommer commented Mar 29, 2018

This commit adds handlers for the asyncio/uvloop protocol callbacks for pause_writing and resume_writing.

These are needed for the correct functioning of built-in tcp flow-control provided by uvloop and asyncio. See #1176
This is somewhat of a breaking change, because the write function in user streaming callbacks now must be awaited.
This is necessary because it is possible now that the http protocol may be paused, and any calls to write may need to wait on an async event to be called to become unpaused.

Updated examples and tests to reflect this change.

This change does not apply to websocket connections. A change to websocket connections may be required to match this change.

@ashleysommer
Copy link
Member Author

ashleysommer commented Mar 29, 2018

Tests are failing because new aiohttp v3.1.0 released 7 days ago (https://github.com/aio-libs/aiohttp/releases#v3.1.0) brings in compatibility problems.
Requires this PR: #1180

@asvetlov
Copy link
Contributor

Much better but as you've mentioned the change is not backward compatible.
Moreover the breakage is awful: old existing code stops writing streamed data without any exception (old code has no await before write(data) call).

I don't know is it ok or not (Sanic is in Beta stage, this assumes some place for breaking changes if needed).

@ashleysommer ashleysommer force-pushed the pausable_response_stream branch 2 times, most recently from 75dc05f to 89df1b4 Compare March 29, 2018 21:00
@ashleysommer
Copy link
Member Author

I've rebased this PR onto the current master, so the tests should pass now.

@ashleysommer
Copy link
Member Author

Damn, that hanging bug in the tests from last week is back.

@ashleysommer
Copy link
Member Author

Is there a way to force the travis test to run again, without pushing another change?

@seemethere
Copy link
Member

There should be a Restart Build button, but that may not be there for people without write access 🤔

@ashleysommer
Copy link
Member Author

No, I don't see the "restart build" button because I don't have write access.
Doesn't matter, I checked the output of the travis error log more closely and noticed there was actually a failure on the flake8 check, so I needed to push a correction for that anyway.

@yunstanford
Copy link
Member

this is cool, we may wanna bump up the major version for next release

@ashleysommer
Copy link
Member Author

Ok, well since it's likely a major version bump, are there any other breaking changes we've been wanting to make but holding off?

@DirkGuijt
Copy link
Contributor

Any news on this? I would be very happy with a new release :)

@yunstanford
Copy link
Member

cc: @seemethere

@ashleysommer
Copy link
Member Author

@seemethere @yunstanford Any update on getting this merged?
There's significant push by enthusiastic and passionate users at the moment to get a new version of Sanic published. I believe important PRs such as this one, and #1220 (which is a similar idea, except for requests) should be merged asap in order to be tested by upstream master users before a new release goes out.

…or pause_writing and resume_writing.

These are needed for the correct functioning of built-in tcp flow-control provided by uvloop and asyncio.
This is somewhat of a breaking change, because the `write` function in user streaming callbacks now must be `await`ed.
This is necessary because it is possible now that the http protocol may be paused, and any calls to write may need to wait on an async event to be called to become unpaused.

Updated examples and tests to reflect this change.

This change does not apply to websocket connections. A change to websocket connections may be required to match this change.
@ashleysommer ashleysommer force-pushed the pausable_response_stream branch from 4931887 to ec0b99f Compare June 15, 2018 09:40
@ashleysommer
Copy link
Member Author

I've just rebased this PR to current master HEAD. There wasn't any conflicts, but I didn't want it to drift too far away from current.

@r0fls
Copy link
Contributor

r0fls commented Jun 16, 2018

@ashleysommer looks like there are some tox/pep8 errors:

sanic/server.py:80:5: E303 too many blank lines (2)
sanic/server.py:399:9: E303 too many blank lines (2)

@ashleysommer
Copy link
Member Author

@Rofls, those must've been caused by the rebase. I've just fixed the formatting up, should pass now.

@@ -83,7 +83,7 @@ def test_request_stream_app():
body = await request.stream.get()
if body is None:
break
response.write(body.decode('utf-8'))
await response.write(body.decode('utf-8'))
Copy link
Contributor

Choose a reason for hiding this comment

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

PR looks good. Hoever, won't this require an update to the docs to use the await syntax for streaming?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. You're right.
I changed the response-streaming example, but I forgot to change the response-streaming docs.

add await syntax to response.write in response-streaming docs.
@ashleysommer
Copy link
Member Author

@r0fls
Updated docs with await syntax


@streaming_app.listener('after_server_start')
async def run_stream(app, loop):
await response.stream()
assert response.transport.write.call_args_list[1][0][0] == (
# assert response.protocol.push_data.call_args_list[1][0][0] == (
Copy link
Contributor

Choose a reason for hiding this comment

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

these comments seem accidental -- did you mean to leave them here? They seem to be the exact same as the tests themselves

Copy link
Member Author

Choose a reason for hiding this comment

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

TBH, I don't remember.
They look accidental. I will remove them.

@r0fls r0fls merged commit 30e6a31 into sanic-org:master Aug 19, 2018
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