Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

compression.md 3.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Compression
  2. The preferred method for configuring message compression on both clients and
  3. servers is to use
  4. [`encoding.RegisterCompressor`](https://godoc.org/google.golang.org/grpc/encoding#RegisterCompressor)
  5. to register an implementation of a compression algorithm. See
  6. `grpc/encoding/gzip/gzip.go` for an example of how to implement one.
  7. Once a compressor has been registered on the client-side, RPCs may be sent using
  8. it via the
  9. [`UseCompressor`](https://godoc.org/google.golang.org/grpc#UseCompressor)
  10. `CallOption`. Remember that `CallOption`s may be turned into defaults for all
  11. calls from a `ClientConn` by using the
  12. [`WithDefaultCallOptions`](https://godoc.org/google.golang.org/grpc#WithDefaultCallOptions)
  13. `DialOption`. If `UseCompressor` is used and the corresponding compressor has
  14. not been installed, an `Internal` error will be returned to the application
  15. before the RPC is sent.
  16. Server-side, registered compressors will be used automatically to decode request
  17. messages and encode the responses. Servers currently always respond using the
  18. same compression method specified by the client. If the corresponding
  19. compressor has not been registered, an `Unimplemented` status will be returned
  20. to the client.
  21. ## Deprecated API
  22. There is a deprecated API for setting compression as well. It is not
  23. recommended for use. However, if you were previously using it, the following
  24. section may be helpful in understanding how it works in combination with the new
  25. API.
  26. ### Client-Side
  27. There are two legacy functions and one new function to configure compression:
  28. ```go
  29. func WithCompressor(grpc.Compressor) DialOption {}
  30. func WithDecompressor(grpc.Decompressor) DialOption {}
  31. func UseCompressor(name) CallOption {}
  32. ```
  33. For outgoing requests, the following rules are applied in order:
  34. 1. If `UseCompressor` is used, messages will be compressed using the compressor
  35. named.
  36. * If the compressor named is not registered, an Internal error is returned
  37. back to the client before sending the RPC.
  38. * If UseCompressor("identity"), no compressor will be used, but "identity"
  39. will be sent in the header to the server.
  40. 1. If `WithCompressor` is used, messages will be compressed using that
  41. compressor implementation.
  42. 1. Otherwise, outbound messages will be uncompressed.
  43. For incoming responses, the following rules are applied in order:
  44. 1. If `WithDecompressor` is used and it matches the message's encoding, it will
  45. be used.
  46. 1. If a registered compressor matches the response's encoding, it will be used.
  47. 1. Otherwise, the stream will be closed and an `Unimplemented` status error will
  48. be returned to the application.
  49. ### Server-Side
  50. There are two legacy functions to configure compression:
  51. ```go
  52. func RPCCompressor(grpc.Compressor) ServerOption {}
  53. func RPCDecompressor(grpc.Decompressor) ServerOption {}
  54. ```
  55. For incoming requests, the following rules are applied in order:
  56. 1. If `RPCDecompressor` is used and that decompressor matches the request's
  57. encoding: it will be used.
  58. 1. If a registered compressor matches the request's encoding, it will be used.
  59. 1. Otherwise, an `Unimplemented` status will be returned to the client.
  60. For outgoing responses, the following rules are applied in order:
  61. 1. If `RPCCompressor` is used, that compressor will be used to compress all
  62. response messages.
  63. 1. If compression was used for the incoming request and a registered compressor
  64. supports it, that same compression method will be used for the outgoing
  65. response.
  66. 1. Otherwise, no compression will be used for the outgoing response.