encoding

package
v1.42.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2022 License: Apache-2.0 Imports: 7 Imported by: 3

Documentation

Overview

Package encoding defines the interface for the compressor and codec, and functions to register and retrieve compressors and codecs.

Experimental

Notice: This package is EXPERIMENTAL and may be changed or removed in a later release.

Index

Constants

View Source
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

func GetArgType added in v1.42.6

func GetArgType(v interface{}) string

GetArgType is copied from hessian, to get java class type of interface

func RegisterCodec added in v1.10.0

func RegisterCodec(codec TwoWayCodec)

RegisterCodec registers the provided Codec for use with all gRPC clients and servers.

The Codec will be stored and looked up by result of its Name() method, which should match the content-subtype of the encoding handled by the Codec. This is case-insensitive, and is stored and looked up as lowercase. If the result of calling Name() is an empty string, RegisterCodec will panic. See Content-Type on https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for more details.

NOTE: this function must only be called during initialization time (i.e. in an init() function), and is not thread-safe. If multiple Compressors are registered with the same name, the one registered last will take effect.

func RegisterCompressor

func RegisterCompressor(c Compressor)

RegisterCompressor registers the compressor with gRPC by its name. It can be activated when sending an RPC via grpc.UseCompressor(). It will be automatically accessed when receiving a message based on the content coding header. Servers also use it to send a response with the same encoding as the request.

NOTE: this function must only be called during initialization time (i.e. in an init() function), and is not thread-safe. If multiple Compressors are registered with the same name, the one registered last will take effect.

Types

type Codec added in v1.10.0

type Codec interface {
	// Marshal returns the wire format of v.
	Marshal(v interface{}) ([]byte, error)
	// Unmarshal parses the wire format into v.
	Unmarshal(data []byte, v interface{}) 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. Note that implementations of this interface must be thread safe; a Codec's methods can be called from concurrent goroutines.

type Compressor

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(w io.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(r io.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 or receiving messages.

func GetCompressor

func GetCompressor(name string) Compressor

GetCompressor returns Compressor for the given compressor name.

type PBTwoWayCodec added in v1.42.6

type PBTwoWayCodec struct {
	// contains filtered or unexported fields
}

PBTwoWayCodec is pb impl of TwoWayCodec

type PBWrapperTwoWayCodec added in v1.42.6

type PBWrapperTwoWayCodec struct {
	// contains filtered or unexported fields
}

PBWrapperTwoWayCodec is codec impl of pb

func (*PBWrapperTwoWayCodec) MarshalRequest added in v1.42.6

func (h *PBWrapperTwoWayCodec) MarshalRequest(v interface{}) ([]byte, error)

MarshalRequest marshal interface @v to []byte

func (*PBWrapperTwoWayCodec) MarshalResponse added in v1.42.6

func (h *PBWrapperTwoWayCodec) MarshalResponse(v interface{}) ([]byte, error)

MarshalResponse marshal interface @v to []byte

func (*PBWrapperTwoWayCodec) Name added in v1.42.6

func (h *PBWrapperTwoWayCodec) Name() string

func (*PBWrapperTwoWayCodec) UnmarshalRequest added in v1.42.6

func (h *PBWrapperTwoWayCodec) UnmarshalRequest(data []byte, v interface{}) error

UnmarshalRequest unmarshal bytes @data to interface

func (*PBWrapperTwoWayCodec) UnmarshalResponse added in v1.42.6

func (h *PBWrapperTwoWayCodec) UnmarshalResponse(data []byte, v interface{}) error

UnmarshalResponse unmarshal bytes @data to interface

type TwoWayCodec added in v1.42.6

type TwoWayCodec interface {
	MarshalRequest(interface{}) ([]byte, error)
	MarshalResponse(interface{}) ([]byte, error)
	UnmarshalRequest(data []byte, v interface{}) error
	UnmarshalResponse(data []byte, v interface{}) error

	Name() string
}

TwoWayCodec is directly used by triple network logic It can specify the marshal and unmarshal logic of req and rsp

func GetCodec added in v1.10.0

func GetCodec(contentSubtype string) TwoWayCodec

GetCodec gets a registered Codec by content-subtype, or nil if no Codec is registered for the content-subtype.

The content-subtype is expected to be lowercase.

func NewPBWrapperTwoWayCodec added in v1.42.6

func NewPBWrapperTwoWayCodec(name string, innerCodec, pbCodec Codec) TwoWayCodec

NewPBWrapperTwoWayCodec new common.TwoWayCodec PBWrapperTwoWayCodec with @codecName defined Codec inside

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 proto defines the protobuf codec.
Package proto defines the protobuf codec.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL