encoding.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package encoding defines the interface for the compressor and codec, and
  19. // functions to register and retrieve compressors and codecs.
  20. //
  21. // # Experimental
  22. //
  23. // Notice: This package is EXPERIMENTAL and may be changed or removed in a
  24. // later release.
  25. package encoding
  26. import (
  27. "io"
  28. "strings"
  29. "google.golang.org/grpc/internal/grpcutil"
  30. )
  31. // Identity specifies the optional encoding for uncompressed streams.
  32. // It is intended for grpc internal use only.
  33. const Identity = "identity"
  34. // Compressor is used for compressing and decompressing when sending or
  35. // receiving messages.
  36. //
  37. // If a Compressor implements `DecompressedSize(compressedBytes []byte) int`,
  38. // gRPC will invoke it to determine the size of the buffer allocated for the
  39. // result of decompression. A return value of -1 indicates unknown size.
  40. type Compressor interface {
  41. // Compress writes the data written to wc to w after compressing it. If an
  42. // error occurs while initializing the compressor, that error is returned
  43. // instead.
  44. Compress(w io.Writer) (io.WriteCloser, error)
  45. // Decompress reads data from r, decompresses it, and provides the
  46. // uncompressed data via the returned io.Reader. If an error occurs while
  47. // initializing the decompressor, that error is returned instead.
  48. Decompress(r io.Reader) (io.Reader, error)
  49. // Name is the name of the compression codec and is used to set the content
  50. // coding header. The result must be static; the result cannot change
  51. // between calls.
  52. Name() string
  53. }
  54. var registeredCompressor = make(map[string]Compressor)
  55. // RegisterCompressor registers the compressor with gRPC by its name. It can
  56. // be activated when sending an RPC via grpc.UseCompressor(). It will be
  57. // automatically accessed when receiving a message based on the content coding
  58. // header. Servers also use it to send a response with the same encoding as
  59. // the request.
  60. //
  61. // NOTE: this function must only be called during initialization time (i.e. in
  62. // an init() function), and is not thread-safe. If multiple Compressors are
  63. // registered with the same name, the one registered last will take effect.
  64. func RegisterCompressor(c Compressor) {
  65. registeredCompressor[c.Name()] = c
  66. if !grpcutil.IsCompressorNameRegistered(c.Name()) {
  67. grpcutil.RegisteredCompressorNames = append(grpcutil.RegisteredCompressorNames, c.Name())
  68. }
  69. }
  70. // GetCompressor returns Compressor for the given compressor name.
  71. func GetCompressor(name string) Compressor {
  72. return registeredCompressor[name]
  73. }
  74. // Codec defines the interface gRPC uses to encode and decode messages. Note
  75. // that implementations of this interface must be thread safe; a Codec's
  76. // methods can be called from concurrent goroutines.
  77. type Codec interface {
  78. // Marshal returns the wire format of v.
  79. Marshal(v any) ([]byte, error)
  80. // Unmarshal parses the wire format into v.
  81. Unmarshal(data []byte, v any) error
  82. // Name returns the name of the Codec implementation. The returned string
  83. // will be used as part of content type in transmission. The result must be
  84. // static; the result cannot change between calls.
  85. Name() string
  86. }
  87. var registeredCodecs = make(map[string]Codec)
  88. // RegisterCodec registers the provided Codec for use with all gRPC clients and
  89. // servers.
  90. //
  91. // The Codec will be stored and looked up by result of its Name() method, which
  92. // should match the content-subtype of the encoding handled by the Codec. This
  93. // is case-insensitive, and is stored and looked up as lowercase. If the
  94. // result of calling Name() is an empty string, RegisterCodec will panic. See
  95. // Content-Type on
  96. // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
  97. // more details.
  98. //
  99. // NOTE: this function must only be called during initialization time (i.e. in
  100. // an init() function), and is not thread-safe. If multiple Codecs are
  101. // registered with the same name, the one registered last will take effect.
  102. func RegisterCodec(codec Codec) {
  103. if codec == nil {
  104. panic("cannot register a nil Codec")
  105. }
  106. if codec.Name() == "" {
  107. panic("cannot register Codec with empty string result for Name()")
  108. }
  109. contentSubtype := strings.ToLower(codec.Name())
  110. registeredCodecs[contentSubtype] = codec
  111. }
  112. // GetCodec gets a registered Codec by content-subtype, or nil if no Codec is
  113. // registered for the content-subtype.
  114. //
  115. // The content-subtype is expected to be lowercase.
  116. func GetCodec(contentSubtype string) Codec {
  117. return registeredCodecs[contentSubtype]
  118. }