connectrpc / vanguard-go

Support REST, gRPC, gRPC-Web, and Connect clients with one server.
https://pkg.go.dev/connectrpc.com/vanguard
Apache License 2.0
197 stars 13 forks source link

Support compression for streaming REST endpoints that use `google.api.HttpBody` #96

Open jhump opened 10 months ago

jhump commented 10 months ago

Currently, requests received by the RPC handler will usually be compressed if the client compressed them, and responses sent to clients will usually be compressed if the RPC handler chose to compress them. If a message needs to be transcoded (e.g. re-serialized from protobuf to JSON), then it will be transparently re-compressed if the original form was compressed. However this is not the case for streaming REST endpoints, that use google.api.HttpBody messages to stream the body in chunks.

The current condition is largely due to the fact that all other streaming endpoints use per-message compression, not whole-body compression. To correct this, an exception is needed for the transcoder to use whole-body compression.

This will be more complicated to implement for compressed, streamed requests: re-compressing the entire stream, instead of just individual chunks/messages, will require the use of an io.Pipe and a dedicated goroutine to adapt a compressor, which implements io.Writer, to the request body, which must implement io.Reader. Supporting compressed responses, on the other hand, is simpler since both the compressor and the response body (in the form of http.ResponseWriter) both implement io.Writer.