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

trailers under h2 #11

Open
synodriver opened this issue Aug 16, 2022 · 3 comments
Open

trailers under h2 #11

synodriver opened this issue Aug 16, 2022 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@synodriver
Copy link
Member

synodriver commented Aug 16, 2022

As you can see, I have implemented trailer extension for http/2... but with some tricks. According to h2's document

When acting as a server, you may call ``send_headers`` any number of
        times allowed by the following rules, in this order:

        - zero or more times with ``(':status', '1XX')`` (where ``1XX`` is a
          placeholder for any 100-level status code).
        - once with any other status header.
        - zero or one time for trailers.

So h2 will force to close stream when build trailers. When stream got closed, there is no chance to send that trailer. So you must flush that trailer immediately, then the stream is close. But wait, where is the response body? It's still waiting for http/2's priority! And since the stream is closed, you will lose the chance to send the response body. So the server must know which body should also be flushed without waiting for priority. How? Emm..... I added a extra key to do that, called meta. And by using await send({"type": "http.response.body", "body": xxxx, "meta": {"flush": True}}), the server will flush it without waiting. Using this method, I built a gRPC server, and successfully talked to a normal gRPC client. It's just.... that's not a part of asgi spec.

@synodriver
Copy link
Member Author

I'm not very familiar with h2, so if there is anything wrong, please point it out.

@synodriver
Copy link
Member Author

Suddenly I came up with an idea: why not look into the headers? If trailer header exists in headers, then server just prepare to flush, in this case, we can throw meta field away

@synodriver
Copy link
Member Author

synodriver commented Aug 30, 2022

So, according to the comments in asgiref so far, now we have a trailer: bool key in http.response.start, maybe that could be used to determin whether to flush.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant