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

Set Content-Length header in NodeHTTPTransport to disable chunked encoding #427

Merged

Conversation

MichaelAquilina
Copy link
Contributor

@MichaelAquilina MichaelAquilina commented May 9, 2019

Changes

Disable Chunked http encoding for unary requests in NodeHttpTransport.

Chunked encoding is only useful when we do not know what the size of our message will be. Using Raw http encoding means less overall data sent which should be more efficient.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding#Chunked_encoding

Setting the Content-Length will disable node's http library default chunked encoding:

https://nodejs.org/api/http.html#http_http_request_url_options_callback

Verification

Tested it locally with my own project.

@MichaelAquilina MichaelAquilina changed the title Set Content-Length header in NodeHTTPTransport to use Raw Http encoding Set Content-Length header in NodeHTTPTransport to disable chunked encoding May 9, 2019
@jonny-improbable
Copy link
Contributor

Hey @MichaelAquilina, thank you for contributing 🙌

If I understand this change correctly then the content-length header should only set for unary requests. If that's correct please can you make this change? FYI, you can determine the request type from via the options member property.

It would also be great if you could look to add a unit test for this option; however I appreciate that this package has not test framework - however I would be happy to help guide you on how to contribute such a test if you have the time and interest.

Copy link
Contributor

@jonny-improbable jonny-improbable left a comment

Choose a reason for hiding this comment

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

Only set content-length for unary requests.

@johanbrandhorst
Copy link
Contributor

Potentially superceded by #446.

@jonny-improbable
Copy link
Contributor

@MichaelAquilina are you still planning on working on this issue?

@MichaelAquilina
Copy link
Contributor Author

Hi @jonny-improbable sorry the delay! Working on it right now however I am struggling to understand how you would identify a unary request from the options you pointed out (looking at https://github.com/MichaelAquilina/grpc-web/blob/1860f230f669c03ef3e410391f6888548f184573/client/grpc-web/src/transports/Transport.ts#L23-L30)

@jonny-improbable
Copy link
Contributor

Hey @MichaelAquilina, that's great news :)

I am struggling to understand how you would identify a unary request from the options you pointed out

The methodDefinition property should give you what you need - in particular the requestStream and responseStream members of that object - here's the relevant API Reference.

@@ -18,6 +18,10 @@ class NodeHttp implements grpc.Transport {
}

sendMessage(msgBytes: Uint8Array) {
if (!this.options.methodDefinition.requestStream) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I understand we only care about checking if the requestStream is false but you mentioned responseStream in your comment too. Perhaps I am missing something?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you only want to set the content-length header for unary methods you will need to check that both requestStream and responseStream are false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated :)

@jonny-improbable
Copy link
Contributor

jonny-improbable commented Jun 11, 2019

Fantastic; 3 things

  1. You have a linting error: src/index.ts(21,64): error TS2551: Property 'option' does not exist on type 'NodeHttp'. Did you mean 'options'? - this makes me concerned that this change may have not been tested - please can you confirm with a manual test again?
  2. Please can you update the Description of this PR to reflect the fact it only disables chunked-encoding for unary requests - this is linked to from the changelog.
  3. Tests would be ace, however I appreciate that no unit testing framework has been put in place for this transport - I would really appreciate it if you were in a position to copy the jest configuration from the grpc-web-fake-transport sibling package and add a unit test for this business logic; however I understand if that's not possible (and I will aim to backfill at a later date).

Thanks.

@MichaelAquilina
Copy link
Contributor Author

1 is done.

I can attempt 2 but I suspect I might be slow to actually implement this as I have very little experience with jest.

Happy to attempt it if you are patient enough for it though :)

@jonny-improbable
Copy link
Contributor

@MichaelAquilina I'm going to merge this PR in-place so as to unblock others in your position. I've cut #519 to remind us to backfill the missing test.

@jonny-improbable jonny-improbable merged commit c17fbd1 into improbable-eng:master Aug 5, 2019
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.

4 participants