encoding
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package encoding defines the interface for the compressor and codec, andfunctions to register and retrieve compressors and codecs.
Experimental¶
Notice: This package is EXPERIMENTAL and may be changed or removed in alater release.
Index¶
Constants¶
const Identity = "identity"Identity specifies the optional encoding for uncompressed streams.It is intended for grpc internal use only.
Variables¶
This section is empty.
Functions¶
funcRegisterCodec¶added inv1.10.0
func RegisterCodec(codecCodec)
RegisterCodec registers the provided Codec for use with all gRPC clients andservers.
The Codec will be stored and looked up by result of its Name() method, whichshould match the content-subtype of the encoding handled by the Codec. Thisis case-insensitive, and is stored and looked up as lowercase. If theresult of calling Name() is an empty string, RegisterCodec will panic. SeeContent-Type onhttps://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests formore details.
NOTE: this function must only be called during initialization time (i.e. inan init() function), and is not thread-safe. If multiple Codecs areregistered with the same name, the one registered last will take effect.
funcRegisterCodecV2¶added inv1.66.0
func RegisterCodecV2(codecCodecV2)
RegisterCodecV2 registers the provided CodecV2 for use with all gRPC clients andservers.
The CodecV2 will be stored and looked up by result of its Name() method, whichshould match the content-subtype of the encoding handled by the CodecV2. Thisis case-insensitive, and is stored and looked up as lowercase. If theresult of calling Name() is an empty string, RegisterCodecV2 will panic. SeeContent-Type onhttps://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests formore details.
If both a Codec and CodecV2 are registered with the same name, the CodecV2will be used.
NOTE: this function must only be called during initialization time (i.e. inan init() function), and is not thread-safe. If multiple Codecs areregistered with the same name, the one registered last will take effect.
funcRegisterCompressor¶
func RegisterCompressor(cCompressor)
RegisterCompressor registers the compressor with gRPC by its name. It canbe activated when sending an RPC via grpc.UseCompressor(). It will beautomatically accessed when receiving a message based on the content codingheader. Servers also use it to send a response with the same encoding asthe request.
NOTE: this function must only be called during initialization time (i.e. inan init() function), and is not thread-safe. If multiple Compressors areregistered with the same name, the one registered last will take effect.
Types¶
typeCodec¶added inv1.10.0
type Codec interface {// Marshal returns the wire format of v.Marshal(vany) ([]byte,error)// Unmarshal parses the wire format into v.Unmarshal(data []byte, vany)error// Name returns the name of the Codec implementation. The returned string// will be used as part of content type in transmission. The result must be// static; the result cannot change between calls.Name()string}Codec defines the interface gRPC uses to encode and decode messages. Notethat implementations of this interface must be thread safe; a Codec'smethods can be called from concurrent goroutines.
typeCodecV2¶added inv1.66.0
type CodecV2 interface {// Marshal returns the wire format of v. The buffers in the returned// [mem.BufferSlice] must have at least one reference each, which will be freed// by gRPC when they are no longer needed.Marshal(vany) (outmem.BufferSlice, errerror)// Unmarshal parses the wire format into v. Note that data will be freed as soon// as this function returns. If the codec wishes to guarantee access to the data// after this function, it must take its own reference that it frees when it is// no longer needed.Unmarshal(datamem.BufferSlice, vany)error// Name returns the name of the Codec implementation. The returned string// will be used as part of content type in transmission. The result must be// static; the result cannot change between calls.Name()string}CodecV2 defines the interface gRPC uses to encode and decode messages. Notethat implementations of this interface must be thread safe; a CodecV2'smethods can be called from concurrent goroutines.
funcGetCodecV2¶added inv1.66.0
GetCodecV2 gets a registered CodecV2 by content-subtype, or nil if no CodecV2 isregistered for the content-subtype.
The content-subtype is expected to be lowercase.
typeCompressor¶
type Compressor interface {// Compress writes the data written to wc to w after compressing it. If an// error occurs while initializing the compressor, that error is returned// instead.Compress(wio.Writer) (io.WriteCloser,error)// Decompress reads data from r, decompresses it, and provides the// uncompressed data via the returned io.Reader. If an error occurs while// initializing the decompressor, that error is returned instead.Decompress(rio.Reader) (io.Reader,error)// Name is the name of the compression codec and is used to set the content// coding header. The result must be static; the result cannot change// between calls.Name()string}Compressor is used for compressing and decompressing when sending orreceiving messages.
If a Compressor implements `DecompressedSize(compressedBytes []byte) int`,gRPC will invoke it to determine the size of the buffer allocated for theresult of decompression. A return value of -1 indicates unknown size.
funcGetCompressor¶
func GetCompressor(namestring)Compressor
GetCompressor returns Compressor for the given compressor name.
Directories¶
| Path | Synopsis |
|---|---|
Package gzip implements and registers the gzip compressor during the initialization. | Package gzip implements and registers the gzip compressor during the initialization. |
Package internal contains code internal to the encoding package. | Package internal contains code internal to the encoding package. |
Package proto defines the protobuf codec. | Package proto defines the protobuf codec. |