Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

encoding.md 5.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Encoding
  2. The gRPC API for sending and receiving is based upon *messages*. However,
  3. messages cannot be transmitted directly over a network; they must first be
  4. converted into *bytes*. This document describes how gRPC-Go converts messages
  5. into bytes and vice-versa for the purposes of network transmission.
  6. ## Codecs (Serialization and Deserialization)
  7. A `Codec` contains code to serialize a message into a byte slice (`Marshal`) and
  8. deserialize a byte slice back into a message (`Unmarshal`). `Codec`s are
  9. registered by name into a global registry maintained in the `encoding` package.
  10. ### Implementing a `Codec`
  11. A typical `Codec` will be implemented in its own package with an `init` function
  12. that registers itself, and is imported anonymously. For example:
  13. ```go
  14. package proto
  15. import "google.golang.org/grpc/encoding"
  16. func init() {
  17. encoding.RegisterCodec(protoCodec{})
  18. }
  19. // ... implementation of protoCodec ...
  20. ```
  21. For an example, gRPC's implementation of the `proto` codec can be found in
  22. [`encoding/proto`](https://godoc.org/google.golang.org/grpc/encoding/proto).
  23. ### Using a `Codec`
  24. By default, gRPC registers and uses the "proto" codec, so it is not necessary to
  25. do this in your own code to send and receive proto messages. To use another
  26. `Codec` from a client or server:
  27. ```go
  28. package myclient
  29. import _ "path/to/another/codec"
  30. ```
  31. `Codec`s, by definition, must be symmetric, so the same desired `Codec` should
  32. be registered in both client and server binaries.
  33. On the client-side, to specify a `Codec` to use for message transmission, the
  34. `CallOption` `CallContentSubtype` should be used as follows:
  35. ```go
  36. response, err := myclient.MyCall(ctx, request, grpc.CallContentSubtype("mycodec"))
  37. ```
  38. As a reminder, all `CallOption`s may be converted into `DialOption`s that become
  39. the default for all RPCs sent through a client using `grpc.WithDefaultCallOptions`:
  40. ```go
  41. myclient := grpc.Dial(ctx, target, grpc.WithDefaultCallOptions(grpc.CallContentSubtype("mycodec")))
  42. ```
  43. When specified in either of these ways, messages will be encoded using this
  44. codec and sent along with headers indicating the codec (`content-type` set to
  45. `application/grpc+<codec name>`).
  46. On the server-side, using a `Codec` is as simple as registering it into the
  47. global registry (i.e. `import`ing it). If a message is encoded with the content
  48. sub-type supported by a registered `Codec`, it will be used automatically for
  49. decoding the request and encoding the response. Otherwise, for
  50. backward-compatibility reasons, gRPC will attempt to use the "proto" codec. In
  51. an upcoming change (tracked in [this
  52. issue](https://github.com/grpc/grpc-go/issues/1824)), such requests will be
  53. rejected with status code `Unimplemented` instead.
  54. ## Compressors (Compression and Decompression)
  55. Sometimes, the resulting serialization of a message is not space-efficient, and
  56. it may be beneficial to compress this byte stream before transmitting it over
  57. the network. To facilitate this operation, gRPC supports a mechanism for
  58. performing compression and decompression.
  59. A `Compressor` contains code to compress and decompress by wrapping `io.Writer`s
  60. and `io.Reader`s, respectively. (The form of `Compress` and `Decompress` were
  61. chosen to most closely match Go's standard package
  62. [implementations](https://golang.org/pkg/compress/) of compressors. Like
  63. `Codec`s, `Compressor`s are registered by name into a global registry maintained
  64. in the `encoding` package.
  65. ### Implementing a `Compressor`
  66. A typical `Compressor` will be implemented in its own package with an `init`
  67. function that registers itself, and is imported anonymously. For example:
  68. ```go
  69. package gzip
  70. import "google.golang.org/grpc/encoding"
  71. func init() {
  72. encoding.RegisterCompressor(compressor{})
  73. }
  74. // ... implementation of compressor ...
  75. ```
  76. An implementation of a `gzip` compressor can be found in
  77. [`encoding/gzip`](https://godoc.org/google.golang.org/grpc/encoding/gzip).
  78. ### Using a `Compressor`
  79. By default, gRPC does not register or use any compressors. To use a
  80. `Compressor` from a client or server:
  81. ```go
  82. package myclient
  83. import _ "google.golang.org/grpc/encoding/gzip"
  84. ```
  85. `Compressor`s, by definition, must be symmetric, so the same desired
  86. `Compressor` should be registered in both client and server binaries.
  87. On the client-side, to specify a `Compressor` to use for message transmission,
  88. the `CallOption` `UseCompressor` should be used as follows:
  89. ```go
  90. response, err := myclient.MyCall(ctx, request, grpc.UseCompressor("gzip"))
  91. ```
  92. As a reminder, all `CallOption`s may be converted into `DialOption`s that become
  93. the default for all RPCs sent through a client using `grpc.WithDefaultCallOptions`:
  94. ```go
  95. myclient := grpc.Dial(ctx, target, grpc.WithDefaultCallOptions(grpc.UseCompresor("gzip")))
  96. ```
  97. When specified in either of these ways, messages will be compressed using this
  98. compressor and sent along with headers indicating the compressor
  99. (`content-coding` set to `<compressor name>`).
  100. On the server-side, using a `Compressor` is as simple as registering it into the
  101. global registry (i.e. `import`ing it). If a message is compressed with the
  102. content coding supported by a registered `Compressor`, it will be used
  103. automatically for decompressing the request and compressing the response.
  104. Otherwise, the request will be rejected with status code `Unimplemented`.